Skip to content

Commit

Permalink
Merge pull request #45 from Gyuhyeok99/refactor/#44
Browse files Browse the repository at this point in the history
[refactor/#44] 다회용기 이용 api 수정
  • Loading branch information
Gyuhyeok99 authored May 14, 2024
2 parents 2be30d8 + 68f6915 commit 7c1cf75
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +40,12 @@ BaseResponse<GetUseDetailRes> getInUseMultipleTimeContainer(@AuthenticationPrinc
return BaseResponse.of(IN_USE_OK, useService.getInUseMultipleTimeContainer(user, useAt));
}

@GetMapping("/location")
@Operation(summary = "QR 이용 시 해당 장소 조회 API",description = "QR 인증을 통해 해당 장소를 조회합니다.")
BaseResponse<GetLocationRes> 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<PostUseRes> useMultipleTimeContainers(@AuthenticationPrincipal User user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
@Builder
@AllArgsConstructor
public class PatchReturnReq {
private String locationName;
private String locationAddress;

private int returnLocationId;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,11 +11,7 @@
@AllArgsConstructor
public class PostUseReq {

private String locationName;

private String locationAddress;

private int locationId;
private int point;

private int multiUseContainerId;
}
Original file line number Diff line number Diff line change
@@ -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<Integer> multiUseContainerIdList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class GetUseDetailRes {
private String locationAddress;
private String useAt;
private int point;
private String multiUseContainer;
private int multiUseContainerId;
private List<GetReturnRes> getReturnResList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public class GetUseRes {
private String locationName;
private String useAt;
private UseStatus status;
private String multiUseContainerType;
private int multiUseContainerId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.math.BigDecimal;

@Getter
@NoArgsConstructor
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -26,7 +24,7 @@ public static List<LocalDateTime> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}

Expand All @@ -75,26 +80,42 @@ public static GetReturnRes toGetReturnRes(Location returnLocation) {
.build();
}

public static GetUseDetailRes toGetUseDetailRes(Use use, Location location, List<GetReturnRes> getReturnResList, String multiUseContainer) {
public static GetUseDetailRes toGetUseDetailRes(Use use, Location location, List<GetReturnRes> getReturnResList, int multiUseContainerId) {
return GetUseDetailRes.builder()
.rentalLocationId(location.getId())
.locationImageUrl(location.getImageUrl())
.locationName(location.getName())
.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<Integer> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,39 @@ public GetUseDetailRes getInUseMultipleTimeContainer(User user, String useAt) {
.toList();
List<GetReturnRes> 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<Integer> 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<Integer> 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
Expand All @@ -100,7 +121,7 @@ public PatchReturnRes returnMultipleTimeContainers(User user, PatchReturnReq pat
List<LocalDateTime> 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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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", "존재하지 않는 이용내역입니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public interface LocationJpaRepository extends JpaRepository<Location, Integer>{

Optional<Location> findByIdAndState(int locationId, BaseEntity.State state);
Optional<Location> findByNameAndAddressAndState (String name, String address, BaseEntity.State state);

List<Location> findByIdInAndLocationType(List<Integer> locationIds, LocationType locationType);
Expand Down

0 comments on commit 7c1cf75

Please sign in to comment.