Skip to content

Commit

Permalink
[feat] AlarmTime 엔티티 time 컬럼 타입 변경 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
soeunkk committed Sep 3, 2022
1 parent 0af791f commit 6a79b29
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.nadoyagsa.pillaroid.common.controller;

import java.io.IOException;
import java.time.format.DateTimeParseException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.nadoyagsa.pillaroid.common.exception.ConflictException;
import com.nadoyagsa.pillaroid.common.exception.ForbiddenException;
import com.nadoyagsa.pillaroid.common.exception.InternalServerException;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -76,6 +76,13 @@ protected ApiResponse notFoundError(NotFoundException ex) {
return ApiResponse.error(ex.getErrorCode());
}

@ResponseStatus(value = HttpStatus.CONFLICT)
@ExceptionHandler(value = {ConflictException.class})
protected ApiResponse conflictError(ConflictException ex) {
log.info(ex.getErrorCode().getDetail());
return ApiResponse.error(ex.getErrorCode());
}

@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = {InternalServerException.class})
protected ApiResponse IOError(InternalServerException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.nadoyagsa.pillaroid.common.exception;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ConflictException extends RuntimeException {
public static final ConflictException DATA_CONFLICT = new ConflictException(ErrorCode.DATA_CONFLICT);

private final ErrorCode errorCode;

public ErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ public enum ErrorCode {
/* 403 FORBIDDEN: 권한 없음 */
DELETE_FORBIDDEN(40301, FORBIDDEN, "삭제할 권한이 없습니다."),

/* 404 NOT_FOUND : Resource 를 찾을 수 없음 */
/* 404 NOT_FOUND: Resource 를 찾을 수 없음 */
DATA_NOT_FOUND(40403, NOT_FOUND, "해당 정보가 없습니다."),
BARCODE_NOT_FOUND(40401, NOT_FOUND, "해당 바코드에 대한 정보가 없습니다."),
MEDICINE_NOT_FOUND(40402, NOT_FOUND, "조건에 맞는 의약품이 존재하지 않습니다."),

/* 408 CONFLICT: 요청이 현재 서버의 상태와 충돌 */
DATA_CONFLICT(40901, CONFLICT, "리소스 충돌이 발생했습니다."),

/* 500 INTERNAL_SERVER_ERROR : 서버 오류 */
INTERNAL_ERROR(500, INTERNAL_SERVER_ERROR, "서버 내부 오류로 인해 응답을 제공할 수 없습니다.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.nadoyagsa.pillaroid.common.exception.UnauthorizedException;
import com.nadoyagsa.pillaroid.dto.AlarmDto;
import com.nadoyagsa.pillaroid.dto.AlarmResponse;
import com.nadoyagsa.pillaroid.dto.AlarmTimeResponse;
import com.nadoyagsa.pillaroid.entity.User;
import com.nadoyagsa.pillaroid.jwt.AuthTokenProvider;
import com.nadoyagsa.pillaroid.service.AlarmService;
Expand Down Expand Up @@ -53,25 +52,25 @@ public ApiResponse<List<AlarmResponse>> getUserAlarmList(HttpServletRequest requ
return ApiResponse.success(alarms);
}

// TODO: 의약품에 대한 사용자 알림 등록
// 의약품에 대한 사용자 알림 등록
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ApiResponse<AlarmTimeResponse> saveUserAlarm(HttpServletRequest request, @RequestBody AlarmDto alarmDto) {
public ApiResponse<AlarmResponse> saveUserAlarm(HttpServletRequest request, @RequestBody AlarmDto alarmDto) {
User user = findUserByToken(request)
.orElseThrow(() -> UnauthorizedException.UNAUTHORIZED_USER);

AlarmTimeResponse alarmTimeResponse = alarmService.saveAlarm(user, alarmDto);
return ApiResponse.success(alarmTimeResponse);
AlarmResponse alarmResponse = alarmService.saveAlarm(user, alarmDto);
return ApiResponse.success(alarmResponse);
}

// 의약품에 대한 사용자 알림 삭제
@DeleteMapping("/{aid}")
public ApiResponse<AlarmTimeResponse> deleteUserAlarm(HttpServletRequest request, @PathVariable("aid") long alarmIdx) throws ForbiddenException {
public ApiResponse<String> deleteUserAlarm(HttpServletRequest request, @PathVariable("aid") long alarmIdx) throws ForbiddenException {
User user = findUserByToken(request)
.orElseThrow(() -> UnauthorizedException.UNAUTHORIZED_USER);

AlarmTimeResponse alarmAndTime = alarmService.deleteAlarm(user, alarmIdx); // 알림 데이터 삭제
return ApiResponse.success(alarmAndTime);
alarmService.deleteAlarm(user, alarmIdx); // 알림 데이터 삭제
return ApiResponse.SUCCESS;
}

// 사용자 jwt 토큰으로부터 회원 정보 조회
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/nadoyagsa/pillaroid/dto/AlarmResponse.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.nadoyagsa.pillaroid.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AlarmResponse {
private long alarmIdx;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nadoyagsa.pillaroid.dto;

import java.time.LocalTime;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonInclude;

Expand All @@ -12,5 +12,5 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AlarmTimeDto {
private long alarmTimeIdx;
private LocalTime time;
private LocalDateTime time;
}
38 changes: 0 additions & 38 deletions src/main/java/com/nadoyagsa/pillaroid/dto/AlarmTimeResponse.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/com/nadoyagsa/pillaroid/entity/AlarmTime.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nadoyagsa.pillaroid.entity;

import java.time.LocalTime;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand Down Expand Up @@ -34,7 +34,7 @@ public class AlarmTime {
private Alarm alarm;

@Column
private LocalTime time;
private LocalDateTime time;

public AlarmTimeDto toAlarmTimeDto() {
return AlarmTimeDto.builder()
Expand Down
49 changes: 23 additions & 26 deletions src/main/java/com/nadoyagsa/pillaroid/service/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.time.temporal.ChronoUnit.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -16,12 +17,11 @@
import org.springframework.transaction.annotation.Transactional;

import com.nadoyagsa.pillaroid.common.exception.BadRequestException;
import com.nadoyagsa.pillaroid.common.exception.ConflictException;
import com.nadoyagsa.pillaroid.common.exception.ForbiddenException;
import com.nadoyagsa.pillaroid.common.exception.NotFoundException;
import com.nadoyagsa.pillaroid.dto.AlarmDto;
import com.nadoyagsa.pillaroid.dto.AlarmResponse;
import com.nadoyagsa.pillaroid.dto.AlarmTimeDto;
import com.nadoyagsa.pillaroid.dto.AlarmTimeResponse;
import com.nadoyagsa.pillaroid.entity.MealTime;
import com.nadoyagsa.pillaroid.entity.Medicine;
import com.nadoyagsa.pillaroid.entity.Alarm;
Expand Down Expand Up @@ -62,10 +62,14 @@ public List<AlarmResponse> findAlarmByUser(User user) {

// 의약품에 해당하는 사용자 알림 등록
@Transactional
public AlarmTimeResponse saveAlarm(User user, AlarmDto alarmDto) {
public AlarmResponse saveAlarm(User user, AlarmDto alarmDto) {
Medicine medicine = medicineRepository.findById(alarmDto.getMedicineIdx())
.orElseThrow(() -> BadRequestException.BAD_PARAMETER);

if (alarmRepository.findByUserAndMedicine(user, medicine).isPresent()) {
throw ConflictException.DATA_CONFLICT;
}

MealTime mealTime = mealTimeRepository.findById(user.getUserIdx())
.orElseThrow(() -> BadRequestException.NOT_EXIST_MEAL_TIME);

Expand All @@ -83,9 +87,9 @@ public AlarmTimeResponse saveAlarm(User user, AlarmDto alarmDto) {
Alarm savedAlarm = alarmRepository.save(alarm);

// 알림 시간 저장
List<AlarmTime> savedAlarmTimes = alarmTimeRepository.saveAll(createAlarmTime(alarm, mealTime, threeTakingTime));
alarmTimeRepository.saveAll(createAlarmTime(alarm, mealTime, threeTakingTime));

return new AlarmTimeResponse(savedAlarm, savedAlarmTimes);
return savedAlarm.toAlarmResponse();
}

// 용법용량에서 복용횟수, 복용량, 복용시기 파싱
Expand Down Expand Up @@ -210,14 +214,18 @@ private List<AlarmTime> createAlarmTime(Alarm alarm, MealTime mealTime, Integer[
List<AlarmTime> result = new ArrayList<>();

LocalTime[] mealTimes = { mealTime.getMorning(), mealTime.getLunch(), mealTime.getDinner() };
for (int i = 0; i < 3; i++) {
if (threeTakingTime[i] != null) {
LocalTime time = mealTimes[i].plusMinutes(threeTakingTime[i]);
AlarmTime alarmTime = AlarmTime.builder()
.alarm(alarm)
.time(time)
.build();
result.add(alarmTime);
for (int day = 0; day < alarm.getPeriod(); day++) {
LocalDate alarmDay = LocalDate.now().plusDays(day);

for (int i = 0; i < 3; i++) {
if (threeTakingTime[i] != null) {
LocalTime time = mealTimes[i].plusMinutes(threeTakingTime[i]);
AlarmTime alarmTime = AlarmTime.builder()
.alarm(alarm)
.time(alarmDay.atTime(time))
.build();
result.add(alarmTime);
}
}
}

Expand All @@ -226,7 +234,7 @@ private List<AlarmTime> createAlarmTime(Alarm alarm, MealTime mealTime, Integer[

// 의약품에 해당하는 사용자 알림 삭제
@Transactional
public AlarmTimeResponse deleteAlarm(User user, long alarmIdx) throws NotFoundException, ForbiddenException {
public void deleteAlarm(User user, long alarmIdx) throws NotFoundException, ForbiddenException {
// 알림 데이터가 있는지 검사
Alarm alarm = alarmRepository.findById(alarmIdx)
.orElseThrow(() -> BadRequestException.BAD_PARAMETER);
Expand All @@ -236,17 +244,6 @@ public AlarmTimeResponse deleteAlarm(User user, long alarmIdx) throws NotFoundEx
throw ForbiddenException.deleteForbidden;
}

List<AlarmTime> alarmTimes = alarmTimeRepository.findByAlarm(alarm); // 삭제할 알림과 관련된 시간 데이터 조회
alarmRepository.deleteById(alarm.getAlarmIdx()); // 데이터 삭제

// 전달할 Response 생성
List<AlarmTimeDto> alarmTimeDtos = alarmTimes.stream()
.map(AlarmTime::toAlarmTimeDto)
.collect(Collectors.toList());

return AlarmTimeResponse.builder()
.alarmIdx(alarmIdx)
.alarmTimeList(alarmTimeDtos)
.build();
alarmRepository.deleteById(alarmIdx); // 데이터 삭제
}
}

0 comments on commit 6a79b29

Please sign in to comment.