diff --git a/src/main/java/carbonneutral/academy/api/controller/use/UseController.java b/src/main/java/carbonneutral/academy/api/controller/use/UseController.java index 071b336..3b62494 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/UseController.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/UseController.java @@ -2,10 +2,7 @@ import carbonneutral.academy.api.controller.use.dto.request.PatchReturnReq; import carbonneutral.academy.api.controller.use.dto.request.PostUseReq; -import carbonneutral.academy.api.controller.use.dto.response.GetHomeRes; -import carbonneutral.academy.api.controller.use.dto.response.GetUseDetailRes; -import carbonneutral.academy.api.controller.use.dto.response.PatchReturnRes; -import carbonneutral.academy.api.controller.use.dto.response.PostUseRes; +import carbonneutral.academy.api.controller.use.dto.response.*; import carbonneutral.academy.api.service.use.UseService; import carbonneutral.academy.common.BaseResponse; import carbonneutral.academy.domain.user.User; @@ -43,6 +40,12 @@ BaseResponse getInUseMultipleTimeContainer(@AuthenticationPrinc return BaseResponse.of(IN_USE_OK, useService.getInUseMultipleTimeContainer(user, useAt)); } + @GetMapping("/location") + @Operation(summary = "QR 이용 시 해당 장소 조회 API",description = "QR 인증을 통해 해당 장소를 조회합니다.") + BaseResponse getLocation(@RequestParam("locationId") int locationId, + @RequestParam("point")int point) { + return BaseResponse.of(LOCATION_OK, useService.getLocation(locationId, point)); + } @PostMapping @Operation(summary = "다회용기 이용 시 API",description = "앱에서 QR 인증을 통해 다회용기를 이용합니다.") BaseResponse useMultipleTimeContainers(@AuthenticationPrincipal User user, diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PatchReturnReq.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PatchReturnReq.java index fbff391..6572839 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PatchReturnReq.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PatchReturnReq.java @@ -11,6 +11,6 @@ @Builder @AllArgsConstructor public class PatchReturnReq { - private String locationName; - private String locationAddress; + + private int returnLocationId; } diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PostUseReq.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PostUseReq.java index d56ab54..14204b7 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PostUseReq.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/request/PostUseReq.java @@ -1,6 +1,5 @@ package carbonneutral.academy.api.controller.use.dto.request; -import jakarta.persistence.Column; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -12,11 +11,7 @@ @AllArgsConstructor public class PostUseReq { - private String locationName; - - private String locationAddress; - + private int locationId; private int point; - private int multiUseContainerId; } diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetLocationRes.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetLocationRes.java new file mode 100644 index 0000000..aff3e3e --- /dev/null +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetLocationRes.java @@ -0,0 +1,24 @@ +package carbonneutral.academy.api.controller.use.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; +import java.util.List; + +@Getter +@NoArgsConstructor +@Builder +@AllArgsConstructor +public class GetLocationRes { + private int locationId; + private String locationName; + private String locationAddress; + private String locationImageUrl; + private BigDecimal latitude; + private BigDecimal longitude; + private int point; + private List multiUseContainerIdList; +} diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseDetailRes.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseDetailRes.java index cb6f91c..f91ce4b 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseDetailRes.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseDetailRes.java @@ -20,6 +20,6 @@ public class GetUseDetailRes { private String locationAddress; private String useAt; private int point; - private String multiUseContainer; + private int multiUseContainerId; private List getReturnResList; } diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseRes.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseRes.java index 60ec68b..1628cd2 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseRes.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/GetUseRes.java @@ -19,5 +19,5 @@ public class GetUseRes { private String locationName; private String useAt; private UseStatus status; - private String multiUseContainerType; + private int multiUseContainerId; } diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PatchReturnRes.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PatchReturnRes.java index 16b617e..8fdda19 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PatchReturnRes.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PatchReturnRes.java @@ -13,8 +13,11 @@ @AllArgsConstructor public class PatchReturnRes { private int returnLocationId; + private String returnLocationName; + private String returnLocationAddress; private String returnTime; - private int point; + private int currentPoint; + private int acquiredPoint; private int userId; private UseStatus status; } diff --git a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PostUseRes.java b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PostUseRes.java index 2a42f81..256a0bb 100644 --- a/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PostUseRes.java +++ b/src/main/java/carbonneutral/academy/api/controller/use/dto/response/PostUseRes.java @@ -6,7 +6,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; -import java.time.LocalDateTime; +import java.math.BigDecimal; @Getter @NoArgsConstructor @@ -17,6 +17,11 @@ public class PostUseRes { private int point; private int userId; private int locationId; + private String locationName; + private String locationAddress; + private String locationImageUrl; + private BigDecimal latitude; + private BigDecimal longitude; private int multiUseContainerId; private UseStatus status; diff --git a/src/main/java/carbonneutral/academy/api/converter/time/TimeConverter.java b/src/main/java/carbonneutral/academy/api/converter/time/TimeConverter.java index 8e0db05..ea4c85f 100644 --- a/src/main/java/carbonneutral/academy/api/converter/time/TimeConverter.java +++ b/src/main/java/carbonneutral/academy/api/converter/time/TimeConverter.java @@ -1,13 +1,11 @@ package carbonneutral.academy.api.converter.time; -import jakarta.persistence.Tuple; import lombok.AccessLevel; import lombok.NoArgsConstructor; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; -import java.util.Map; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class TimeConverter { @@ -26,7 +24,7 @@ public static List toLocalDateTime(String useAt) { } public static String toMonthDayString(LocalDateTime localDateTime) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M월 d일"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 M월 d일"); return localDateTime.format(formatter); } } diff --git a/src/main/java/carbonneutral/academy/api/converter/use/UseConverter.java b/src/main/java/carbonneutral/academy/api/converter/use/UseConverter.java index 234daa9..b25532a 100644 --- a/src/main/java/carbonneutral/academy/api/converter/use/UseConverter.java +++ b/src/main/java/carbonneutral/academy/api/converter/use/UseConverter.java @@ -30,12 +30,17 @@ public static Use toUse(User user, Location location, int point, int multiUseCon .build(); } - public static PostUseRes toPostUseRes(Use use) { + public static PostUseRes toPostUseRes(Use use, Location location) { return PostUseRes.builder() .useAt(TimeConverter.toFormattedDate(use.getUseAt())) .point(use.getPoint()) .userId(use.getUser().getId()) - .locationId(use.getRentalLocation().getId()) + .locationId(location.getId()) + .locationName(location.getName()) + .locationAddress(location.getAddress()) + .locationImageUrl(location.getImageUrl()) + .latitude(location.getLatitude()) + .longitude(location.getLongitude()) .multiUseContainerId(use.getMultiUseContainerId()) .status(use.getStatus()) .build(); @@ -48,7 +53,7 @@ public static GetUseRes toGetUseRes(Use use, Location location, MultiUseContaine .locationName(location.getName()) .useAt(TimeConverter.toFormattedDate(use.getUseAt())) .status(use.getStatus()) - .multiUseContainerType(multiUseContainer.getType()) + .multiUseContainerId(multiUseContainer.getId()) .build(); } @@ -75,7 +80,7 @@ public static GetReturnRes toGetReturnRes(Location returnLocation) { .build(); } - public static GetUseDetailRes toGetUseDetailRes(Use use, Location location, List getReturnResList, String multiUseContainer) { + public static GetUseDetailRes toGetUseDetailRes(Use use, Location location, List getReturnResList, int multiUseContainerId) { return GetUseDetailRes.builder() .rentalLocationId(location.getId()) .locationImageUrl(location.getImageUrl()) @@ -83,18 +88,34 @@ public static GetUseDetailRes toGetUseDetailRes(Use use, Location location, List .locationAddress(location.getAddress()) .useAt(TimeConverter.toFormattedDate(use.getUseAt())) .point(use.getPoint()) - .multiUseContainer(multiUseContainer) + .multiUseContainerId(multiUseContainerId) .getReturnResList(getReturnResList) .build(); } - public static PatchReturnRes toPatchReturnRes(User user, Location returnLocation, Use use) { + public static PatchReturnRes toPatchReturnRes(User user, Location returnLocation, Use use, Point point) { return PatchReturnRes.builder() .returnLocationId(returnLocation.getId()) + .returnLocationName(returnLocation.getName()) + .returnLocationAddress(returnLocation.getAddress()) .returnTime(TimeConverter.toFormattedDate(use.getReturnTime())) - .point(use.getPoint()) + .currentPoint(point.getAccumulatedPoint() - point.getUtilizedPoint()) + .acquiredPoint(use.getPoint()) .userId(user.getId()) .status(use.getStatus()) .build(); } + + public static GetLocationRes toGetLocationRes(Location location, List multiUseContainerIdList, int point) { + return GetLocationRes.builder() + .locationId(location.getId()) + .locationName(location.getName()) + .locationAddress(location.getAddress()) + .locationImageUrl(location.getImageUrl()) + .latitude(location.getLatitude()) + .longitude(location.getLongitude()) + .point(point) + .multiUseContainerIdList(multiUseContainerIdList) + .build(); + } } diff --git a/src/main/java/carbonneutral/academy/api/service/use/UseService.java b/src/main/java/carbonneutral/academy/api/service/use/UseService.java index f1fb86e..acc7b0c 100644 --- a/src/main/java/carbonneutral/academy/api/service/use/UseService.java +++ b/src/main/java/carbonneutral/academy/api/service/use/UseService.java @@ -5,7 +5,6 @@ import carbonneutral.academy.api.controller.use.dto.response.*; import carbonneutral.academy.domain.user.User; -import java.time.LocalDateTime; public interface UseService { @@ -15,4 +14,6 @@ public interface UseService { GetUseDetailRes getInUseMultipleTimeContainer(User user, String useAt); PatchReturnRes returnMultipleTimeContainers(User user, PatchReturnReq patchReturnReq, String useAt); + + GetLocationRes getLocation(int locationId, int point); } diff --git a/src/main/java/carbonneutral/academy/api/service/use/UseServiceImpl.java b/src/main/java/carbonneutral/academy/api/service/use/UseServiceImpl.java index c490ca9..d4ab4b5 100644 --- a/src/main/java/carbonneutral/academy/api/service/use/UseServiceImpl.java +++ b/src/main/java/carbonneutral/academy/api/service/use/UseServiceImpl.java @@ -80,18 +80,39 @@ public GetUseDetailRes getInUseMultipleTimeContainer(User user, String useAt) { .toList(); List returnResList = Stream.concat(returnResList1.stream(), returnResList2.stream()) .toList(); - return UseConverter.toGetUseDetailRes(use, location, returnResList, multiUseContainer.getType()); + return UseConverter.toGetUseDetailRes(use, location, returnResList, multiUseContainer.getId()); } + @Override + public GetLocationRes getLocation(int locationId, int point) { + Location location = locationJpaRepository.findById(locationId).orElseThrow(() -> new BaseException(NOT_FIND_LOCATION)); + if(location.getLocationType().equals(LocationType.RETURN)) { + throw new BaseException(NOT_USE_LOCATION); + } + List multiUseContainerIdList = locationContainerJpaRepository.findByLocation_Id(location.getId()) + .stream() + .map(LocationContainer::getMultiUseContainer) + .map(MultiUseContainer::getId) + .toList(); + return UseConverter.toGetLocationRes(location, multiUseContainerIdList,point); + } @Override @Transactional public PostUseRes useMultipleTimeContainers(User user, PostUseReq postUseReq) { - Location location = locationJpaRepository.findByNameAndAddressAndState(postUseReq.getLocationName(), postUseReq.getLocationAddress(), ACTIVE) + Location location = locationJpaRepository.findByIdAndState(postUseReq.getLocationId(), ACTIVE) .orElseThrow(() -> new BaseException(NOT_FIND_LOCATION)); + List multiUseContainerIdList = locationContainerJpaRepository.findByLocation_Id(location.getId()) + .stream() + .map(LocationContainer::getMultiUseContainer) + .map(MultiUseContainer::getId) + .toList(); + if(!multiUseContainerIdList.contains(postUseReq.getMultiUseContainerId())) { + throw new BaseException(NOT_USE_LOCATION); + } Use use = UseConverter.toUse(user, location, postUseReq.getPoint(), postUseReq.getMultiUseContainerId()); useJpaRepository.save(use); useStatisticsJpaRepository.findById(user.getId()).orElseThrow(() -> new BaseException(NOT_FIND_USE_STATISTICS)).addTotalUseCount(); - return UseConverter.toPostUseRes(use); + return UseConverter.toPostUseRes(use, location); } @Override @@ -100,7 +121,7 @@ public PatchReturnRes returnMultipleTimeContainers(User user, PatchReturnReq pat List localDateTime = TimeConverter.toLocalDateTime(usetAt); Use use = useJpaRepository.findByUserIdAndUseAtBetweenAndStatus(user.getId(), localDateTime.get(0), localDateTime.get(1), USING) .orElseThrow(() -> new BaseException(NOT_FIND_USE)); - Location returnLocation = locationJpaRepository.findByNameAndAddressAndState(patchReturnReq.getLocationName(), patchReturnReq.getLocationAddress(), ACTIVE) + Location returnLocation = locationJpaRepository.findByIdAndState(patchReturnReq.getReturnLocationId(), ACTIVE) .orElseThrow(() -> new BaseException(NOT_FIND_LOCATION)); if(!(returnLocation.getLocationType().equals(LocationType.RETURN)) || !(returnLocation.isReturned())) { throw new BaseException(NOT_RETURN_LOCATION); @@ -118,6 +139,6 @@ public PatchReturnRes returnMultipleTimeContainers(User user, PatchReturnReq pat useStatisticsJpaRepository.findById(user.getId()) .orElseThrow(() -> new BaseException(NOT_FIND_USE_STATISTICS)) .addTotalReturnCount(); - return UseConverter.toPatchReturnRes(user, returnLocation, use); + return UseConverter.toPatchReturnRes(user, returnLocation, use, userPoint); } } diff --git a/src/main/java/carbonneutral/academy/common/code/status/ErrorStatus.java b/src/main/java/carbonneutral/academy/common/code/status/ErrorStatus.java index 2cb7556..9d35161 100644 --- a/src/main/java/carbonneutral/academy/common/code/status/ErrorStatus.java +++ b/src/main/java/carbonneutral/academy/common/code/status/ErrorStatus.java @@ -29,6 +29,7 @@ public enum ErrorStatus implements BaseErrorCode { NOT_FIND_LOCATION(HttpStatus.NOT_FOUND, "LOCATION4000", "존재하지 않는 위치입니다."), NOT_RETURN_LOCATION(HttpStatus.BAD_REQUEST, "LOCATION4001", "반납할 수 있는 위치가 아닙니다."), + NOT_USE_LOCATION(HttpStatus.BAD_REQUEST, "LOCATION4002", "이용할 수 있는 위치가 아닙니다."), //USE NOT_FIND_USE(HttpStatus.NOT_FOUND, "USE4000", "존재하지 않는 이용내역입니다."), diff --git a/src/main/java/carbonneutral/academy/common/code/status/SuccessStatus.java b/src/main/java/carbonneutral/academy/common/code/status/SuccessStatus.java index ba8d8d1..8efbc2a 100644 --- a/src/main/java/carbonneutral/academy/common/code/status/SuccessStatus.java +++ b/src/main/java/carbonneutral/academy/common/code/status/SuccessStatus.java @@ -25,7 +25,8 @@ public enum SuccessStatus implements BaseCode { IN_USES_OK(HttpStatus.OK, "USE2001", "이용중인 다회용기 조회 성공"), IN_USE_OK(HttpStatus.OK, "USE2002", "이용중인 다회용기 단일 조회 성공" ), RETURN_SAVE_OK(HttpStatus.CREATED, "USE2003", "반납 성공"), - GET_POINT_OK(HttpStatus.OK, "POINT2000", "포인트 조회 성공"); + GET_POINT_OK(HttpStatus.OK, "POINT2000", "포인트 조회 성공"), + LOCATION_OK(HttpStatus.OK, "LOCATION2000", "장소 조회 성공"); private final HttpStatus httpStatus; private final String code; diff --git a/src/main/java/carbonneutral/academy/domain/location/repository/LocationJpaRepository.java b/src/main/java/carbonneutral/academy/domain/location/repository/LocationJpaRepository.java index 4ea4b64..d9cb80c 100644 --- a/src/main/java/carbonneutral/academy/domain/location/repository/LocationJpaRepository.java +++ b/src/main/java/carbonneutral/academy/domain/location/repository/LocationJpaRepository.java @@ -10,6 +10,7 @@ public interface LocationJpaRepository extends JpaRepository{ + Optional findByIdAndState(int locationId, BaseEntity.State state); Optional findByNameAndAddressAndState (String name, String address, BaseEntity.State state); List findByIdInAndLocationType(List locationIds, LocationType locationType);