Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: SlackNotification 어노테이션 기반으로 수정 #252

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static TopRankingInfo topRankingResponse(int rank, long score, RankingInf

public static TopRankingInfo topRankingResponse(int rank, UpdateRanking updateRanking) {
return TopRankingInfo.builder()
.rank(rank + 1)
.rank(rank)
.score(updateRanking.score())
.nickname(updateRanking.rankingInfo().nickname())
.image(updateRanking.rankingInfo().image())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public void removeRanking(RankingInfo rankingInfo) {

public TopRankingResponse getMemberRanking(UpdateRanking myRankingInfo) {
List<TopRankingInfo> topRankings = getTopRankings();
Long myRanking = zSetRedisRepository.reverseRank(RANKING, myRankingInfo.rankingInfo());
long myRanking = zSetRedisRepository.reverseRank(RANKING, myRankingInfo.rankingInfo()) + 1;

Optional<TopRankingInfo> myTopRanking = topRankings.stream()
.filter(topRankingInfo -> Objects.equals(topRankingInfo.memberId(), myRankingInfo.rankingInfo().memberId()))
.findFirst();

if (myTopRanking.isPresent()) {
myRanking = (long)myTopRanking.get().rank();
myRanking = myTopRanking.get().rank();
}

TopRankingInfo myRankingInfoResponse = RankingMapper.topRankingResponse(myRanking.intValue(), myRankingInfo);
TopRankingInfo myRankingInfoResponse = RankingMapper.topRankingResponse((int)myRanking, myRankingInfo);

return RankingMapper.topRankingResponses(myRankingInfoResponse, topRankings);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.moabam.global.common.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SlackNotification {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.moabam.global.common.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.moabam.api.infrastructure.slack.SlackService;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@Aspect
@Component
@Profile({"dev", "prod"})
@RequiredArgsConstructor
public class SlackNotificationAspect {

private final SlackService slackService;

@Around("@annotation(com.moabam.global.common.annotation.SlackNotification) && args(request, exception)")
public Object sendSlack(ProceedingJoinPoint joinPoint, HttpServletRequest request, Exception exception)
throws Throwable {
slackService.send(request, exception);

return joinPoint.proceed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestValueException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import com.moabam.global.common.annotation.SlackNotification;
import com.moabam.global.error.exception.BadRequestException;
import com.moabam.global.error.exception.ConflictException;
import com.moabam.global.error.exception.FcmException;
Expand All @@ -26,9 +28,18 @@
import com.moabam.global.error.exception.UnauthorizedException;
import com.moabam.global.error.model.ErrorResponse;

import jakarta.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {

@SlackNotification
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
protected ErrorResponse handleException(HttpServletRequest request, Exception exception) {
return new ErrorResponse(exception.getMessage(), null);
}

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
protected ErrorResponse handleNotFoundException(MoabamException exception) {
Expand Down Expand Up @@ -104,6 +115,12 @@ protected ErrorResponse handleMethodArgumentTypeMismatchException(MethodArgument
return new ErrorResponse(message, null);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingRequestValueException.class)
protected ErrorResponse handleMethodArgumentTypeMismatchException(MissingRequestValueException exception) {
return new ErrorResponse(exception.getMessage(), null);
}

@ExceptionHandler(MaxUploadSizeExceededException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
protected ErrorResponse handleMaxSizeException(MaxUploadSizeExceededException exception) {
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/config
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ void token_info_response_fail(int code) throws Exception {
.andRespond(withStatus(HttpStatusCode.valueOf(code)));

mockMvc.perform(post("/members/login/kakao/oauth")
.flashAttr("authorizationCodeResponse", authorizationCodeResponse))
.content(objectMapper.writeValueAsString(authorizationCodeResponse))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}
}
Loading