Skip to content

Commit

Permalink
[Feat]: 복용 체크 API 구현
Browse files Browse the repository at this point in the history
복용 체크 완료 API 구현
복용 체크 취소 API 구현

Related to: #58
  • Loading branch information
onpyeong committed Jan 30, 2024
1 parent a238c17 commit 54ba4c5
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum ErrorCode {
UNREGISTERED_PILL(HttpStatus.NOT_FOUND, "등록되지 않은 약입니다."),
UNAUTHORIZED_PILL(HttpStatus.FORBIDDEN, "접근 권한이 없는 약입니다."),
NOT_SEND_PILL(HttpStatus.NOT_FOUND, "전송된 적이 없는 약입니다."),
UNREGISTERED_PILL_SCHEDULE(HttpStatus.NOT_FOUND, "등록되지 않은 약 일정입니다."),


// friend
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/sobok/SobokSobok/exception/SuccessCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public enum SuccessCode {
GET_PILL_COUNT_SUCCESS(HttpStatus.OK, "약 개수 조회에 성공했습니다."),
SEND_PILL_SUCCESS(HttpStatus.CREATED, "약 전송에 성공했습니다."),
DELETE_PILL_SUCCESS(HttpStatus.OK, "약 삭제에 성공했습니다."),
CHECK_PILL_SCHEDULE_SUCCESS(HttpStatus.OK, "복용 완료 체크에 성공했습니다."),
UNCHECK_PILL_SCHEDULE_SUCCESS(HttpStatus.OK, "복용 체크 취소에 성공했습니다."),

// friend
ADD_FRIEND_SUCCESS(HttpStatus.OK, "공유 요청에 성공했습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.sobok.SobokSobok.pill.application;

import io.sobok.SobokSobok.auth.application.util.UserServiceUtil;
import io.sobok.SobokSobok.auth.infrastructure.UserRepository;
import io.sobok.SobokSobok.exception.ErrorCode;
import io.sobok.SobokSobok.exception.model.ForbiddenException;
import io.sobok.SobokSobok.pill.domain.Pill;
import io.sobok.SobokSobok.pill.domain.PillSchedule;
import io.sobok.SobokSobok.pill.infrastructure.PillRepository;
import io.sobok.SobokSobok.pill.infrastructure.PillScheduleRepository;
import io.sobok.SobokSobok.pill.ui.dto.CheckPillScheduleResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class PillScheduleService {

private final UserRepository userRepository;
private final PillRepository pillRepository;
private final PillScheduleRepository pillScheduleRepository;

@Transactional
public CheckPillScheduleResponse changePillScheduleCheck(Long userId, Long scheduleId, Boolean isCheck) {
UserServiceUtil.existsUserById(userRepository, userId);

PillSchedule pillSchedule = PillScheduleServiceUtil.findPillScheduleById(pillScheduleRepository, scheduleId);

Pill pill = PillServiceUtil.findPillById(pillRepository, pillSchedule.getPillId());

if (!pill.getUserId().equals(userId)) {
throw new ForbiddenException(ErrorCode.FORBIDDEN_EXCEPTION);
}

pillSchedule.changePillScheduleCheck(isCheck);

return CheckPillScheduleResponse.builder()
.scheduleId(scheduleId)
.pillId(pill.getId())
.userId(userId)
.scheduleDate(pillSchedule.getScheduleDate().atStartOfDay())
.scheduleTime(pillSchedule.getScheduleTime())
.isCheck(isCheck)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sobok.SobokSobok.pill.application;

import io.sobok.SobokSobok.exception.ErrorCode;
import io.sobok.SobokSobok.exception.model.NotFoundException;
import io.sobok.SobokSobok.pill.domain.PillSchedule;
import io.sobok.SobokSobok.pill.infrastructure.PillScheduleRepository;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PillScheduleServiceUtil {

public static PillSchedule findPillScheduleById(PillScheduleRepository pillScheduleRepository, Long id) {
return pillScheduleRepository.findById(id)
.orElseThrow(() -> new NotFoundException(ErrorCode.UNREGISTERED_PILL_SCHEDULE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public PillSchedule(LocalDate scheduleDate, String scheduleTime, Long pillId) {
this.scheduleTime = scheduleTime;
this.pillId = pillId;
}

public void changePillScheduleCheck(Boolean isCheck) {
this.isCheck = isCheck;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.sobok.SobokSobok.pill.ui;

import io.sobok.SobokSobok.auth.domain.User;
import io.sobok.SobokSobok.common.dto.ApiResponse;
import io.sobok.SobokSobok.exception.SuccessCode;
import io.sobok.SobokSobok.pill.application.PillScheduleService;
import io.sobok.SobokSobok.pill.ui.dto.CheckPillScheduleResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/schedule")
@Tag(name = "Schedule", description = "약 일정 관련 컨트롤러")
public class PillScheduleController {

private final PillScheduleService pillScheduleService;

@PutMapping("/check/{scheduleId}")
@Operation(
summary = "복용 체크 완료 API 메서드",
description = "약 일정의 복용 체크를 완료하는 메서드입니다."
)
public ResponseEntity<ApiResponse<CheckPillScheduleResponse>> checkPillSchedule(
@AuthenticationPrincipal User user,
@PathVariable Long scheduleId
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(ApiResponse.success(
SuccessCode.CHECK_PILL_SCHEDULE_SUCCESS,
pillScheduleService.changePillScheduleCheck(user.getId(), scheduleId, true)
));
}

@PutMapping("/uncheck/{scheduleId}")
@Operation(
summary = "복용 체크 취소 API 메서드",
description = "약 일정의 복용 체크를 취소하는 메서드입니다."
)
public ResponseEntity<ApiResponse<CheckPillScheduleResponse>> uncheckPillSchedule(
@AuthenticationPrincipal User user,
@PathVariable Long scheduleId
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(ApiResponse.success(
SuccessCode.UNCHECK_PILL_SCHEDULE_SUCCESS,
pillScheduleService.changePillScheduleCheck(user.getId(), scheduleId, false)
));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.sobok.SobokSobok.pill.ui.dto;

import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record CheckPillScheduleResponse(
Long scheduleId,
Long pillId,
Long userId,
LocalDateTime scheduleDate,
String scheduleTime,
Boolean isCheck
) {

}

0 comments on commit 54ba4c5

Please sign in to comment.