From 193d9a7c2e3411377a3f08bb0a5af9815d9bce6d Mon Sep 17 00:00:00 2001 From: Jang99u Date: Tue, 7 May 2024 22:17:59 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=84=B0=EB=A7=81=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BoardingRecordController.java | 5 ++++ .../java/ice/spot/domain/BoardingRecord.java | 14 +++++++-- src/main/java/ice/spot/domain/ParkingLot.java | 7 +++-- src/main/java/ice/spot/domain/User.java | 4 +++ .../response/BoardingRecordListResponse.java | 14 +++++++++ .../response/BoardingRecordResponse.java | 9 ++---- .../dto/user/response/PersonResponse.java | 11 +++++++ .../spot/service/BoardingRecordService.java | 29 +++++++++++++++++-- 8 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordListResponse.java create mode 100644 src/main/java/ice/spot/dto/user/response/PersonResponse.java diff --git a/src/main/java/ice/spot/controller/BoardingRecordController.java b/src/main/java/ice/spot/controller/BoardingRecordController.java index 82c107a..28bfc37 100644 --- a/src/main/java/ice/spot/controller/BoardingRecordController.java +++ b/src/main/java/ice/spot/controller/BoardingRecordController.java @@ -32,4 +32,9 @@ public ResponseDto saveBoardingRecord ( return ResponseDto.created(boardingRecordService .saveBoardingRecord(userId, imageId, boardingRecordRequest)); } + + @GetMapping("/boarding-record") + public ResponseDto getBoardingRecord (@UserId Long userId) { + return ResponseDto.ok(boardingRecordService.boardingRecordList(userId)); + } } diff --git a/src/main/java/ice/spot/domain/BoardingRecord.java b/src/main/java/ice/spot/domain/BoardingRecord.java index 10346e0..33abcd8 100644 --- a/src/main/java/ice/spot/domain/BoardingRecord.java +++ b/src/main/java/ice/spot/domain/BoardingRecord.java @@ -6,6 +6,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; + @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -22,11 +24,17 @@ public class BoardingRecord { @Column(name = "time") private Integer time; + @Column(name = "point") + private Integer point; + + @Column(name = "createdAt") + private LocalDateTime createdAt; + @ManyToOne @JoinColumn(name = "user_id") private User user; - @OneToOne + @ManyToOne @JoinColumn(name = "parking_lot_id") private ParkingLot parkingLot; @@ -35,9 +43,11 @@ public class BoardingRecord { private Image image; @Builder - public BoardingRecord(Double distance, Integer time, User user, ParkingLot parkingLot, Image image) { + public BoardingRecord(Double distance, Integer time, Integer point, User user, ParkingLot parkingLot, Image image) { this.distance = distance; this.time = time; + this.point = point; + this.createdAt = LocalDateTime.now(); this.user = user; this.parkingLot = parkingLot; this.image = image; diff --git a/src/main/java/ice/spot/domain/ParkingLot.java b/src/main/java/ice/spot/domain/ParkingLot.java index 574011c..0bde2cc 100644 --- a/src/main/java/ice/spot/domain/ParkingLot.java +++ b/src/main/java/ice/spot/domain/ParkingLot.java @@ -5,6 +5,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.ArrayList; +import java.util.List; + @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -27,6 +30,6 @@ public class ParkingLot { @Column(name = "detail_address") private String detailAddress; - @OneToOne(mappedBy = "parkingLot") - private BoardingRecord boardingRecord; + @OneToMany(mappedBy = "parkingLot") + private List boardingRecords = new ArrayList<>(); } diff --git a/src/main/java/ice/spot/domain/User.java b/src/main/java/ice/spot/domain/User.java index 9e32a50..203fb77 100644 --- a/src/main/java/ice/spot/domain/User.java +++ b/src/main/java/ice/spot/domain/User.java @@ -74,4 +74,8 @@ public void register(String nickname) { public void updateRefreshToken(String refreshToken) { this.refreshToken = refreshToken; } + + public void plusPoint() { + this.point += 100L; + } } diff --git a/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordListResponse.java b/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordListResponse.java new file mode 100644 index 0000000..f6bf320 --- /dev/null +++ b/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordListResponse.java @@ -0,0 +1,14 @@ +package ice.spot.dto.boardingrecord.response; + +import ice.spot.dto.user.response.PersonResponse; +import lombok.Builder; + +import java.util.List; + +@Builder +public record BoardingRecordListResponse( + PersonResponse personResponse, + + List boardingRecordResponseList +) { +} diff --git a/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordResponse.java b/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordResponse.java index e816e47..08eea96 100644 --- a/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordResponse.java +++ b/src/main/java/ice/spot/dto/boardingrecord/response/BoardingRecordResponse.java @@ -18,12 +18,9 @@ public record BoardingRecordResponse( Integer time, @JsonProperty("point") - Integer point, - - @JsonProperty("is_parking_lot") - Boolean isParkingLot + Integer point ) { - public static BoardingRecordResponse of(String createdAt, String image, Double distance, Integer time, Integer point, Boolean isParkingLot) { - return new BoardingRecordResponse(createdAt, image, distance, time, point, isParkingLot); + public static BoardingRecordResponse of(String createdAt, String image, Double distance, Integer time, Integer point) { + return new BoardingRecordResponse(createdAt, image, distance, time, point); } } diff --git a/src/main/java/ice/spot/dto/user/response/PersonResponse.java b/src/main/java/ice/spot/dto/user/response/PersonResponse.java new file mode 100644 index 0000000..72ac830 --- /dev/null +++ b/src/main/java/ice/spot/dto/user/response/PersonResponse.java @@ -0,0 +1,11 @@ +package ice.spot.dto.user.response; + +import lombok.Builder; + +@Builder +public record PersonResponse( + String nickname, + Integer recordCount, + Long point +) { +} diff --git a/src/main/java/ice/spot/service/BoardingRecordService.java b/src/main/java/ice/spot/service/BoardingRecordService.java index 268f65f..325f31e 100644 --- a/src/main/java/ice/spot/service/BoardingRecordService.java +++ b/src/main/java/ice/spot/service/BoardingRecordService.java @@ -5,7 +5,9 @@ import ice.spot.domain.ParkingLot; import ice.spot.domain.User; import ice.spot.dto.boardingrecord.request.BoardingRecordRequest; +import ice.spot.dto.boardingrecord.response.BoardingRecordListResponse; import ice.spot.dto.boardingrecord.response.BoardingRecordResponse; +import ice.spot.dto.user.response.PersonResponse; import ice.spot.exception.CommonException; import ice.spot.exception.ErrorCode; import ice.spot.repository.BoardingRecordRepository; @@ -43,16 +45,39 @@ public Boolean saveBoardingRecord(Long userId, Long imageId, BoardingRecordReque boardingRecordRepository.save(BoardingRecord.builder() .distance(boardingRecordRequest.distance()) .time(boardingRecordRequest.time()) + .point(100) .user(user) .image(image) .parkingLot(parkingLot) .build()); + user.plusPoint(); + return Boolean.TRUE; } @Transactional(readOnly = true) - public List boardingRecordList(Long userId) { - return null; + public BoardingRecordListResponse boardingRecordList(Long userId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_USER)); + + BoardingRecordListResponse boardingRecordListResponse = BoardingRecordListResponse.builder() + .personResponse(PersonResponse.builder() + .nickname(user.getNickname()) + .recordCount(user.getBoardingRecords().size()) + .point(user.getPoint()) + .build()) + .boardingRecordResponseList(user.getBoardingRecords().stream() + .map(boardingRecord -> + BoardingRecordResponse.builder() + .createdAt(boardingRecord.getCreatedAt().toString()) + .image(boardingRecord.getImage().getImageUrl()) + .distance(boardingRecord.getDistance()) + .time(boardingRecord.getTime()) + .point(boardingRecord.getPoint()) + .build()) + .toList()) + .build(); + return boardingRecordListResponse; } }