Skip to content

Commit

Permalink
[feat] 알림 시간대 (조회, 등록) API 구현 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
soeunkk committed Aug 28, 2022
1 parent 0bdff7c commit 5da7993
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 3 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@
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;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.List;
import java.util.Optional;

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

// 사용자의 의약품 번호에 해당하는 즐겨찾기 여부 조회
Expand Down Expand Up @@ -90,7 +99,7 @@ public ApiResponse<FavoritesResponse> postUserFavorites(HttpServletRequest reque

// 즐겨찾기 삭제
@DeleteMapping(value = "/favorites/{fid}")
public ApiResponse deleteUserFavorites(HttpServletRequest request, @PathVariable("fid") Long favoritesIdx) throws IllegalStateException {
public ApiResponse<String> deleteUserFavorites(HttpServletRequest request, @PathVariable("fid") Long favoritesIdx) throws IllegalStateException {
Optional<Favorites> favorites = favoritesService.findFavoritesByIdx(favoritesIdx);

if (favorites.isPresent()) {
Expand Down Expand Up @@ -130,6 +139,33 @@ public ApiResponse<List<FavoritesResponse>> getUserFavorites(HttpServletRequest
throw UnauthorizedException.UNAUTHORIZED_USER;
}

// 사용자의 복용 시간대 조회
@GetMapping("/alarmtime")
public ApiResponse<AlarmTimeResponse> getUserAlarmTime(HttpServletRequest request) {
User user = findUserByToken(request).orElseThrow(
() -> UnauthorizedException.UNAUTHORIZED_USER
);

Optional<AlarmTime> optAlarmTime = alarmTimeService.findAlarmTimeByUserIdx(user.getUserIdx());
AlarmTime alarmTime = optAlarmTime.orElseThrow(
() -> NotFoundException.DATA_NOT_FOUND
);

return ApiResponse.success(alarmTime.toAlarmTimeResponse());
}

// 사용자의 복용 시간대 추가 및 수정
@PostMapping("/alarmtime")
public ApiResponse<AlarmTimeResponse> 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<User> findUserByToken(HttpServletRequest request) {
try {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeDto.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
17 changes: 17 additions & 0 deletions src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeResponse.java
Original file line number Diff line number Diff line change
@@ -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; // 저녁 식사 시간
}
61 changes: 61 additions & 0 deletions src/main/java/com/nadoyagsa/pillaroid/entity/AlarmTime.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<AlarmTime, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<AlarmTime> findAlarmTimeByUserIdx(Long userIdx) {
return alarmTimeRepository.findById(userIdx);
}

// 알림 시간대 저장
@Transactional
public AlarmTime saveAlarmTime(User user, AlarmTimeDto alarmTimeDto) {
// 사용자의 알림 시간대 조회
Optional<AlarmTime> 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;
}
}

0 comments on commit 5da7993

Please sign in to comment.