Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat]: 약 수정 API 구현 #155

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum SuccessCode {
GET_PILL_INFO_SUCCESS(HttpStatus.OK, "약 정보 조회에 성공했습니다."),
SEND_PILL_SUCCESS(HttpStatus.CREATED, "약 전송에 성공했습니다."),
DELETE_PILL_SUCCESS(HttpStatus.OK, "약 삭제에 성공했습니다."),
UPDATE_PILL_SUCCESS(HttpStatus.OK, "약 수정에 성공했습니다."),

// schedule
CHECK_PILL_SCHEDULE_SUCCESS(HttpStatus.OK, "복용 완료 체크에 성공했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.sobok.SobokSobok.pill.ui.dto.PillListResponse;
import io.sobok.SobokSobok.pill.ui.dto.PillRequest;
import io.sobok.SobokSobok.pill.ui.dto.PillResponse;
import io.sobok.SobokSobok.pill.ui.dto.PillUpdateRequest;
import io.sobok.SobokSobok.utils.PillUtil;
import lombok.RequiredArgsConstructor;
import io.sobok.SobokSobok.pill.infrastructure.PillQueryRepository;
Expand All @@ -30,6 +31,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -152,6 +154,41 @@ public Integer getPillCount(Long userId) {
return pillQueryRepository.getPillCount(userId);
}

@Transactional
public void updatePill(Long userId, Long pillId, PillUpdateRequest request) {
UserServiceUtil.existsUserById(userRepository, userId);
Pill pill = PillServiceUtil.findPillById(pillRepository, pillId);
boolean isTimeListChanged = false;

if (!pill.isPillUser(userId)) {
throw new ForbiddenException(ErrorCode.UNAUTHORIZED_PILL);
}

if (!pill.getPillName().equals(request.pillName())) {
pill.updatePillName(request.pillName());
}

List<String> timeList = Arrays.asList(request.timeList());
if (!timeList.equals(pillScheduleQueryRepository.getPillScheduleTime(pillId))) {
isTimeListChanged = true;
}

if (PillUtil.checkChangePillSchedule(pill, request) || isTimeListChanged) {
pillScheduleRepository.deleteAllByPillId(pillId);
LocalDate[] scheduleDate = PillUtil.getScheduleDateList(request.startDate(), request.endDate(), request.day().split(", "));
for (LocalDate date : scheduleDate) {
for (String time : request.timeList()) {
pillScheduleRepository.save(PillSchedule.builder()
.scheduleDate(date)
.scheduleTime(time)
.pillId(pillId)
.build()
);
}
}
}
}

@Transactional
public void deletePill(Long userId, Long pillId) {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/sobok/SobokSobok/pill/domain/Pill.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public void receivePill(Long userId) {
this.userId = userId;
}

public void updatePillName(String newPillName) {
this.pillName = newPillName;
}

public boolean isPillUser(Long userId) {
return Objects.equals(this.userId, userId);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/sobok/SobokSobok/pill/ui/PillController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
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.application.PillService;
import io.sobok.SobokSobok.pill.ui.dto.PillListResponse;
import io.sobok.SobokSobok.pill.ui.dto.PillRequest;
import io.sobok.SobokSobok.pill.ui.dto.PillResponse;
import io.sobok.SobokSobok.pill.ui.dto.PillUpdateRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand All @@ -25,6 +27,7 @@
public class PillController {

private final PillService pillService;
private final PillScheduleService pillScheduleService;

@PostMapping("")
@Operation(
Expand Down Expand Up @@ -89,6 +92,22 @@ public ResponseEntity<ApiResponse<Void>> sendPillToFriend(
.body(ApiResponse.success(SuccessCode.SEND_PILL_SUCCESS));
}

@PutMapping("/{pillId}")
@Operation(
summary = "약 수정 API 메서드",
description = "내 약 정보를 수정하는 메서드입니다."
)
public ResponseEntity<ApiResponse<Void>> updatePill(
@AuthenticationPrincipal User user,
@PathVariable Long pillId,
@RequestBody @Valid final PillUpdateRequest request
) {
pillService.updatePill(user.getId(), pillId, request);
return ResponseEntity
.status(HttpStatus.OK)
.body(ApiResponse.success(SuccessCode.UPDATE_PILL_SUCCESS));
}

@DeleteMapping("/{pillId}")
@Operation(
summary = "약 삭제 API 메서드",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.sobok.SobokSobok.pill.ui.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDate;

public record PillUpdateRequest(

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
String pillName,

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
String day,

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
String[] timeList,

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
LocalDate startDate,

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
LocalDate endDate
) {
}
9 changes: 9 additions & 0 deletions src/main/java/io/sobok/SobokSobok/utils/PillUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import io.sobok.SobokSobok.exception.ErrorCode;
import io.sobok.SobokSobok.exception.model.BadRequestException;
import io.sobok.SobokSobok.pill.domain.Pill;
import io.sobok.SobokSobok.pill.ui.dto.PillRequest;
import io.sobok.SobokSobok.pill.ui.dto.PillUpdateRequest;

import java.time.DayOfWeek;
import java.time.LocalDate;
Expand Down Expand Up @@ -39,6 +42,12 @@ public static LocalDate[] getScheduleDateList(LocalDate startDate, LocalDate end
return matchingDates.toArray(new LocalDate[0]);
}

public static Boolean checkChangePillSchedule(Pill pill, PillUpdateRequest pillRequest) {
return !pill.getScheduleDay().equals(pillRequest.day()) ||
!pill.getStartDate().equals(pillRequest.startDate()) ||
!pill.getEndDate().equals(pillRequest.endDate());
}

private static String convertToEnglishDayOfWeek(String koreanDayOfWeek) {
return switch (koreanDayOfWeek) {
case "월" -> "MONDAY";
Expand Down
Loading