diff --git a/build.gradle b/build.gradle index cf697c5..15089fd 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' implementation 'com.fasterxml.jackson.core:jackson-core:2.13.3' - compileOnly 'org.projectlombok:lombok' + implementation 'org.springframework.boot:spring-boot-starter-validation:2.7.3' + compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' runtimeOnly 'mysql:mysql-connector-java' diff --git a/src/main/java/com/nadoyagsa/pillaroid/controller/UserController.java b/src/main/java/com/nadoyagsa/pillaroid/controller/UserController.java index 48b71cc..ec90b8c 100644 --- a/src/main/java/com/nadoyagsa/pillaroid/controller/UserController.java +++ b/src/main/java/com/nadoyagsa/pillaroid/controller/UserController.java @@ -5,11 +5,15 @@ import com.nadoyagsa.pillaroid.common.exception.InternalServerException; import com.nadoyagsa.pillaroid.common.exception.NotFoundException; import com.nadoyagsa.pillaroid.common.exception.UnauthorizedException; +import com.nadoyagsa.pillaroid.dto.AlarmTimeDto; +import com.nadoyagsa.pillaroid.dto.AlarmTimeResponse; import com.nadoyagsa.pillaroid.dto.FavoritesDTO; import com.nadoyagsa.pillaroid.dto.FavoritesResponse; +import com.nadoyagsa.pillaroid.entity.AlarmTime; import com.nadoyagsa.pillaroid.entity.Favorites; import com.nadoyagsa.pillaroid.entity.User; import com.nadoyagsa.pillaroid.jwt.AuthTokenProvider; +import com.nadoyagsa.pillaroid.service.AlarmTimeService; import com.nadoyagsa.pillaroid.service.FavoritesService; import com.nadoyagsa.pillaroid.service.UserService; import org.springframework.beans.factory.annotation.Autowired; @@ -17,6 +21,8 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; + import java.util.List; import java.util.Optional; @@ -26,12 +32,15 @@ public class UserController { private final AuthTokenProvider authTokenProvider; private final UserService userService; private final FavoritesService favoritesService; + private final AlarmTimeService alarmTimeService; @Autowired - public UserController(AuthTokenProvider authTokenProvider, UserService userService, FavoritesService favoritesService) { + public UserController(AuthTokenProvider authTokenProvider, UserService userService, + FavoritesService favoritesService, AlarmTimeService alarmTimeService) { this.authTokenProvider = authTokenProvider; this.userService = userService; this.favoritesService = favoritesService; + this.alarmTimeService = alarmTimeService; } // 사용자의 의약품 번호에 해당하는 즐겨찾기 여부 조회 @@ -90,7 +99,7 @@ public ApiResponse postUserFavorites(HttpServletRequest reque // 즐겨찾기 삭제 @DeleteMapping(value = "/favorites/{fid}") - public ApiResponse deleteUserFavorites(HttpServletRequest request, @PathVariable("fid") Long favoritesIdx) throws IllegalStateException { + public ApiResponse deleteUserFavorites(HttpServletRequest request, @PathVariable("fid") Long favoritesIdx) throws IllegalStateException { Optional favorites = favoritesService.findFavoritesByIdx(favoritesIdx); if (favorites.isPresent()) { @@ -130,6 +139,33 @@ public ApiResponse> getUserFavorites(HttpServletRequest throw UnauthorizedException.UNAUTHORIZED_USER; } + // 사용자의 복용 시간대 조회 + @GetMapping("/alarmtime") + public ApiResponse getUserAlarmTime(HttpServletRequest request) { + User user = findUserByToken(request).orElseThrow( + () -> UnauthorizedException.UNAUTHORIZED_USER + ); + + Optional optAlarmTime = alarmTimeService.findAlarmTimeByUserIdx(user.getUserIdx()); + AlarmTime alarmTime = optAlarmTime.orElseThrow( + () -> NotFoundException.DATA_NOT_FOUND + ); + + return ApiResponse.success(alarmTime.toAlarmTimeResponse()); + } + + // 사용자의 복용 시간대 추가 및 수정 + @PostMapping("/alarmtime") + public ApiResponse saveUserAlarmTime(HttpServletRequest request, @RequestBody @Valid AlarmTimeDto alarmTimeDto) { + User user = findUserByToken(request).orElseThrow( + () -> UnauthorizedException.UNAUTHORIZED_USER + ); + + AlarmTime alarmTime = alarmTimeService.saveAlarmTime(user, alarmTimeDto); + + return ApiResponse.success(alarmTime.toAlarmTimeResponse()); + } + // 사용자 jwt 토큰으로부터 회원 정보 조회 public Optional findUserByToken(HttpServletRequest request) { try { diff --git a/src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeDto.java b/src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeDto.java new file mode 100644 index 0000000..247bb25 --- /dev/null +++ b/src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeDto.java @@ -0,0 +1,32 @@ +package com.nadoyagsa.pillaroid.dto; + +import java.time.LocalTime; + +import javax.validation.constraints.NotNull; + +import com.nadoyagsa.pillaroid.entity.AlarmTime; +import com.nadoyagsa.pillaroid.entity.User; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class AlarmTimeDto { + @NotNull + private LocalTime morning; + @NotNull + private LocalTime lunch; + @NotNull + private LocalTime dinner; + + public AlarmTime toEntity(User user) { + return AlarmTime.builder() + .user(user) + .morning(this.morning) + .lunch(this.lunch) + .dinner(this.dinner) + .build(); + + } +} diff --git a/src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeResponse.java b/src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeResponse.java new file mode 100644 index 0000000..882514e --- /dev/null +++ b/src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeResponse.java @@ -0,0 +1,17 @@ +package com.nadoyagsa.pillaroid.dto; + +import java.time.LocalTime; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@AllArgsConstructor +@Builder +public class AlarmTimeResponse { + private long userIdx; // 사용자 번호 + private LocalTime morning; // 아침 식사 시간 + private LocalTime lunch; // 점심 식사 시간 + private LocalTime dinner; // 저녁 식사 시간 +} diff --git a/src/main/java/com/nadoyagsa/pillaroid/entity/AlarmTime.java b/src/main/java/com/nadoyagsa/pillaroid/entity/AlarmTime.java new file mode 100644 index 0000000..079b39a --- /dev/null +++ b/src/main/java/com/nadoyagsa/pillaroid/entity/AlarmTime.java @@ -0,0 +1,61 @@ +package com.nadoyagsa.pillaroid.entity; + +import java.time.LocalTime; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import com.nadoyagsa.pillaroid.dto.AlarmTimeDto; +import com.nadoyagsa.pillaroid.dto.AlarmTimeResponse; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Builder +@Entity +@Table(name = "alarm_time") +public class AlarmTime { + @Id + @Column(name = "user_idx") + private Long userIdx; + + @OneToOne + @MapsId + @JoinColumn(name = "user_idx", referencedColumnName = "user_idx") + private User user; + + @Column(nullable = false) + private LocalTime morning; + + @Column(nullable = false) + private LocalTime lunch; + + @Column(nullable = false) + private LocalTime dinner; + + public void updateAlarmTime(AlarmTimeDto alarmTimeDto) { + this.morning = alarmTimeDto.getMorning(); + this.lunch = alarmTimeDto.getLunch(); + this.dinner = alarmTimeDto.getDinner(); + } + + public AlarmTimeResponse toAlarmTimeResponse() { + return AlarmTimeResponse.builder() + .userIdx(this.userIdx) + .morning(this.morning) + .lunch(this.lunch) + .dinner(this.dinner) + .build(); + } +} diff --git a/src/main/java/com/nadoyagsa/pillaroid/repository/AlarmTimeRepository.java b/src/main/java/com/nadoyagsa/pillaroid/repository/AlarmTimeRepository.java new file mode 100644 index 0000000..5e46061 --- /dev/null +++ b/src/main/java/com/nadoyagsa/pillaroid/repository/AlarmTimeRepository.java @@ -0,0 +1,8 @@ +package com.nadoyagsa.pillaroid.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.nadoyagsa.pillaroid.entity.AlarmTime; + +public interface AlarmTimeRepository extends JpaRepository { +} diff --git a/src/main/java/com/nadoyagsa/pillaroid/service/AlarmTimeService.java b/src/main/java/com/nadoyagsa/pillaroid/service/AlarmTimeService.java new file mode 100644 index 0000000..f14e19f --- /dev/null +++ b/src/main/java/com/nadoyagsa/pillaroid/service/AlarmTimeService.java @@ -0,0 +1,44 @@ +package com.nadoyagsa.pillaroid.service; + +import java.util.Optional; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.nadoyagsa.pillaroid.dto.AlarmTimeDto; +import com.nadoyagsa.pillaroid.entity.AlarmTime; +import com.nadoyagsa.pillaroid.entity.User; +import com.nadoyagsa.pillaroid.repository.AlarmTimeRepository; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Service +public class AlarmTimeService { + + private final AlarmTimeRepository alarmTimeRepository; + + // 알림 시간대 + public Optional findAlarmTimeByUserIdx(Long userIdx) { + return alarmTimeRepository.findById(userIdx); + } + + // 알림 시간대 저장 + @Transactional + public AlarmTime saveAlarmTime(User user, AlarmTimeDto alarmTimeDto) { + // 사용자의 알림 시간대 조회 + Optional optAlarmTime = alarmTimeRepository.findById(user.getUserIdx()); + + AlarmTime alarmTime; + if (optAlarmTime.isPresent()) { // 조회 O + alarmTime = optAlarmTime.get(); + alarmTime.updateAlarmTime(alarmTimeDto); // 알림 시간대 업데이트 + } else { // 조회 X + alarmTime = alarmTimeDto.toEntity(user); // 알림 시간대 생성 + } + + alarmTimeRepository.save(alarmTime); + + return alarmTime; + } +}