Skip to content

Commit

Permalink
Merge branch 'IT-Pick:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
minseok1015 authored Aug 12, 2024
2 parents a6ccddd + 090c4f7 commit f9553e7
Show file tree
Hide file tree
Showing 24 changed files with 362 additions and 101 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ dependencies {
//mail
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '3.0.5'

// s3 bucket
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package store.itpick.backend.common.exception;

import lombok.Getter;
import store.itpick.backend.common.response.status.ResponseStatus;

@Getter //사용자 정의 예외
public class AuthException extends RuntimeException{
private final ResponseStatus exceptionStatus; //예외 상태 저장

//객체를 매개변수로 예외 객체 생성
public AuthException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}

//객체, 메세지를 매개변수로 예외 객체 생성
public AuthException(ResponseStatus exceptionStatus, String message) {
super(message);
this.exceptionStatus = exceptionStatus;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package store.itpick.backend.common.exception_handler;

import jakarta.annotation.Priority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import store.itpick.backend.common.exception.AuthException;
import store.itpick.backend.common.response.BaseErrorResponse;

@Slf4j
@Priority(0)
@RestControllerAdvice //모든 컨트롤러에서 발생하는 예외를 전역적으로 처리
public class AuthExceptionControllerAdvice {

@ResponseStatus(HttpStatus.BAD_REQUEST) //해당 메서드의 예외 발생시 보낼
@ExceptionHandler(AuthException.class)
public BaseErrorResponse handle_UserException(AuthException e){
log.error("[handle_AuthException]", e);
return new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import store.itpick.backend.common.exception.UserException;
import store.itpick.backend.common.response.BaseErrorResponse;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;

@Slf4j
@Priority(0)
@RestControllerAdvice //모든 컨트롤러에서 발생하는 예외를 전역적으로 처리
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
NO_SUCH_ALGORITHM(5009, HttpStatus.BAD_REQUEST.value(), "인증 번호 생성을 위한 알고리즘을 찾을 수 없습니다."),
AUTH_CODE_IS_NOT_SAME(5010, HttpStatus.BAD_REQUEST.value(), "인증 번호가 일치하지 않습니다."),
MEMBER_EXISTS(5011,HttpStatus.BAD_REQUEST.value(), "이미 존재하는 회원입니다."),
INVALID_PROFILE_IMG(5012,HttpStatus.BAD_REQUEST.value(), "잘못된 이미지 파일입니다."),
UPLOAD_FAIL(5013,HttpStatus.BAD_REQUEST.value(), "파일 업로드에 실패했습니다. 인터넷 연결을 확인하거나, 나중에 다시 시도해 주세요."),
INVALID_USER_DB_VALUE(5014,HttpStatus.BAD_REQUEST.value(), "유저 정보에 오류가 발생했습니다. 관리자에게 문의해주세요."),

/**
* 6000: Debate 오류
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/store/itpick/backend/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package store.itpick.backend.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;
@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials awsCredentials= new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import store.itpick.backend.dto.auth.PostUserRequest;
import store.itpick.backend.dto.auth.PostUserResponse;
import store.itpick.backend.service.AuthService;
import store.itpick.backend.common.exception.UserException;
import store.itpick.backend.common.exception.AuthException;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;
import static store.itpick.backend.util.BindingResultUtils.getErrorMessages;
Expand All @@ -44,7 +44,7 @@ public BaseResponse<RefreshResponse> refresh(@Validated @RequestBody RefreshRequ
public BaseResponse<LoginResponse> login(@Validated @RequestBody LoginRequest authRequest, BindingResult bindingResult) {
log.info("[AuthController.login]");
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
throw new AuthException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
}
return new BaseResponse<>(authService.login(authRequest));
}
Expand All @@ -62,7 +62,7 @@ public BaseResponse<Object> logout(@PreAuthorize long userId) {
@PostMapping("/signup")
public BaseResponse<PostUserResponse> signUp(@Valid @RequestBody PostUserRequest postUserRequest, BindingResult bindingResult) {
if(bindingResult.hasErrors()){
throw new UserException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
throw new AuthException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
}
return new BaseResponse<>(authService.signUp(postUserRequest));
}
Expand Down
33 changes: 15 additions & 18 deletions src/main/java/store/itpick/backend/controller/RankController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import org.openqa.selenium.TimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import store.itpick.backend.common.exception.UserException;
import store.itpick.backend.common.response.BaseErrorResponse;
import store.itpick.backend.common.response.BaseResponse;
import store.itpick.backend.common.response.status.ResponseStatus;
import store.itpick.backend.model.CommunityType;
import store.itpick.backend.model.PeriodType;
import store.itpick.backend.dto.rank.RankResponseDTO;
import store.itpick.backend.common.response.BaseResponse;
import store.itpick.backend.model.rank.CommunityType;
import store.itpick.backend.model.rank.PeriodType;
import store.itpick.backend.dto.rank.RankResponseDTO;
import store.itpick.backend.model.Reference;
import store.itpick.backend.service.*;
Expand All @@ -27,8 +23,6 @@
import java.util.concurrent.TimeUnit;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.BAD_REQUEST;
import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;
import static store.itpick.backend.util.BindingResultUtils.getErrorMessages;

@RestController
@RequestMapping("/rank")
Expand All @@ -55,7 +49,6 @@ public RankController(RankService rankService) {
private SchedulerService schedulerService;



// 최대 재시도 횟수와 재시도 간격 (초)
private static final int MAX_RETRIES = 5;
private static final int RETRY_DELAY_SECONDS = 5;
Expand Down Expand Up @@ -97,9 +90,6 @@ public String getRankFromNamuwiki() {
return executeWithRetries(() -> seleniumService.useDriverForNamuwiki(url), "Namuwiki 데이터 수집");
}




@GetMapping("/reference")
public BaseResponse<RankResponseDTO> getReference(
@RequestParam String community,
Expand All @@ -108,8 +98,6 @@ public BaseResponse<RankResponseDTO> getReference(

RankResponseDTO rankResponse = rankService.getReferenceByKeyword(community, period, keyword);



if (rankResponse == null) {
// 키워드가 없거나 커뮤니티/기간이 없을 경우 적절한 응답 처리
return new BaseResponse<>(null);
Expand All @@ -119,22 +107,22 @@ public BaseResponse<RankResponseDTO> getReference(
}

@GetMapping("/update/naver")
public void getUpdate(){
public void getUpdate() {
keywordService.performDailyTasksNaver();
}

@GetMapping("/update/nate")
public void updateNate(){
public void updateNate() {
keywordService.performDailyTasksNate();
}

@GetMapping("/update/zum")
public void updateZum(){
public void updateZum() {
keywordService.performDailyTasksZum();
}

@GetMapping("/update")
public void update(){
public void update() {
schedulerService.performHourlyTasks();
}

Expand All @@ -161,6 +149,15 @@ public ResponseStatus getRankingList(@RequestParam String community, @RequestPar
return new BaseResponse<>(redis.getRankingList(communityType, periodType, date));
}

@GetMapping("/badge")
public ResponseStatus getRankingBadgeForKeyword(@RequestParam String keyword, @RequestParam String period, @RequestParam String date) {
PeriodType periodType = getPeriodType(period);
if (periodType == null || !isValidatedDate(periodType, date)) {
return new BaseErrorResponse(BAD_REQUEST);
}
return new BaseResponse<>(redis.getRankingBadgeResponse(keyword, periodType, date));
}

@GetMapping("/day/test")
public void dayTest() {
redis.saveDay();
Expand Down
30 changes: 20 additions & 10 deletions src/main/java/store/itpick/backend/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import store.itpick.backend.common.argument_resolver.PreAccessToken;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import store.itpick.backend.common.argument_resolver.PreAuthorize;
import store.itpick.backend.common.exception.UserException;
import store.itpick.backend.common.exception.AuthException;
import store.itpick.backend.common.response.BaseResponse;
import store.itpick.backend.dto.user.*;
import store.itpick.backend.service.S3ImageBucketService;
import store.itpick.backend.service.UserService;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;
Expand All @@ -26,13 +24,14 @@
@RequestMapping("/user")
public class UserController {

@Autowired
private final UserService userService;

private final S3ImageBucketService s3ImageBucketService;

@PatchMapping("/nickname")
public BaseResponse<?> changeNickname(@PreAuthorize long userId, @Validated @RequestBody NicknameRequest nicknameRequest, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
throw new AuthException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
}
userService.changeNickname(userId, nicknameRequest.getNickname());
return new BaseResponse<>(null);
Expand All @@ -53,7 +52,7 @@ public BaseResponse<?> changeBrithDate(@PreAuthorize long userId, @Validated @Re
@PatchMapping("/liked-topics")
public BaseResponse<?> changeLikedTopics(@PreAuthorize long userId, @Validated @RequestBody LikedTopicsRequest likedTopicsRequest, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
throw new AuthException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
}
log.info(String.valueOf(userId));
userService.changeLikedTopics(userId, likedTopicsRequest.getLikedTopicList());
Expand All @@ -73,10 +72,21 @@ public BaseResponse<?> changeEmail(@PreAuthorize long userId, @Validated @Reques
@PatchMapping("/password")
public BaseResponse<?> changePassword(@PreAuthorize long userId, @Validated @RequestBody PasswordRequest passwordRequest, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
throw new AuthException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
}
userService.changePassword(userId, passwordRequest.getPassword());
return new BaseResponse<>(null);
}

@PostMapping("/profile-img")
public BaseResponse<ProfileImgResponse> changeProfileImg(@PreAuthorize long userId,@RequestParam("file") MultipartFile file){
String previousImgUrl = userService.getProfileImgUrl(userId);
if (previousImgUrl != null) {
s3ImageBucketService.deleteImage(previousImgUrl);
}
String imgUrl = s3ImageBucketService.saveProfileImg(file);
userService.changeProfileImg(userId, imgUrl);
return new BaseResponse<>(new ProfileImgResponse(imgUrl));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.itpick.backend.dto.redis;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class GetRankingBadgeResponse {
private Long nateRank;
private Long naverRank;
private Long zumRank;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package store.itpick.backend.dto.user;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class ProfileImgResponse {
private String url;
}
17 changes: 0 additions & 17 deletions src/main/java/store/itpick/backend/model/CommunityType.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/store/itpick/backend/model/rank/CommunityType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package store.itpick.backend.model.rank;

import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequiredArgsConstructor
public enum CommunityType {
TOTAL("total"),
NAVER("naver"),
NATE("nate"),
ZUM("zum");

private final String communityType;

public String value() {
return communityType;
}

public static List<CommunityType> getAllExceptTotal() {
return new ArrayList<>(Arrays.asList(NATE, NAVER, ZUM));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store.itpick.backend.model;
package store.itpick.backend.model.rank;

import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store.itpick.backend.model;
package store.itpick.backend.model.rank;

import lombok.RequiredArgsConstructor;

Expand Down
Loading

0 comments on commit f9553e7

Please sign in to comment.