Skip to content

Commit

Permalink
refactor: #8 TimeSlot enum으로 변경 및 @transactional 추가
Browse files Browse the repository at this point in the history
- TimeSlot을 Enum type으로 변경
- update에서 @transactional 추가
  • Loading branch information
ss0ngcode committed Aug 5, 2024
1 parent 65789ac commit cb1aca8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import com.talkka.server.bus.dao.BusRouteEntity;
import com.talkka.server.bus.dao.BusRouteStationEntity;
import com.talkka.server.review.enums.TimeSlot;
import com.talkka.server.user.dao.UserEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -55,7 +57,8 @@ public class BusReviewEntity {
private String content;

@Column(name = "time_slot", nullable = false)
private Integer timeSlot;
@Convert(converter = TimeSlot.class)
private TimeSlot timeSlot;

@Column(name = "rating", nullable = false)
private Integer rating;
Expand Down Expand Up @@ -83,7 +86,7 @@ public int hashCode() {
return Objects.hashCode(busReviewId);
}

public void updateReview(String content, Integer timeSlot, Integer rating) {
public void updateReview(String content, TimeSlot timeSlot, Integer rating) {
this.content = content;
this.timeSlot = timeSlot;
this.rating = rating;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.talkka.server.bus.dao.BusRouteEntity;
import com.talkka.server.bus.dao.BusRouteStationEntity;
import com.talkka.server.review.dao.BusReviewEntity;
import com.talkka.server.review.enums.TimeSlot;
import com.talkka.server.user.dao.UserEntity;

import jakarta.validation.constraints.Max;
Expand Down Expand Up @@ -35,16 +36,16 @@ public class BusReviewReqDto {
private String content;

@NotNull
@Min(value = 0, message = "형식에 맞지 않은 timeSlot 입니다.")
@Max(value = 47, message = "형식에 맞지 않은 timeSlot 입니다.")
private Integer timeSlot;
@Size(min = 1, max = 2, message = "형식에 맞지 않은 timeSlot 입니다.")
private String timeSlot;

@NotNull
@Min(value = 1, message = "형식에 맞지 않는 rating 입니다.")
@Max(value = 10, message = "형식에 맞지 않는 rating 입니다.")
private Integer rating;

public BusReviewEntity toEntity(UserEntity user, BusRouteStationEntity station, BusRouteEntity route) {
return new BusReviewEntity(null, user, station, route, content, timeSlot, rating, null, null);
return new BusReviewEntity(null, user, station, route, content, TimeSlot.valueOf(timeSlot),
rating, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BusReviewRespDto {
private Long routeId;
private Long busRouteStationId;
private String content;
private Integer timeSlot;
private String timeSlot;
private Integer rating;

public static BusReviewRespDto of(BusReviewEntity busEntity) {
Expand All @@ -31,7 +31,7 @@ public static BusReviewRespDto of(BusReviewEntity busEntity) {
busEntity.getRoute().getRouteId(),
busEntity.getStation().getBusRouteStationId(),
busEntity.getContent(),
busEntity.getTimeSlot(),
busEntity.getTimeSlot().getCode(),
busEntity.getRating());
}
}
65 changes: 65 additions & 0 deletions server/src/main/java/com/talkka/server/review/enums/TimeSlot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.talkka.server.review.enums;

import com.talkka.server.common.util.EnumCodeInterface;

import lombok.Getter;

@Getter
public enum TimeSlot implements EnumCodeInterface {
T_00_00("00:00", "0"),
T_00_30("00:30", "1"),
T_01_00("01:00", "2"),
T_01_30("01:30", "3"),
T_02_00("02:00", "4"),
T_02_30("02:30", "5"),
T_03_00("03:00", "6"),
T_03_30("03:30", "7"),
T_04_00("04:00", "8"),
T_04_30("04:30", "9"),
T_05_00("05:00", "10"),
T_05_30("05:30", "11"),
T_06_00("06:00", "12"),
T_06_30("06:30", "13"),
T_07_00("07:00", "14"),
T_07_30("07:30", "15"),
T_08_00("08:00", "16"),
T_08_30("08:30", "17"),
T_09_00("09:00", "18"),
T_09_30("09:30", "19"),
T_10_00("10:00", "20"),
T_10_30("10:30", "21"),
T_11_00("11:00", "22"),
T_11_30("11:30", "23"),
T_12_00("12:00", "24"),
T_12_30("12:30", "25"),
T_13_00("13:00", "26"),
T_13_30("13:30", "27"),
T_14_00("14:00", "28"),
T_14_30("14:30", "29"),
T_15_00("15:00", "30"),
T_15_30("15:30", "31"),
T_16_00("16:00", "32"),
T_16_30("16:30", "33"),
T_17_00("17:00", "34"),
T_17_30("17:30", "35"),
T_18_00("18:00", "36"),
T_18_30("18:30", "37"),
T_19_00("19:00", "38"),
T_19_30("19:30", "39"),
T_20_00("20:00", "40"),
T_20_30("20:30", "41"),
T_21_00("21:00", "42"),
T_21_30("21:30", "43"),
T_22_00("22:00", "44"),
T_22_30("22:30", "45"),
T_23_00("23:00", "46"),
T_23_30("23:30", "47");

private final String timsSlot;
private final String code;

TimeSlot(String timsSlot, String code) {
this.timsSlot = timsSlot;
this.code = code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.talkka.server.bus.dao.BusRouteEntity;
import com.talkka.server.bus.dao.BusRouteRepository;
Expand All @@ -14,6 +15,7 @@
import com.talkka.server.review.dao.BusReviewRepository;
import com.talkka.server.review.dto.BusReviewReqDto;
import com.talkka.server.review.dto.BusReviewRespDto;
import com.talkka.server.review.enums.TimeSlot;
import com.talkka.server.user.dao.UserEntity;
import com.talkka.server.user.dao.UserRepository;

Expand Down Expand Up @@ -56,6 +58,7 @@ public BusReviewRespDto createBusReview(Long userId, BusReviewReqDto busReviewRe
return BusReviewRespDto.of(savedReview);
}

@Transactional
public BusReviewRespDto updateBusReview(Long userId, Long busReviewId, BusReviewReqDto busReviewReqDto) {
BusReviewEntity review = busReviewRepository.findById(busReviewId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 리뷰입니다."));
Expand All @@ -64,9 +67,10 @@ public BusReviewRespDto updateBusReview(Long userId, Long busReviewId, BusReview
throw new ForbiddenException("작성자와 일치하지 않는 ID입니다.");
}

review.updateReview(busReviewReqDto.getContent(), busReviewReqDto.getRating(), busReviewReqDto.getTimeSlot());
BusReviewEntity updatedReview = busReviewRepository.save(review);
return BusReviewRespDto.of(updatedReview);
review.updateReview(busReviewReqDto.getContent(), TimeSlot.valueOf(busReviewReqDto.getTimeSlot()),
busReviewReqDto.getRating());

return BusReviewRespDto.of(review);
}

public Long deleteBusReview(Long userId, Long busReviewId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.talkka.server.review.util;

import com.talkka.server.common.util.EnumCodeConverter;
import com.talkka.server.review.enums.TimeSlot;

public class TimeSlotConverter extends EnumCodeConverter<TimeSlot> {

public TimeSlotConverter() {
super(TimeSlot.class);
}
}

0 comments on commit cb1aca8

Please sign in to comment.