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: letter패키지 정리하기 #513

Merged
merged 11 commits into from
Nov 8, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.now.naaga.auth.presentation.interceptor.ManagerAuthInterceptor;
import com.now.naaga.common.presentation.interceptor.RequestMatcherInterceptor;
import java.util.List;

import com.now.naaga.letter.presentation.converter.LetterLogTypeConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.HttpMethod;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
Expand Down Expand Up @@ -39,6 +42,11 @@ public WebConfig(final PlayerArgumentResolver playerArgumentResolver,
this.managerAuthInterceptor = managerAuthInterceptor;
}

@Override
public void addFormatters(final FormatterRegistry registry){
registry.addConverter(new LetterLogTypeConverter());
}

@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(mapAuthInterceptor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@RestControllerAdvice
public class ControllerExceptionHandler {
Expand Down Expand Up @@ -38,6 +39,16 @@ public ResponseEntity<ExceptionResponse> handleTypeMismatchException(final Excep
return ResponseEntity.status(commonExceptionType.httpStatus()).body(exceptionResponse);
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ExceptionResponse> handleArgumentTypeMismatchException(final Exception e) {
final CommonExceptionType commonExceptionType = CommonExceptionType.INVALID_REQUEST_PARAMETERS;
final ExceptionResponse exceptionResponse = new ExceptionResponse(commonExceptionType.errorCode(), commonExceptionType.errorMessage());

log.info("error = {}", exceptionResponse);

return ResponseEntity.status(commonExceptionType.httpStatus()).body(exceptionResponse);
}
Comment on lines +42 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2:

클라이언트 단에서 들어오는 입력 값 검증에서 발생하는 예외는 바로 위 메서드에서 공통적으로 처리해주고 있어요! 그래서 MethodArgumentTypeMismatchException 예외를 위 메서드에 추가하는 게 좋을 것 같습니다 😁

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인정

Copy link
Collaborator Author

@chaewon121 chaewon121 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인정...이지만 제가 던져줘야 하는 예외는 INVALID_REQUEST_BODY 가 아니라 INVALID_REQUEST_PARAMETERS여서 새로운 함수를 만들어서 던져주게 되었습니다 ㅠㅠ 사실 사용자에게 전달해야하는 예외가 요청바디가 잘못되었는지 요청 파라미터가 잘못되었는지 모두 알려줄 필요가 있을까 하는생각이어서 "잘못된 요청입니다"로 모두 보내주는것이 어떨가 생각했지만 논의가 필요하고 일단은 저희가 합의한 예외는 INVALID_REQUEST_PARAMETERS를 던져줘야해서 함수를 새로 만들게 되었네요


@ExceptionHandler(InternalException.class)
public ResponseEntity<ExceptionResponse> handleInternalException(final InternalException e) {
final BaseExceptionType internalExceptionType = e.exceptionType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Game findGameById(final FindGameByIdCommand findGameByIdCommand) {

@Transactional(readOnly = true)
public List<Game> findGamesByStatus(final FindGameByStatusCommand findGameByStatusCommand) {
Player player = playerService.findPlayerById(findGameByStatusCommand.playerId());
final Player player = playerService.findPlayerById(findGameByStatusCommand.playerId());
return gameRepository.findByPlayerIdAndGameStatus(player.getId(), findGameByStatusCommand.gameStatus());
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.now.naaga.letter.application;

import static com.now.naaga.letter.exception.LetterExceptionType.NO_EXIST;

import com.now.naaga.game.application.GameService;
import com.now.naaga.game.application.dto.FindGameInProgressCommand;
import com.now.naaga.game.domain.Game;
import com.now.naaga.letter.application.dto.CreateLetterCommand;
import com.now.naaga.letter.application.letterlog.ReadLetterLogService;
import com.now.naaga.letter.application.letterlog.WriteLetterLogService;
import com.now.naaga.letter.application.letterlog.dto.LetterLogCreateCommand;
import com.now.naaga.letter.application.letterlog.dto.WriteLetterLogCreateCommand;
import com.now.naaga.letter.domain.Letter;
import com.now.naaga.letter.domain.letterlog.ReadLetterLog;
import com.now.naaga.letter.domain.letterlog.WriteLetterLog;
import com.now.naaga.letter.exception.LetterException;
import com.now.naaga.letter.presentation.dto.FindLetterLogByGameCommand;
import com.now.naaga.letter.presentation.dto.FindNearByLetterCommand;
import com.now.naaga.letter.presentation.dto.LetterReadCommand;
import com.now.naaga.letter.repository.LetterRepository;
import com.now.naaga.letter.repository.letterlog.ReadLetterLogRepository;
import com.now.naaga.letter.repository.letterlog.WriteLetterLogRepository;
import com.now.naaga.player.application.PlayerService;
import com.now.naaga.player.domain.Player;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.now.naaga.letter.exception.LetterExceptionType.NO_EXIST;

@Transactional
@Service
public class LetterService {
Expand All @@ -26,43 +31,79 @@ public class LetterService {

private final LetterRepository letterRepository;

private final ReadLetterLogService readLetterLogService;
private final WriteLetterLogService writeLetterLogService;
private final ReadLetterLogRepository readLetterLogRepository;

private final WriteLetterLogRepository writeLetterLogRepository;

private final PlayerService playerService;

private final GameService gameService;

public LetterService(final LetterRepository letterRepository,
final ReadLetterLogService readLetterLogService,
final WriteLetterLogService writeLetterLogService,
final PlayerService playerService) {
final ReadLetterLogRepository readLetterLogRepository,
final WriteLetterLogRepository writeLetterLogRepository,
final PlayerService playerService,
final GameService gameService) {
this.letterRepository = letterRepository;
this.readLetterLogService = readLetterLogService;
this.writeLetterLogService = writeLetterLogService;
this.readLetterLogRepository = readLetterLogRepository;
this.writeLetterLogRepository = writeLetterLogRepository;
this.playerService = playerService;
this.gameService = gameService;
}

public Letter writeLetter(final CreateLetterCommand createLetterCommand) {
final Player player = playerService.findPlayerById(createLetterCommand.playerId());
final Letter letter = new Letter(player, createLetterCommand.position(), createLetterCommand.message());
letterRepository.save(letter);

logWriteLetter(letter);
return letter;
}

private void logWriteLetter(final Letter letter) {
final Game gameInProgress = getGameInProgress(letter.getRegisteredPlayer().getId());
final WriteLetterLog writeLetterLog = new WriteLetterLog(gameInProgress, letter);
writeLetterLogRepository.save(writeLetterLog);
}

public Letter findLetter(final LetterReadCommand letterReadCommand) {
final Player player = playerService.findPlayerById(letterReadCommand.playerId());
final Letter foundLetter = letterRepository.findById(letterReadCommand.letterId())
.orElseThrow(() -> new LetterException(NO_EXIST));

readLetterLogService.log(new LetterLogCreateCommand(player.getId(), foundLetter));
logReadLetter(player, foundLetter);
return foundLetter;
}

private void logReadLetter(final Player player,
final Letter letter) {
final Game gameInProgress = getGameInProgress(player.getId());
final ReadLetterLog readLetterLog = new ReadLetterLog(gameInProgress, letter);
readLetterLogRepository.save(readLetterLog);
}

@Transactional(readOnly = true)
public List<Letter> findNearByLetters(final FindNearByLetterCommand findNearByLetterCommand) {
return letterRepository.findLetterByPositionAndDistance(findNearByLetterCommand.position(), LETTER_RADIUS);
}

public Letter writeLetter(final CreateLetterCommand createLetterCommand) {
final Player player = playerService.findPlayerById(createLetterCommand.playerId());
final Letter letter = new Letter(player, createLetterCommand.position(), createLetterCommand.message());
letterRepository.save(letter);

final WriteLetterLogCreateCommand writeLetterLogCreateCommand = new WriteLetterLogCreateCommand(letter.getId());
writeLetterLogService.log(writeLetterLogCreateCommand);
return letter;

@Transactional(readOnly = true)
public List<Letter> findLetterLogInGame(final FindLetterLogByGameCommand findLetterLogByGameCommand) {
if (findLetterLogByGameCommand.letterLogType().isWrite()) {
final List<WriteLetterLog> writeLetterLogs = writeLetterLogRepository.findByGameId(findLetterLogByGameCommand.gameId());
return writeLetterLogs.stream()
.map(WriteLetterLog::getLetter)
.toList();
}
final List<ReadLetterLog> readLetterLogs = readLetterLogRepository.findByGameId(findLetterLogByGameCommand.gameId());
return readLetterLogs.stream()
.map(ReadLetterLog::getLetter)
.toList();
}

private Game getGameInProgress(final Long playerId) {
final FindGameInProgressCommand findGameByStatusCommand = new FindGameInProgressCommand(playerId);
return gameService.findGameInProgress(findGameByStatusCommand);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.now.naaga.letter.domain.letterlog;

import com.now.naaga.letter.exception.LetterException;

import java.util.Arrays;

import static com.now.naaga.letter.exception.LetterExceptionType.INVALID_LOG_TYPE;

public enum LetterLogType {

READ,
WRITE,
;

public static LetterLogType from(final String logType) {
return Arrays.stream(values())
.filter(enumValue -> enumValue.name().equalsIgnoreCase(logType))
.findFirst()
.orElseThrow(() -> new LetterException(INVALID_LOG_TYPE));
}

public boolean isWrite() {
return this == WRITE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import jakarta.persistence.ManyToOne;
import java.util.Objects;


@Entity
public class ReadLetterLog extends BaseEntity {

Expand Down
Loading