diff --git a/backend/src/main/java/com/now/naaga/auth/application/AuthService.java b/backend/src/main/java/com/now/naaga/auth/application/AuthService.java index 497e8b48c..d0f364f26 100644 --- a/backend/src/main/java/com/now/naaga/auth/application/AuthService.java +++ b/backend/src/main/java/com/now/naaga/auth/application/AuthService.java @@ -1,7 +1,5 @@ package com.now.naaga.auth.application; -import static com.now.naaga.auth.exception.AuthExceptionType.PASSWORD_MISMATCH; - import com.now.naaga.auth.exception.AuthException; import com.now.naaga.member.application.MemberService; import com.now.naaga.member.application.dto.MemberCommand; @@ -9,6 +7,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import static com.now.naaga.auth.exception.AuthExceptionType.PASSWORD_MISMATCH; + @Transactional @Service public class AuthService { diff --git a/backend/src/main/java/com/now/naaga/auth/exception/AuthExceptionType.java b/backend/src/main/java/com/now/naaga/auth/exception/AuthExceptionType.java index 4b08c1187..855faedc3 100644 --- a/backend/src/main/java/com/now/naaga/auth/exception/AuthExceptionType.java +++ b/backend/src/main/java/com/now/naaga/auth/exception/AuthExceptionType.java @@ -9,13 +9,28 @@ public enum AuthExceptionType implements BaseExceptionType { 100, HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다." - ); + ), + + NOT_EXIST_HEADER( + 101, + HttpStatus.UNAUTHORIZED, + "헤더 정보가 존재하지 않습니다." + ), + + INVALID_HEADER( + 102, + HttpStatus.UNAUTHORIZED, + "헤더 정보가 유효하지 않습니다." + ), + ; private final int errorCode; private final HttpStatus httpStatus; private final String errorMessage; - AuthExceptionType(final int errorCode, final HttpStatus httpStatus, final String errorMessage) { + AuthExceptionType(final int errorCode, + final HttpStatus httpStatus, + final String errorMessage) { this.errorCode = errorCode; this.httpStatus = httpStatus; this.errorMessage = errorMessage; diff --git a/backend/src/main/java/com/now/naaga/auth/infrastructure/AuthenticationExtractor.java b/backend/src/main/java/com/now/naaga/auth/infrastructure/AuthenticationExtractor.java index 7c443b9ea..2ba8ca655 100644 --- a/backend/src/main/java/com/now/naaga/auth/infrastructure/AuthenticationExtractor.java +++ b/backend/src/main/java/com/now/naaga/auth/infrastructure/AuthenticationExtractor.java @@ -2,7 +2,5 @@ public interface AuthenticationExtractor { - String AUTHORIZATION = "Authorization"; - - T extract(String request); + T extract(final String request); } diff --git a/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationDecoder.java b/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationDecoder.java index 815a81103..cf2177972 100644 --- a/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationDecoder.java +++ b/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationDecoder.java @@ -9,10 +9,10 @@ public class BasicAuthenticationDecoder { private static final String BASIC_TYPE = "Basic"; private static final String DELIMITER = ":"; - public String[] decode(String header) { - String authHeaderValue = header.substring(BASIC_TYPE.length()).trim(); - byte[] decodedBytes = Base64.decodeBase64(authHeaderValue); - String decodedString = new String(decodedBytes); + public String[] decode(final String header) { + final String authHeaderValue = header.substring(BASIC_TYPE.length()).trim(); + final byte[] decodedBytes = Base64.decodeBase64(authHeaderValue); + final String decodedString = new String(decodedBytes); return decodedString.split(DELIMITER); } diff --git a/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationExtractor.java b/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationExtractor.java index 41c283e59..07c607e19 100644 --- a/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationExtractor.java +++ b/backend/src/main/java/com/now/naaga/auth/infrastructure/BasicAuthenticationExtractor.java @@ -1,8 +1,12 @@ package com.now.naaga.auth.infrastructure; +import com.now.naaga.auth.exception.AuthException; import com.now.naaga.member.application.dto.MemberCommand; import org.springframework.stereotype.Component; +import static com.now.naaga.auth.exception.AuthExceptionType.INVALID_HEADER; +import static com.now.naaga.auth.exception.AuthExceptionType.NOT_EXIST_HEADER; + @Component public class BasicAuthenticationExtractor implements AuthenticationExtractor { @@ -16,16 +20,16 @@ public BasicAuthenticationExtractor(final BasicAuthenticationDecoder basicAuthen this.basicAuthenticationDecoder = basicAuthenticationDecoder; } - public MemberCommand extract(String header) { + public MemberCommand extract(final String header) { if (header == null) { - throw new RuntimeException("헤더가 존재하지 않습니다."); + throw new AuthException(NOT_EXIST_HEADER); } if (!header.toLowerCase().startsWith(BASIC_TYPE.toLowerCase())) { - throw new RuntimeException("헤더 정보가 잘못됐습니다."); + throw new AuthException(INVALID_HEADER); } - String[] credentials = basicAuthenticationDecoder.decode(header); - String email = credentials[EMAIL_INDEX]; - String password = credentials[PASSWORD_INDEX]; + final String[] credentials = basicAuthenticationDecoder.decode(header); + final String email = credentials[EMAIL_INDEX]; + final String password = credentials[PASSWORD_INDEX]; return new MemberCommand(email, password); } } diff --git a/backend/src/main/java/com/now/naaga/auth/presentation/AuthInterceptor.java b/backend/src/main/java/com/now/naaga/auth/presentation/AuthInterceptor.java index fa737f48c..7f649e449 100644 --- a/backend/src/main/java/com/now/naaga/auth/presentation/AuthInterceptor.java +++ b/backend/src/main/java/com/now/naaga/auth/presentation/AuthInterceptor.java @@ -17,13 +17,16 @@ public class AuthInterceptor implements HandlerInterceptor { private final AuthenticationExtractor authenticationExtractor; private final AuthService authService; - public AuthInterceptor(final AuthenticationExtractor authenticationExtractor, final AuthService authService) { + public AuthInterceptor(final AuthenticationExtractor authenticationExtractor, + final AuthService authService) { this.authenticationExtractor = authenticationExtractor; this.authService = authService; } @Override - public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { + public boolean preHandle(final HttpServletRequest request, + final HttpServletResponse response, + final Object handler) throws Exception { final MemberCommand memberCommand = authenticationExtractor.extract(request.getHeader(HttpHeaders.AUTHORIZATION)); authService.validateAuthentication(memberCommand); request.setAttribute(AUTH_KEY, memberCommand); diff --git a/backend/src/main/java/com/now/naaga/auth/presentation/AuthenticationArgumentResolver.java b/backend/src/main/java/com/now/naaga/auth/presentation/AuthenticationArgumentResolver.java index 5a8e25419..c539679b9 100644 --- a/backend/src/main/java/com/now/naaga/auth/presentation/AuthenticationArgumentResolver.java +++ b/backend/src/main/java/com/now/naaga/auth/presentation/AuthenticationArgumentResolver.java @@ -1,7 +1,5 @@ package com.now.naaga.auth.presentation; -import static com.now.naaga.auth.presentation.AuthInterceptor.AUTH_KEY; - import com.now.naaga.auth.annotation.Auth; import com.now.naaga.member.application.dto.MemberCommand; import jakarta.servlet.http.HttpServletRequest; @@ -12,6 +10,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; +import static com.now.naaga.auth.presentation.AuthInterceptor.AUTH_KEY; + @Component public class AuthenticationArgumentResolver implements HandlerMethodArgumentResolver { diff --git a/backend/src/main/java/com/now/naaga/common/config/WebConfig.java b/backend/src/main/java/com/now/naaga/common/config/WebConfig.java index b131ffe6f..cd2667218 100644 --- a/backend/src/main/java/com/now/naaga/common/config/WebConfig.java +++ b/backend/src/main/java/com/now/naaga/common/config/WebConfig.java @@ -2,19 +2,21 @@ import com.now.naaga.auth.presentation.AuthInterceptor; import com.now.naaga.auth.presentation.AuthenticationArgumentResolver; -import java.util.List; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.util.List; + @Configuration public class WebConfig implements WebMvcConfigurer { private final AuthenticationArgumentResolver authenticationArgumentResolver; private final AuthInterceptor authInterceptor; - public WebConfig(final AuthenticationArgumentResolver authenticationArgumentResolver, final AuthInterceptor authInterceptor) { + public WebConfig(final AuthenticationArgumentResolver authenticationArgumentResolver, + final AuthInterceptor authInterceptor) { this.authenticationArgumentResolver = authenticationArgumentResolver; this.authInterceptor = authInterceptor; } @@ -25,7 +27,7 @@ public void addInterceptors(final InterceptorRegistry registry) { } @Override - public void addArgumentResolvers(List resolvers) { + public void addArgumentResolvers(final List resolvers) { resolvers.add(authenticationArgumentResolver); } } diff --git a/backend/src/main/java/com/now/naaga/game/application/GameService.java b/backend/src/main/java/com/now/naaga/game/application/GameService.java index d8ca73616..6abc88c8a 100644 --- a/backend/src/main/java/com/now/naaga/game/application/GameService.java +++ b/backend/src/main/java/com/now/naaga/game/application/GameService.java @@ -1,9 +1,5 @@ package com.now.naaga.game.application; -import static com.now.naaga.game.exception.GameExceptionType.ALREADY_IN_PROGRESS; -import static com.now.naaga.game.exception.GameExceptionType.INACCESSIBLE_AUTHENTICATION; -import static com.now.naaga.game.exception.GameExceptionType.NOT_EXIST; - import com.now.naaga.game.domain.Game; import com.now.naaga.game.domain.GameStatus; import com.now.naaga.game.exception.GameException; @@ -14,10 +10,13 @@ import com.now.naaga.place.application.PlaceService; import com.now.naaga.place.domain.Place; import com.now.naaga.place.domain.Position; -import java.util.List; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + +import static com.now.naaga.game.exception.GameExceptionType.*; + @Transactional @Service public class GameService { @@ -26,13 +25,16 @@ public class GameService { private final PlaceService placeService; private final MemberService memberService; - public GameService(final GameRepository gameRepository, final PlaceService placeService, final MemberService memberService) { + public GameService(final GameRepository gameRepository, + final PlaceService placeService, + final MemberService memberService) { this.gameRepository = gameRepository; this.placeService = placeService; this.memberService = memberService; } - public Game createGame(final MemberCommand memberCommand, final Position position) { + public Game createGame(final MemberCommand memberCommand, + final Position position) { final List gamesByStatus = findGamesByStatus(memberCommand, GameStatus.IN_PROGRESS.name()); if (!gamesByStatus.isEmpty()) { throw new GameException(ALREADY_IN_PROGRESS); @@ -43,8 +45,21 @@ public Game createGame(final MemberCommand memberCommand, final Position positio return gameRepository.save(game); } + public Game finishGame(final MemberCommand memberCommand, + final Position requestPosition, + final Long gameId) { + final Game game = gameRepository.findById(gameId) + .orElseThrow(() -> new GameException(NOT_EXIST)); + Member member = memberService.findMemberByEmail(memberCommand.getEmail()); + game.validateOwner(member); + game.validateInRange(requestPosition); + game.changeGameStatus(GameStatus.DONE); + return game; + } + @Transactional(readOnly = true) - public Game findGame(final MemberCommand memberCommand, final Long id) { + public Game findGame(final MemberCommand memberCommand, + final Long id) { final Member member = memberService.findMemberByEmail(memberCommand.getEmail()); final Game game = gameRepository.findById(id) .orElseThrow(() -> new GameException(NOT_EXIST)); @@ -55,7 +70,8 @@ public Game findGame(final MemberCommand memberCommand, final Long id) { } @Transactional(readOnly = true) - public List findGamesByStatus(final MemberCommand memberCommand, final String gameStatus) { + public List findGamesByStatus(final MemberCommand memberCommand, + final String gameStatus) { final Member member = memberService.findMemberByEmail(memberCommand.getEmail()); final Long memberId = member.getId(); diff --git a/backend/src/main/java/com/now/naaga/game/domain/Game.java b/backend/src/main/java/com/now/naaga/game/domain/Game.java index 91fd42c7d..29ec6c6d3 100644 --- a/backend/src/main/java/com/now/naaga/game/domain/Game.java +++ b/backend/src/main/java/com/now/naaga/game/domain/Game.java @@ -1,20 +1,21 @@ package com.now.naaga.game.domain; +import com.now.naaga.game.exception.GameException; import com.now.naaga.member.domain.Member; import com.now.naaga.place.domain.Place; -import jakarta.persistence.Entity; -import jakarta.persistence.EnumType; -import jakarta.persistence.Enumerated; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import com.now.naaga.place.domain.Position; +import jakarta.persistence.*; + import java.util.Objects; +import static com.now.naaga.game.exception.GameExceptionType.INACCESSIBLE_AUTHENTICATION; +import static com.now.naaga.game.exception.GameExceptionType.NOT_ARRIVED; + @Entity public class Game { + public static final double MIN_RANGE = 0.05; + @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private Long id; @@ -33,17 +34,37 @@ public class Game { protected Game() { } - public Game(final Member member, final Place place) { + public Game(final Member member, + final Place place) { this(null, GameStatus.IN_PROGRESS, member, place); } - public Game(final Long id, final GameStatus gameStatus, final Member member, final Place place) { + public Game(final Long id, + final GameStatus gameStatus, + final Member member, + final Place place) { this.id = id; this.gameStatus = gameStatus; this.member = member; this.place = place; } + public void validateOwner(final Member member) { + if (!member.equals(this.member)) { + throw new GameException(INACCESSIBLE_AUTHENTICATION); + } + } + + public void validateInRange(final Position position) { + if (!place.isInValidRange(position)) { + throw new GameException(NOT_ARRIVED); + } + } + + public void changeGameStatus(final GameStatus gameStatus) { + this.gameStatus = gameStatus; + } + public Long getId() { return id; } diff --git a/backend/src/main/java/com/now/naaga/game/domain/GameStatus.java b/backend/src/main/java/com/now/naaga/game/domain/GameStatus.java index 07e9474a6..aa2cb942e 100644 --- a/backend/src/main/java/com/now/naaga/game/domain/GameStatus.java +++ b/backend/src/main/java/com/now/naaga/game/domain/GameStatus.java @@ -4,4 +4,5 @@ public enum GameStatus { IN_PROGRESS, DONE, + ; } diff --git a/backend/src/main/java/com/now/naaga/game/exception/GameExceptionType.java b/backend/src/main/java/com/now/naaga/game/exception/GameExceptionType.java index 1da9e3f8f..ecc0e4a56 100644 --- a/backend/src/main/java/com/now/naaga/game/exception/GameExceptionType.java +++ b/backend/src/main/java/com/now/naaga/game/exception/GameExceptionType.java @@ -34,7 +34,9 @@ public enum GameExceptionType implements BaseExceptionType { private final HttpStatus httpStatus; private final String errorMessage; - GameExceptionType(final int errorCode, final HttpStatus httpStatus, final String errorMessage) { + GameExceptionType(final int errorCode, + final HttpStatus httpStatus, + final String errorMessage) { this.errorCode = errorCode; this.httpStatus = httpStatus; this.errorMessage = errorMessage; diff --git a/backend/src/main/java/com/now/naaga/game/presentation/GameController.java b/backend/src/main/java/com/now/naaga/game/presentation/GameController.java index f1bf6be63..d86e7b270 100644 --- a/backend/src/main/java/com/now/naaga/game/presentation/GameController.java +++ b/backend/src/main/java/com/now/naaga/game/presentation/GameController.java @@ -4,19 +4,15 @@ import com.now.naaga.game.application.GameService; import com.now.naaga.game.domain.Game; import com.now.naaga.game.presentation.dto.GameResponse; +import com.now.naaga.game.presentation.dto.GameStatusResponse; import com.now.naaga.member.application.dto.MemberCommand; import com.now.naaga.place.domain.Position; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + import java.net.URI; import java.util.List; import java.util.stream.Collectors; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; @RequestMapping("/games") @RestController @@ -29,23 +25,34 @@ public GameController(final GameService gameService) { } @PostMapping - public ResponseEntity createGame(@Auth final MemberCommand memberCommand, @RequestBody final Position position) { + public ResponseEntity createGame(@Auth final MemberCommand memberCommand, + @RequestBody final Position position) { final Game game = gameService.createGame(memberCommand, position); return ResponseEntity.created(URI.create("/games/" + game.getId())).build(); } + @PatchMapping("/{gameId}") + public ResponseEntity changeGameStatus(@Auth final MemberCommand memberCommand, + @RequestBody final Position position, + @PathVariable final Long gameId) { + final Game game = gameService.finishGame(memberCommand, position, gameId); + return ResponseEntity.ok(GameStatusResponse.from(game)); + } + @GetMapping("/{gameId}") - public ResponseEntity findGame(@Auth final MemberCommand memberCommand, @PathVariable final Long gameId) { + public ResponseEntity findGame(@Auth final MemberCommand memberCommand, + @PathVariable final Long gameId) { final Game game = gameService.findGame(memberCommand, gameId); return ResponseEntity.ok(GameResponse.from(game)); } @GetMapping - public ResponseEntity> findGamesByGameStatus(@Auth final MemberCommand memberCommand, @RequestParam final String gameStatus) { - final List games = gameService.findGamesByStatus(memberCommand, gameStatus); + public ResponseEntity> findGamesByGameStatus(@Auth final MemberCommand memberCommand, + @RequestParam final String status) { + final List games = gameService.findGamesByStatus(memberCommand, status); final List gameResponses = games.stream() .map(GameResponse::from) .collect(Collectors.toList()); return ResponseEntity.ok(gameResponses); } -} \ No newline at end of file +} diff --git a/backend/src/main/java/com/now/naaga/game/presentation/dto/GameResponse.java b/backend/src/main/java/com/now/naaga/game/presentation/dto/GameResponse.java index 9c33ca3d4..5bcd44510 100644 --- a/backend/src/main/java/com/now/naaga/game/presentation/dto/GameResponse.java +++ b/backend/src/main/java/com/now/naaga/game/presentation/dto/GameResponse.java @@ -9,7 +9,9 @@ public class GameResponse { private final PlaceResponse place; private final String gameStatus; - private GameResponse(final Long id, final PlaceResponse place, final String gameStatus) { + private GameResponse(final Long id, + final PlaceResponse place, + final String gameStatus) { this.id = id; this.place = place; this.gameStatus = gameStatus; diff --git a/backend/src/main/java/com/now/naaga/game/presentation/dto/GameStatusResponse.java b/backend/src/main/java/com/now/naaga/game/presentation/dto/GameStatusResponse.java index d3c1cdec9..6537d419a 100644 --- a/backend/src/main/java/com/now/naaga/game/presentation/dto/GameStatusResponse.java +++ b/backend/src/main/java/com/now/naaga/game/presentation/dto/GameStatusResponse.java @@ -7,7 +7,8 @@ public class GameStatusResponse { private final Long id; private final String gameStatus; - private GameStatusResponse(final Long id, final String gameStatus) { + private GameStatusResponse(final Long id, + final String gameStatus) { this.id = id; this.gameStatus = gameStatus; } diff --git a/backend/src/main/java/com/now/naaga/game/repository/GameRepository.java b/backend/src/main/java/com/now/naaga/game/repository/GameRepository.java index 162e98697..0d5265c70 100644 --- a/backend/src/main/java/com/now/naaga/game/repository/GameRepository.java +++ b/backend/src/main/java/com/now/naaga/game/repository/GameRepository.java @@ -2,10 +2,12 @@ import com.now.naaga.game.domain.Game; import com.now.naaga.game.domain.GameStatus; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface GameRepository extends JpaRepository { - List findByMemberIdAndGameStatus(Long memberId, GameStatus gameStatus); + List findByMemberIdAndGameStatus(final Long memberId, + final GameStatus gameStatus); } diff --git a/backend/src/main/java/com/now/naaga/member/application/MemberService.java b/backend/src/main/java/com/now/naaga/member/application/MemberService.java index 537e87cfd..dc490f3ab 100644 --- a/backend/src/main/java/com/now/naaga/member/application/MemberService.java +++ b/backend/src/main/java/com/now/naaga/member/application/MemberService.java @@ -1,13 +1,13 @@ package com.now.naaga.member.application; -import static com.now.naaga.member.exception.MemberExceptionType.NOT_EXIST_MEMBER; - import com.now.naaga.member.domain.Member; import com.now.naaga.member.exception.MemberException; import com.now.naaga.member.persistence.repository.MemberRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import static com.now.naaga.member.exception.MemberExceptionType.NOT_EXIST_MEMBER; + @Transactional @Service public class MemberService { diff --git a/backend/src/main/java/com/now/naaga/member/application/dto/MemberCommand.java b/backend/src/main/java/com/now/naaga/member/application/dto/MemberCommand.java index 3204954bf..78776fe7b 100644 --- a/backend/src/main/java/com/now/naaga/member/application/dto/MemberCommand.java +++ b/backend/src/main/java/com/now/naaga/member/application/dto/MemberCommand.java @@ -9,7 +9,8 @@ public class MemberCommand { private MemberCommand() { } - public MemberCommand(final String email, final String password) { + public MemberCommand(final String email, + final String password) { this.email = email; this.password = password; } diff --git a/backend/src/main/java/com/now/naaga/member/domain/Member.java b/backend/src/main/java/com/now/naaga/member/domain/Member.java index 435b0c445..495928964 100644 --- a/backend/src/main/java/com/now/naaga/member/domain/Member.java +++ b/backend/src/main/java/com/now/naaga/member/domain/Member.java @@ -4,6 +4,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; + import java.util.Objects; @Entity diff --git a/backend/src/main/java/com/now/naaga/member/exception/MemberExceptionType.java b/backend/src/main/java/com/now/naaga/member/exception/MemberExceptionType.java index 540b4ea3c..5b9542ff7 100644 --- a/backend/src/main/java/com/now/naaga/member/exception/MemberExceptionType.java +++ b/backend/src/main/java/com/now/naaga/member/exception/MemberExceptionType.java @@ -16,7 +16,9 @@ public enum MemberExceptionType implements BaseExceptionType { private final HttpStatus httpStatus; private final String errorMessage; - MemberExceptionType(final int errorCode, final HttpStatus httpStatus, final String errorMessage) { + MemberExceptionType(final int errorCode, + final HttpStatus httpStatus, + final String errorMessage) { this.errorCode = errorCode; this.httpStatus = httpStatus; this.errorMessage = errorMessage; diff --git a/backend/src/main/java/com/now/naaga/member/persistence/repository/MemberRepository.java b/backend/src/main/java/com/now/naaga/member/persistence/repository/MemberRepository.java index 401207e44..d84d6f260 100644 --- a/backend/src/main/java/com/now/naaga/member/persistence/repository/MemberRepository.java +++ b/backend/src/main/java/com/now/naaga/member/persistence/repository/MemberRepository.java @@ -1,10 +1,11 @@ package com.now.naaga.member.persistence.repository; import com.now.naaga.member.domain.Member; -import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface MemberRepository extends JpaRepository { - Optional findByEmail(String email); + Optional findByEmail(final String email); } diff --git a/backend/src/main/java/com/now/naaga/place/application/PlaceService.java b/backend/src/main/java/com/now/naaga/place/application/PlaceService.java index c9647e390..1f9c2d57d 100644 --- a/backend/src/main/java/com/now/naaga/place/application/PlaceService.java +++ b/backend/src/main/java/com/now/naaga/place/application/PlaceService.java @@ -1,16 +1,17 @@ package com.now.naaga.place.application; -import static com.now.naaga.place.exception.PlaceExceptionType.PLACE_NOT_FOUND; - import com.now.naaga.place.domain.Place; import com.now.naaga.place.domain.Position; import com.now.naaga.place.exception.PlaceException; import com.now.naaga.place.persistence.repository.PlaceRepository; -import java.util.List; -import java.util.Random; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.Random; + +import static com.now.naaga.place.exception.PlaceExceptionType.PLACE_NOT_FOUND; + @Transactional @Service public class PlaceService { diff --git a/backend/src/main/java/com/now/naaga/place/domain/Place.java b/backend/src/main/java/com/now/naaga/place/domain/Place.java index dd99be377..940a4b931 100644 --- a/backend/src/main/java/com/now/naaga/place/domain/Place.java +++ b/backend/src/main/java/com/now/naaga/place/domain/Place.java @@ -1,12 +1,11 @@ package com.now.naaga.place.domain; -import jakarta.persistence.Embedded; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; + import java.util.Objects; +import static com.now.naaga.game.domain.Game.MIN_RANGE; + @Entity public class Place { @@ -22,6 +21,10 @@ public class Place { protected Place() { } + public boolean isInValidRange(final Position other) { + return position.calculateDistance(other) <= MIN_RANGE; + } + public Long getId() { return id; } diff --git a/backend/src/main/java/com/now/naaga/place/domain/Position.java b/backend/src/main/java/com/now/naaga/place/domain/Position.java index 1476cd5e7..e3b7061a7 100644 --- a/backend/src/main/java/com/now/naaga/place/domain/Position.java +++ b/backend/src/main/java/com/now/naaga/place/domain/Position.java @@ -2,6 +2,7 @@ import jakarta.persistence.Column; import jakarta.persistence.Embeddable; + import java.math.BigDecimal; import java.util.Objects; @@ -17,11 +18,18 @@ public class Position { protected Position() { } - public Position(final BigDecimal latitude, final BigDecimal longitude) { + public Position(final BigDecimal latitude, + final BigDecimal longitude) { this.latitude = latitude; this.longitude = longitude; } + public double calculateDistance(final Position other) { + return Math.acos(Math.sin(Math.toRadians(other.latitude.doubleValue())) * Math.sin(Math.toRadians(this.latitude.doubleValue())) + + (Math.cos(Math.toRadians(other.latitude.doubleValue())) * Math.cos(Math.toRadians(this.latitude.doubleValue())) * Math.cos(Math.toRadians(other.longitude.doubleValue() - this.longitude.doubleValue()))) + ) * 6371.0; + } + public BigDecimal getLatitude() { return latitude; } diff --git a/backend/src/main/java/com/now/naaga/place/exception/PlaceExceptionType.java b/backend/src/main/java/com/now/naaga/place/exception/PlaceExceptionType.java index 760cc65da..bbb3219f0 100644 --- a/backend/src/main/java/com/now/naaga/place/exception/PlaceExceptionType.java +++ b/backend/src/main/java/com/now/naaga/place/exception/PlaceExceptionType.java @@ -16,7 +16,9 @@ public enum PlaceExceptionType implements BaseExceptionType { private final HttpStatus httpStatus; private final String errorMessage; - PlaceExceptionType(final int errorCode, final HttpStatus httpStatus, final String errorMessage) { + PlaceExceptionType(final int errorCode, + final HttpStatus httpStatus, + final String errorMessage) { this.errorCode = errorCode; this.httpStatus = httpStatus; this.errorMessage = errorMessage; diff --git a/backend/src/main/java/com/now/naaga/place/persistence/repository/PlaceRepository.java b/backend/src/main/java/com/now/naaga/place/persistence/repository/PlaceRepository.java index 983cf6764..244604f20 100644 --- a/backend/src/main/java/com/now/naaga/place/persistence/repository/PlaceRepository.java +++ b/backend/src/main/java/com/now/naaga/place/persistence/repository/PlaceRepository.java @@ -2,11 +2,12 @@ import com.now.naaga.place.domain.Place; import com.now.naaga.place.domain.Position; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.List; + public interface PlaceRepository extends JpaRepository { @Query(value = "SELECT place FROM Place place " + @@ -14,6 +15,6 @@ public interface PlaceRepository extends JpaRepository { "SIN(RADIANS(:#{#user_position.latitude})) * SIN(RADIANS(place.position.latitude)) " + "+ (COS(RADIANS(:#{#user_position.latitude})) * COS(RADIANS(place.position.latitude)) * COS(RADIANS(:#{#user_position.longitude} - place.position.longitude)))" + ") * 6371.0 <= :distance") - List findPlaceByPositionAndDistance(@Param(value = "user_position") Position position, - @Param(value = "distance") double distance); + List findPlaceByPositionAndDistance(@Param(value = "user_position") final Position position, + @Param(value = "distance") final double distance); } diff --git a/backend/src/main/java/com/now/naaga/place/presentation/dto/PlaceResponse.java b/backend/src/main/java/com/now/naaga/place/presentation/dto/PlaceResponse.java index 2ab3c0b2c..d18ff604b 100644 --- a/backend/src/main/java/com/now/naaga/place/presentation/dto/PlaceResponse.java +++ b/backend/src/main/java/com/now/naaga/place/presentation/dto/PlaceResponse.java @@ -9,7 +9,9 @@ public class PlaceResponse { private final Position position; private final String imageUrl; - private PlaceResponse(final Long id, final Position position, final String imageUrl) { + private PlaceResponse(final Long id, + final Position position, + final String imageUrl) { this.id = id; this.position = position; this.imageUrl = imageUrl;