From 8eeeaea81f0cacb99c48beba35e0b4059914c469 Mon Sep 17 00:00:00 2001 From: junmo95 Date: Mon, 8 Jan 2024 10:48:27 +0900 Subject: [PATCH 1/6] =?UTF-8?q?[feat]=20=EC=97=AC=ED=96=89=ED=9B=84?= =?UTF-8?q?=EA=B8=B0,=20=EC=97=AC=ED=96=89=EC=8A=A4=EC=BC=80=EC=A5=B4=20?= =?UTF-8?q?=EC=97=94=ED=8B=B0=ED=8B=B0=20=EC=B6=94=EA=B0=80=20-=20TripReco?= =?UTF-8?q?rd=20=EC=97=94=ED=8B=B0=ED=8B=B0=20=EC=B6=94=EA=B0=80=20-=20Tri?= =?UTF-8?q?pRecordSchedule=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/triprecord/entity/TripRecord.java | 93 +++++++++++++++++++ .../triprecord/entity/TripRecordSchedule.java | 45 +++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecordSchedule.java diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java new file mode 100644 index 00000000..d2113805 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java @@ -0,0 +1,93 @@ +package com.haejwo.tripcometrue.domain.triprecord.entity; + +import com.haejwo.tripcometrue.domain.triprecord.dto.request.TripRecordRequestDto; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.PrePersist; +import jakarta.persistence.PreUpdate; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.List; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class TripRecord { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "trip_record_id") + private Long id; + + @Column(nullable = false) + private String title; + private String content; + + private ExpenseType expenseType; + + private String countries; + + private LocalDate tripStartDay; + private LocalDate tripEndDay; + + private Integer totalDays; + private Integer average_rating; + private Integer viewCount; + + + @OneToMany(mappedBy = "tripRecord", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + private List tripRecordSchedules; + + @Builder + public TripRecord(Long id, String title, String content, Integer average_rating, + ExpenseType expenseType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, + String countries, Integer viewCount) { + this.id = id; + this.title = title; + this.content = content; + this.average_rating = average_rating; + this.expenseType = expenseType; + this.tripStartDay = tripStartDay; + this.tripEndDay = tripEndDay; + this.totalDays = totalDays; + this.countries = countries; + this.viewCount = viewCount; + } + + // TODO: update 함수 + public void update(TripRecordRequestDto requestDto) { + this.title = requestDto.title(); + this.content = requestDto.content(); + this.expenseType = requestDto.expenseType(); + this.tripStartDay = requestDto.tripStartDay(); + this.tripEndDay = requestDto.tripEndDay(); + this.countries = requestDto.countries(); + } + + @PrePersist + public void prePersist() { + this.totalDays = calculateTotalDays(); + } + + @PreUpdate + public void preUpdate() { + this.totalDays = calculateTotalDays(); + } + + private int calculateTotalDays() { + if (this.tripStartDay == null || this.tripEndDay == null) { + return 0; + } + return (int) ChronoUnit.DAYS.between(this.tripStartDay, this.tripEndDay); + } +} diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecordSchedule.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecordSchedule.java new file mode 100644 index 00000000..5cd4c972 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecordSchedule.java @@ -0,0 +1,45 @@ +package com.haejwo.tripcometrue.domain.triprecord.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class TripRecordSchedule { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "trip_record_schedule_id") + private Long id; + + @Column(nullable = false) + private Integer dayNumber; + + @Column(nullable = false) + private Integer ordering; + + private String content; + + @ManyToOne + @JoinColumn(name = "tripRecord_id") + private TripRecord tripRecord; + + @Builder + public TripRecordSchedule(Long id, Integer dayNumber, Integer ordering, String content) { + this.id = id; + this.dayNumber = dayNumber; + this.ordering = ordering; + this.content = content; + } + + +} From a336fcb9cbeeaeab90f656a42644367973fb4b49 Mon Sep 17 00:00:00 2001 From: junmo95 Date: Mon, 8 Jan 2024 10:49:47 +0900 Subject: [PATCH 2/6] =?UTF-8?q?[feat]=20=EC=97=AC=ED=96=89=ED=9B=84?= =?UTF-8?q?=EA=B8=B0=20=EC=97=94=ED=8B=B0=ED=8B=B0=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?enum=20=ED=83=80=EC=9E=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/triprecord/entity/ExpenseType.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java new file mode 100644 index 00000000..b64a76e8 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java @@ -0,0 +1,26 @@ +package com.haejwo.tripcometrue.domain.triprecord.entity; + +import java.util.stream.Stream; + +public enum ExpenseType { + + BELOW_50(50), + BELOW_100(100), + BELOW_200(200), + BELOW_300(300), + ABOVE_300(Integer.MAX_VALUE); + + private int max; + + ExpenseType(int max) { + this.max = max; + } + + public static ExpenseType findByMax(int max) { + return Stream.of(ExpenseType.values()) + .filter(p -> p.max == max) + .findFirst() + .orElseThrow(); // TODO: ExpenseType 예외 추가 + } + +} From 762d733768393ec20470115d75228c4b960bd32f Mon Sep 17 00:00:00 2001 From: junmo95 Date: Mon, 8 Jan 2024 10:50:33 +0900 Subject: [PATCH 3/6] =?UTF-8?q?[feat]=20=EC=97=AC=ED=96=89=ED=9B=84?= =?UTF-8?q?=EA=B8=B0(trip=5Frecord)=20controller,=20dto=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20-=20=ED=95=B4=EB=8B=B9=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20=EB=82=B4=EC=9A=A9=20security=EC=97=90=20?= =?UTF-8?q?=EC=A0=9C=EC=99=B8=EB=90=98=EB=8F=84=EB=A1=9D=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TripRecordController.java | 83 +++++++++++++++++++ .../dto/request/TripRecordRequestDto.java | 40 +++++++++ .../dto/response/TripRecordResponseDto.java | 56 +++++++++++++ .../springsecurity/SpringSecurityConfig.java | 1 + 4 files changed, 180 insertions(+) create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/controller/TripRecordController.java create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/controller/TripRecordController.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/controller/TripRecordController.java new file mode 100644 index 00000000..455392bb --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/controller/TripRecordController.java @@ -0,0 +1,83 @@ +package com.haejwo.tripcometrue.domain.triprecord.controller; + +import com.haejwo.tripcometrue.domain.triprecord.dto.request.TripRecordRequestDto; +import com.haejwo.tripcometrue.domain.triprecord.dto.response.TripRecordResponseDto; +import com.haejwo.tripcometrue.domain.triprecord.service.TripRecordService; +import com.haejwo.tripcometrue.global.util.ResponseDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +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.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/v1/trip-record") +@RequiredArgsConstructor +public class TripRecordController { + + private final TripRecordService tripRecordService; + + @PostMapping + public ResponseEntity> tripRecordAdd( + @RequestBody TripRecordRequestDto requestDto + ) { + + TripRecordResponseDto responseDto = tripRecordService.addTripRecord(requestDto); + ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); + + return ResponseEntity + .status(responseBody.getCode()) + .body(responseBody); + } + + @GetMapping("/{tripRecordId}") + public ResponseEntity> tripRecordDetails( + @PathVariable Long tripRecordId + ) { + + TripRecordResponseDto responseDto = tripRecordService.findTripRecord(tripRecordId); + ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); + + return ResponseEntity + .status(responseBody.getCode()) + .body(responseBody); + } + + @PutMapping("/{tripRecordId}") + public ResponseEntity> tripRecordModify( + @PathVariable Long tripRecordId, + @RequestBody TripRecordRequestDto requestDto + ) { + + TripRecordResponseDto responseDto = tripRecordService.modifyTripRecord(tripRecordId, requestDto); + ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); + + return ResponseEntity + .status(responseBody.getCode()) + .body(responseBody); + + } + + @DeleteMapping("/{tripRecordId}") + public ResponseEntity tripRecordRemove( + @PathVariable Long tripRecordId + ) { + + tripRecordService.removeTripRecord(tripRecordId); + ResponseDTO responseBody = ResponseDTO.ok(); + + return ResponseEntity + .status(responseBody.getCode()) + .body(responseBody); + } + + + + + +} diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java new file mode 100644 index 00000000..f58e8661 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java @@ -0,0 +1,40 @@ +package com.haejwo.tripcometrue.domain.triprecord.dto.request; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseType; +import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord; +import java.time.LocalDate; +import lombok.Builder; + +public record TripRecordRequestDto( + String title, + String content, + ExpenseType expenseType, + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripStartDay, + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripEndDay, + String countries +) { + + @Builder + public TripRecordRequestDto(String title, String content, ExpenseType expenseType, + LocalDate tripStartDay, LocalDate tripEndDay, String countries) { + this.title = title; + this.content = content; + this.expenseType = expenseType; + this.tripStartDay = tripStartDay; + this.tripEndDay = tripEndDay; + this.countries = countries; + } + + public TripRecord toEntity() { + return TripRecord.builder() + .title(this.title) + .content(this.content) + .expenseType(this.expenseType) + .tripStartDay(this.tripStartDay) + .tripEndDay(this.tripEndDay) + .countries(this.countries) + .build(); + } + +} diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java new file mode 100644 index 00000000..d5d72440 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java @@ -0,0 +1,56 @@ +package com.haejwo.tripcometrue.domain.triprecord.dto.response; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseType; +import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord; +import java.time.LocalDate; +import lombok.Builder; + +public record TripRecordResponseDto( + Long id, + String title, + String content, + Integer average_rating, + ExpenseType expenseType, + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripStartDay, + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripEndDay, + Integer totalDays, + String countries, + Integer viewCount + +) { + + @Builder + public TripRecordResponseDto(Long id, String title, String content, Integer average_rating, + ExpenseType expenseType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, + String countries, Integer viewCount) { + this.id = id; + this.title = title; + this.content = content; + this.average_rating = average_rating; + this.expenseType = expenseType; + this.tripStartDay = tripStartDay; + this.tripEndDay = tripEndDay; + this.totalDays = totalDays; + this.countries = countries; + this.viewCount = viewCount; + } + + + public static TripRecordResponseDto fromEntity(TripRecord entity) { + return TripRecordResponseDto.builder() + .id(entity.getId()) + .title(entity.getTitle()) + .content(entity.getContent()) + .average_rating(entity.getAverage_rating()) + .expenseType(entity.getExpenseType()) + .tripStartDay(entity.getTripStartDay()) + .tripEndDay(entity.getTripEndDay()) + .totalDays(entity.getTotalDays()) + .countries(entity.getCountries()) + .viewCount(entity.getViewCount()) + .build(); + + } + +} diff --git a/src/main/java/com/haejwo/tripcometrue/global/springsecurity/SpringSecurityConfig.java b/src/main/java/com/haejwo/tripcometrue/global/springsecurity/SpringSecurityConfig.java index c97d87bd..19a863f5 100644 --- a/src/main/java/com/haejwo/tripcometrue/global/springsecurity/SpringSecurityConfig.java +++ b/src/main/java/com/haejwo/tripcometrue/global/springsecurity/SpringSecurityConfig.java @@ -70,6 +70,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http, .requestMatchers(new AntPathRequestMatcher("/v1/member/check-duplicated-email")).permitAll() .requestMatchers(new AntPathRequestMatcher("/v1/places/**")).permitAll() + .requestMatchers(new AntPathRequestMatcher("/v1/trip-record/**")).permitAll() .anyRequest().authenticated()); http.exceptionHandling(exceptionHandling -> { From 312171de9b048ba44f12b9cc7aed082bb21204cc Mon Sep 17 00:00:00 2001 From: junmo95 Date: Mon, 8 Jan 2024 10:51:16 +0900 Subject: [PATCH 4/6] =?UTF-8?q?[feat]=20=EC=97=AC=ED=96=89=ED=9B=84?= =?UTF-8?q?=EA=B8=B0(trip=5Frecord)=20service,=20repository=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/TripRecordRepository.java | 8 +++ .../triprecord/service/TripRecordService.java | 58 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/repository/TripRecordRepository.java create mode 100644 src/main/java/com/haejwo/tripcometrue/domain/triprecord/service/TripRecordService.java diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/repository/TripRecordRepository.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/repository/TripRecordRepository.java new file mode 100644 index 00000000..61051e67 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/repository/TripRecordRepository.java @@ -0,0 +1,8 @@ +package com.haejwo.tripcometrue.domain.triprecord.repository; + +import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TripRecordRepository extends JpaRepository { + +} diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/service/TripRecordService.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/service/TripRecordService.java new file mode 100644 index 00000000..b91a8904 --- /dev/null +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/service/TripRecordService.java @@ -0,0 +1,58 @@ +package com.haejwo.tripcometrue.domain.triprecord.service; + +import com.haejwo.tripcometrue.domain.triprecord.dto.request.TripRecordRequestDto; +import com.haejwo.tripcometrue.domain.triprecord.dto.response.TripRecordResponseDto; +import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord; +import com.haejwo.tripcometrue.domain.triprecord.repository.TripRecordRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class TripRecordService { + + private final TripRecordRepository tripRecordRepository; + + @Transactional + public TripRecordResponseDto addTripRecord(TripRecordRequestDto requestDto) { + + TripRecord requestTripRecord = requestDto.toEntity(); + TripRecord savedTripRecord = tripRecordRepository.save(requestTripRecord); + TripRecordResponseDto responseDto = TripRecordResponseDto.fromEntity(savedTripRecord); + + return responseDto; + } + + @Transactional(readOnly = true) + public TripRecordResponseDto findTripRecord(Long tripRecordId) { + + TripRecord findTripRecord = findTripRecordById(tripRecordId); + TripRecordResponseDto responseDto = TripRecordResponseDto.fromEntity(findTripRecord); + + return responseDto; + } + + @Transactional + public TripRecordResponseDto modifyTripRecord(Long tripRecordId, TripRecordRequestDto requestDto) { + + TripRecord findTripRecord = findTripRecordById(tripRecordId); + findTripRecord.update(requestDto); + TripRecordResponseDto responseDto = TripRecordResponseDto.fromEntity(findTripRecord); + + return responseDto; + } + + public void removeTripRecord(Long tripRecordId) { + TripRecord findTripRecord = findTripRecordById(tripRecordId); + tripRecordRepository.delete(findTripRecord); + } + + private TripRecord findTripRecordById(Long tripRecordId) { + TripRecord findTripRecord = tripRecordRepository.findById(tripRecordId) + .orElseThrow(); // TODO: 여행후기 조회 예외 추가 + return findTripRecord; + } + + +} From ba7106db173e4e828231f3ad8a59920c947af9f0 Mon Sep 17 00:00:00 2001 From: junmo95 Date: Mon, 8 Jan 2024 12:38:46 +0900 Subject: [PATCH 5/6] =?UTF-8?q?[refactor]=20=EC=97=AC=ED=96=89=ED=9B=84?= =?UTF-8?q?=EA=B8=B0(trip=5Frecord)=20=EC=88=98=EC=A0=95=EC=9A=94=EC=B2=AD?= =?UTF-8?q?=20=EB=B6=80=EB=B6=84=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/TripRecordResponseDto.java | 2 +- .../domain/triprecord/entity/TripRecord.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java index d5d72440..257196a8 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java @@ -42,7 +42,7 @@ public static TripRecordResponseDto fromEntity(TripRecord entity) { .id(entity.getId()) .title(entity.getTitle()) .content(entity.getContent()) - .average_rating(entity.getAverage_rating()) + .average_rating(entity.getAverageRating()) .expenseType(entity.getExpenseType()) .tripStartDay(entity.getTripStartDay()) .tripEndDay(entity.getTripEndDay()) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java index d2113805..02e42e0d 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java @@ -4,6 +4,8 @@ import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -13,6 +15,7 @@ import jakarta.persistence.PreUpdate; import java.time.LocalDate; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.List; import lombok.AccessLevel; import lombok.Builder; @@ -33,6 +36,7 @@ public class TripRecord { private String title; private String content; + @Enumerated(EnumType.STRING) private ExpenseType expenseType; private String countries; @@ -41,21 +45,21 @@ public class TripRecord { private LocalDate tripEndDay; private Integer totalDays; - private Integer average_rating; + private Integer averageRating; private Integer viewCount; @OneToMany(mappedBy = "tripRecord", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) - private List tripRecordSchedules; + private List tripRecordSchedules =new ArrayList<>(); @Builder - public TripRecord(Long id, String title, String content, Integer average_rating, + public TripRecord(Long id, String title, String content, Integer averageRating, ExpenseType expenseType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, String countries, Integer viewCount) { this.id = id; this.title = title; this.content = content; - this.average_rating = average_rating; + this.averageRating = averageRating; this.expenseType = expenseType; this.tripStartDay = tripStartDay; this.tripEndDay = tripEndDay; From 516c7c6aacabfc50d9f19a73e0cd8e642cae21a9 Mon Sep 17 00:00:00 2001 From: junmo95 Date: Mon, 8 Jan 2024 14:47:01 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[refactor]=20=EC=97=AC=ED=96=89=ED=9B=84?= =?UTF-8?q?=EA=B8=B0(trip=5Frecord),=20=EC=97=AC=ED=96=89=EC=A7=80(place)?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=EC=9A=94=EC=B2=AD=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../place/controller/PlaceController.java | 30 +++++++++---------- ...estDTO.java => PlaceFilterRequestDto.java} | 4 +-- ...ceRequestDTO.java => PlaceRequestDto.java} | 4 +-- ...ResponseDTO.java => PlaceResponseDto.java} | 8 ++--- .../domain/place/entity/Place.java | 4 +-- .../domain/place/service/PlaceService.java | 22 +++++++------- .../dto/request/TripRecordRequestDto.java | 10 +++---- .../dto/response/TripRecordResponseDto.java | 10 +++---- ...ExpenseType.java => ExpenseRangeType.java} | 10 +++---- .../domain/triprecord/entity/TripRecord.java | 8 ++--- 10 files changed, 54 insertions(+), 56 deletions(-) rename src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/{PlaceFilterRequestDTO.java => PlaceFilterRequestDto.java} (81%) rename src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/{PlaceRequestDTO.java => PlaceRequestDto.java} (96%) rename src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/{PlaceResponseDTO.java => PlaceResponseDto.java} (92%) rename src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/{ExpenseType.java => ExpenseRangeType.java} (66%) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/place/controller/PlaceController.java b/src/main/java/com/haejwo/tripcometrue/domain/place/controller/PlaceController.java index 1f963443..73c602a8 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/place/controller/PlaceController.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/place/controller/PlaceController.java @@ -1,7 +1,7 @@ package com.haejwo.tripcometrue.domain.place.controller; -import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDTO; -import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDTO; +import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto; +import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDto; import com.haejwo.tripcometrue.domain.place.service.PlaceService; import com.haejwo.tripcometrue.global.util.ResponseDTO; import lombok.RequiredArgsConstructor; @@ -26,12 +26,12 @@ public class PlaceController { private final PlaceService placeService; @PostMapping - public ResponseEntity> placeAdd( - @RequestBody PlaceRequestDTO requestDto + public ResponseEntity> placeAdd( + @RequestBody PlaceRequestDto requestDto ) { - PlaceResponseDTO responseDto = placeService.addPlace(requestDto); - ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); + PlaceResponseDto responseDto = placeService.addPlace(requestDto); + ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); return ResponseEntity .status(responseBody.getCode()) @@ -39,12 +39,12 @@ public ResponseEntity> placeAdd( } @GetMapping("/{placeId}") - public ResponseEntity> placeDetails( + public ResponseEntity> placeDetails( @PathVariable Long placeId ) { - PlaceResponseDTO responseDto = placeService.findPlace(placeId); - ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); + PlaceResponseDto responseDto = placeService.findPlace(placeId); + ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); return ResponseEntity .status(responseBody.getCode()) @@ -52,12 +52,12 @@ public ResponseEntity> placeDetails( } @GetMapping - public ResponseEntity>> placeList( + public ResponseEntity>> placeList( Pageable pageable, @RequestParam Integer storedCount ) { - Page placePage = placeService.findPlaces(pageable, storedCount); + Page placePage = placeService.findPlaces(pageable, storedCount); ResponseDTO responseBody = ResponseDTO.okWithData(placePage); @@ -67,13 +67,13 @@ public ResponseEntity>> placeList( } @PutMapping("/{placeId}") - public ResponseEntity> placeModify( + public ResponseEntity> placeModify( @PathVariable Long placeId, - @RequestBody PlaceRequestDTO requestDto + @RequestBody PlaceRequestDto requestDto ) { - PlaceResponseDTO responseDto = placeService.modifyPlace(placeId, requestDto); - ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); + PlaceResponseDto responseDto = placeService.modifyPlace(placeId, requestDto); + ResponseDTO responseBody = ResponseDTO.okWithData(responseDto); return ResponseEntity .status(responseBody.getCode()) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceFilterRequestDTO.java b/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceFilterRequestDto.java similarity index 81% rename from src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceFilterRequestDTO.java rename to src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceFilterRequestDto.java index d8bd5f1c..adbafccf 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceFilterRequestDTO.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceFilterRequestDto.java @@ -1,12 +1,12 @@ package com.haejwo.tripcometrue.domain.place.dto.request; -public record PlaceFilterRequestDTO( +public record PlaceFilterRequestDto( Integer stored_count, Integer storedCount ) { // record는 Compact Constructor라는 기능있어, 생성자 내부의 변수에 대한 로직이 마지막으로 동작하여 변수 초기화를 한다. - public PlaceFilterRequestDTO { + public PlaceFilterRequestDto { storedCount = stored_count; } diff --git a/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceRequestDTO.java b/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceRequestDto.java similarity index 96% rename from src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceRequestDTO.java rename to src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceRequestDto.java index 38b7b89b..32fefb63 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceRequestDTO.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/place/dto/request/PlaceRequestDto.java @@ -5,7 +5,7 @@ import java.time.LocalTime; import lombok.Builder; -public record PlaceRequestDTO( +public record PlaceRequestDto( String name, String address, String description, @@ -18,7 +18,7 @@ public record PlaceRequestDTO( ) { @Builder - public PlaceRequestDTO( + public PlaceRequestDto( String name, String address, String description, diff --git a/src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/PlaceResponseDTO.java b/src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/PlaceResponseDto.java similarity index 92% rename from src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/PlaceResponseDTO.java rename to src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/PlaceResponseDto.java index c631e866..7a9b41a5 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/PlaceResponseDTO.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/place/dto/response/PlaceResponseDto.java @@ -4,7 +4,7 @@ import com.haejwo.tripcometrue.domain.place.entity.Place; import java.time.LocalTime; import lombok.Builder; -public record PlaceResponseDTO( +public record PlaceResponseDto( Long id, String name, String address, @@ -18,7 +18,7 @@ public record PlaceResponseDTO( ) { @Builder - public PlaceResponseDTO( + public PlaceResponseDto( Long id, String name, String address, @@ -42,8 +42,8 @@ public PlaceResponseDTO( this.cityId = cityId; } - public static PlaceResponseDTO fromEntity(Place entity) { - return PlaceResponseDTO.builder() + public static PlaceResponseDto fromEntity(Place entity) { + return PlaceResponseDto.builder() .id(entity.getId()) .name(entity.getName()) .address(entity.getAddress()) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/place/entity/Place.java b/src/main/java/com/haejwo/tripcometrue/domain/place/entity/Place.java index 20deb3dc..231f4d18 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/place/entity/Place.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/place/entity/Place.java @@ -1,6 +1,6 @@ package com.haejwo.tripcometrue.domain.place.entity; -import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDTO; +import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto; import com.haejwo.tripcometrue.global.entity.BaseTimeEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -60,7 +60,7 @@ public Place( this.cityId = cityId; } - public void update(PlaceRequestDTO requestDto) { + public void update(PlaceRequestDto requestDto) { this.name = requestDto.name(); this.address = requestDto.address(); this.description = requestDto.description(); diff --git a/src/main/java/com/haejwo/tripcometrue/domain/place/service/PlaceService.java b/src/main/java/com/haejwo/tripcometrue/domain/place/service/PlaceService.java index 528ee1cd..fb5f992a 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/place/service/PlaceService.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/place/service/PlaceService.java @@ -1,7 +1,7 @@ package com.haejwo.tripcometrue.domain.place.service; -import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDTO; -import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDTO; +import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto; +import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDto; import com.haejwo.tripcometrue.domain.place.entity.Place; import com.haejwo.tripcometrue.domain.place.exception.PlaceNotFoundException; import com.haejwo.tripcometrue.domain.place.repositroy.PlaceRepository; @@ -20,45 +20,43 @@ public class PlaceService { private final PlaceRepository placeRepository; @Transactional - public PlaceResponseDTO addPlace(PlaceRequestDTO requestDto) { + public PlaceResponseDto addPlace(PlaceRequestDto requestDto) { - System.out.println(requestDto.storedCount()); Place requestPlace = requestDto.toEntity(); Place savedPlace = placeRepository.save(requestPlace); - System.out.println(savedPlace.getStoredCount()); - PlaceResponseDTO responseDto = PlaceResponseDTO.fromEntity(savedPlace); + PlaceResponseDto responseDto = PlaceResponseDto.fromEntity(savedPlace); return responseDto; } @Transactional(readOnly = true) - public PlaceResponseDTO findPlace(Long placeId) { + public PlaceResponseDto findPlace(Long placeId) { Place findPlace = findPlaceById(placeId); - PlaceResponseDTO responseDto = PlaceResponseDTO.fromEntity(findPlace); + PlaceResponseDto responseDto = PlaceResponseDto.fromEntity(findPlace); return responseDto; } @Transactional(readOnly = true) - public Page findPlaces(Pageable pageable, Integer storedCount) { + public Page findPlaces(Pageable pageable, Integer storedCount) { Page findPlaces = placeRepository.findPlaceWithFilter(pageable, storedCount); - Page result = findPlaces.map(PlaceResponseDTO::fromEntity); + Page result = findPlaces.map(PlaceResponseDto::fromEntity); return result; } @Transactional - public PlaceResponseDTO modifyPlace(Long placeId, PlaceRequestDTO requestDto) { + public PlaceResponseDto modifyPlace(Long placeId, PlaceRequestDto requestDto) { Place place = findPlaceById(placeId); place.update(requestDto); - PlaceResponseDTO responseDto = PlaceResponseDTO.fromEntity(place); + PlaceResponseDto responseDto = PlaceResponseDto.fromEntity(place); return responseDto; } diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java index f58e8661..61eeabf7 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/request/TripRecordRequestDto.java @@ -1,7 +1,7 @@ package com.haejwo.tripcometrue.domain.triprecord.dto.request; import com.fasterxml.jackson.annotation.JsonFormat; -import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseType; +import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseRangeType; import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord; import java.time.LocalDate; import lombok.Builder; @@ -9,18 +9,18 @@ public record TripRecordRequestDto( String title, String content, - ExpenseType expenseType, + ExpenseRangeType expenseRangeType, @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripStartDay, @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripEndDay, String countries ) { @Builder - public TripRecordRequestDto(String title, String content, ExpenseType expenseType, + public TripRecordRequestDto(String title, String content, ExpenseRangeType expenseRangeType, LocalDate tripStartDay, LocalDate tripEndDay, String countries) { this.title = title; this.content = content; - this.expenseType = expenseType; + this.expenseRangeType = expenseRangeType; this.tripStartDay = tripStartDay; this.tripEndDay = tripEndDay; this.countries = countries; @@ -30,7 +30,7 @@ public TripRecord toEntity() { return TripRecord.builder() .title(this.title) .content(this.content) - .expenseType(this.expenseType) + .expenseRangeType(this.expenseRangeType) .tripStartDay(this.tripStartDay) .tripEndDay(this.tripEndDay) .countries(this.countries) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java index 257196a8..7467820e 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/dto/response/TripRecordResponseDto.java @@ -1,7 +1,7 @@ package com.haejwo.tripcometrue.domain.triprecord.dto.response; import com.fasterxml.jackson.annotation.JsonFormat; -import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseType; +import com.haejwo.tripcometrue.domain.triprecord.entity.ExpenseRangeType; import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord; import java.time.LocalDate; import lombok.Builder; @@ -11,7 +11,7 @@ public record TripRecordResponseDto( String title, String content, Integer average_rating, - ExpenseType expenseType, + ExpenseRangeType expenseRangeType, @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripStartDay, @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate tripEndDay, Integer totalDays, @@ -22,13 +22,13 @@ public record TripRecordResponseDto( @Builder public TripRecordResponseDto(Long id, String title, String content, Integer average_rating, - ExpenseType expenseType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, + ExpenseRangeType expenseRangeType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, String countries, Integer viewCount) { this.id = id; this.title = title; this.content = content; this.average_rating = average_rating; - this.expenseType = expenseType; + this.expenseRangeType = expenseRangeType; this.tripStartDay = tripStartDay; this.tripEndDay = tripEndDay; this.totalDays = totalDays; @@ -43,7 +43,7 @@ public static TripRecordResponseDto fromEntity(TripRecord entity) { .title(entity.getTitle()) .content(entity.getContent()) .average_rating(entity.getAverageRating()) - .expenseType(entity.getExpenseType()) + .expenseRangeType(entity.getExpenseRangeType()) .tripStartDay(entity.getTripStartDay()) .tripEndDay(entity.getTripEndDay()) .totalDays(entity.getTotalDays()) diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseRangeType.java similarity index 66% rename from src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java rename to src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseRangeType.java index b64a76e8..20f9da53 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseType.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/ExpenseRangeType.java @@ -2,7 +2,7 @@ import java.util.stream.Stream; -public enum ExpenseType { +public enum ExpenseRangeType { BELOW_50(50), BELOW_100(100), @@ -10,14 +10,14 @@ public enum ExpenseType { BELOW_300(300), ABOVE_300(Integer.MAX_VALUE); - private int max; + private final int max; - ExpenseType(int max) { + ExpenseRangeType(int max) { this.max = max; } - public static ExpenseType findByMax(int max) { - return Stream.of(ExpenseType.values()) + public static ExpenseRangeType findByMax(int max) { + return Stream.of(ExpenseRangeType.values()) .filter(p -> p.max == max) .findFirst() .orElseThrow(); // TODO: ExpenseType 예외 추가 diff --git a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java index 02e42e0d..3d2a88cc 100644 --- a/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java +++ b/src/main/java/com/haejwo/tripcometrue/domain/triprecord/entity/TripRecord.java @@ -37,7 +37,7 @@ public class TripRecord { private String content; @Enumerated(EnumType.STRING) - private ExpenseType expenseType; + private ExpenseRangeType expenseRangeType; private String countries; @@ -54,13 +54,13 @@ public class TripRecord { @Builder public TripRecord(Long id, String title, String content, Integer averageRating, - ExpenseType expenseType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, + ExpenseRangeType expenseRangeType, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays, String countries, Integer viewCount) { this.id = id; this.title = title; this.content = content; this.averageRating = averageRating; - this.expenseType = expenseType; + this.expenseRangeType = expenseRangeType; this.tripStartDay = tripStartDay; this.tripEndDay = tripEndDay; this.totalDays = totalDays; @@ -72,7 +72,7 @@ public TripRecord(Long id, String title, String content, Integer averageRating, public void update(TripRecordRequestDto requestDto) { this.title = requestDto.title(); this.content = requestDto.content(); - this.expenseType = requestDto.expenseType(); + this.expenseRangeType = requestDto.expenseRangeType(); this.tripStartDay = requestDto.tripStartDay(); this.tripEndDay = requestDto.tripEndDay(); this.countries = requestDto.countries();