Skip to content

Commit

Permalink
[feat] 용법용량 파싱 제외 알림 등록 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
soeunkk committed Aug 30, 2022
1 parent 81cc033 commit 8d13bdb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class BadRequestException extends RuntimeException {
public static final BadRequestException BAD_PARAMETER = new BadRequestException(ErrorCode.BAD_PARAMETER);
public static final BadRequestException NOT_SUPPORTED_BARCODE_FORMAT = new BadRequestException(ErrorCode.NOT_SUPPORTED_BARCODE_FORMAT);
public static final BadRequestException NOT_EXIST_MEAL_TIME = new BadRequestException(ErrorCode.NOT_EXIST_MEAL_TIME);

private final ErrorCode errorCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ErrorCode {
BAD_PARAMETER(40001, BAD_REQUEST, "요청 파라미터가 잘못되었습니다."),
BAD_PARAMETER_TYPE(40002, BAD_REQUEST, "지원하지 않는 파라미터 형식입니다."),
NOT_SUPPORTED_BARCODE_FORMAT(40003, BAD_REQUEST, "지원하지 않는 바코드 형식입니다."),
NOT_EXIST_MEAL_TIME(40004, BAD_REQUEST, "복용 시간대가 설정되지 않았습니다."),

/* 401 UNAUTHORIZED: 인증 자격 없음 */
UNAUTHORIZED_USER(401, UNAUTHORIZED, "인증된 사용자가 아닙니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,18 @@ public ApiResponse<List<AlarmResponse>> getUserAlarmList(HttpServletRequest requ
List<AlarmResponse> alarms = alarmService.findAlarmByUser(user);
return ApiResponse.success(alarms);
}


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

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

// 의약품에 대한 사용자 알림 삭제
@DeleteMapping("/alarm/{aid}")
public ApiResponse<AlarmTimeResponse> deleteUserAlarm(HttpServletRequest request, @PathVariable("aid") long alarmIdx) throws ForbiddenException {
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/nadoyagsa/pillaroid/service/AlarmService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.nadoyagsa.pillaroid.service;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

Expand All @@ -10,9 +14,11 @@
import com.nadoyagsa.pillaroid.common.exception.BadRequestException;
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;
import com.nadoyagsa.pillaroid.entity.AlarmTime;
Expand Down Expand Up @@ -50,6 +56,71 @@ public List<AlarmResponse> findAlarmByUser(User user) {
.collect(Collectors.toList());
}

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

Map<String, String> dosageSummary = parseDosage(medicine.getDosage()); // TODO: 메소드 구현
Integer[] threeTakingTime = getThreeTakingTime(dosageSummary.get("number"), dosageSummary.get("when")); // TODO: 메소드 구현

// 알림 저장
Alarm alarm = Alarm.builder()
.user(user)
.medicine(medicine)
.name(alarmDto.getName())
.period(alarmDto.getPeriod())
.dosage(String.format("%s, %s, %s", dosageSummary.get("number"), dosageSummary.get("amount"), dosageSummary.get("when")))
.build();
Alarm savedAlarm = alarmRepository.save(alarm);

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

return new AlarmTimeResponse(savedAlarm, savedAlarmTimes);
}

// 용법용량에서 복용횟수, 복용량, 복용시기 파싱
private Map<String, String> parseDosage(String dosage) {
// TODO: 복용횟수, 복용량, 복용시기 파싱
Map<String,String> map = new HashMap<>();

map.put("number", ""); // 복용횟수 ex) 1일 1회
map.put("amount", ""); // 복용량 ex) 1회 2정
map.put("when", ""); // 복용시긴 ex) 식전 30분
return map;
}

// 상대복용시간 계산
private Integer[] getThreeTakingTime(String number, String when) {
// TODO: 복용횟수와 복용시기를 통해 상대복용시간 계산
Integer[] threeTakingTime = new Integer[3]; // 아침,점심,저녁식사 기준 상대복용시간 (단위:분) (ex. 1일 2회, 식전 30분: [-30,null,-30])

return threeTakingTime;
}

// 복용 시간대를 기준으로 AlarmTime 객체 생성
private List<AlarmTime> createAlarmTime(User user, Integer[] threeTakingTime) {
List<AlarmTime> result = new ArrayList<>();

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

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()
.time(time)
.build();
result.add(alarmTime);
}
}

return result;
}

// 의약품에 해당하는 사용자 알림 삭제
@Transactional
public AlarmTimeResponse deleteAlarm(User user, long alarmIdx) throws NotFoundException, ForbiddenException {
Expand Down

0 comments on commit 8d13bdb

Please sign in to comment.