From 016f8603126c4482198c054292b03a07be56fb7b Mon Sep 17 00:00:00 2001 From: iampingu99 <154869950+iampingu99@users.noreply.github.com> Date: Sat, 3 Aug 2024 23:16:42 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20medicine=20delete=20=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20cascade=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../holing/bounded_context/medicine/entity/Medicine.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/holing/bounded_context/medicine/entity/Medicine.java b/src/main/java/com/example/holing/bounded_context/medicine/entity/Medicine.java index 2fa3ed8..c454f8d 100644 --- a/src/main/java/com/example/holing/bounded_context/medicine/entity/Medicine.java +++ b/src/main/java/com/example/holing/bounded_context/medicine/entity/Medicine.java @@ -1,6 +1,7 @@ package com.example.holing.bounded_context.medicine.entity; import com.example.holing.bounded_context.user.entity.User; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -37,7 +38,7 @@ public class Medicine { @JoinColumn(name = "user_id", nullable = false) private User user; - @OneToMany(fetch = FetchType.LAZY, mappedBy = "medicine") + @OneToMany(fetch = FetchType.LAZY, mappedBy = "medicine", cascade = CascadeType.REMOVE) private List medicineHistoryList = new ArrayList<>(); @Builder From e93c8c6885dd8e161705635a9049221dd1bd26aa Mon Sep 17 00:00:00 2001 From: iampingu99 <154869950+iampingu99@users.noreply.github.com> Date: Sat, 3 Aug 2024 23:17:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20medicine=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../medicine/api/MedicineApi.java | 30 +++++++++++++++++-- .../controller/MedicineController.java | 14 +++++++-- .../medicine/serivce/MedicineService.java | 10 +++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/holing/bounded_context/medicine/api/MedicineApi.java b/src/main/java/com/example/holing/bounded_context/medicine/api/MedicineApi.java index ebe0083..bf90173 100644 --- a/src/main/java/com/example/holing/bounded_context/medicine/api/MedicineApi.java +++ b/src/main/java/com/example/holing/bounded_context/medicine/api/MedicineApi.java @@ -34,7 +34,33 @@ public interface MedicineApi { @ApiResponse(responseCode = "200", description = "영양제 목록 조회 성공") ResponseEntity> read(HttpServletRequest request); - @PostMapping("/{medicineId}") + @DeleteMapping("/{medicineId}") + @Operation(summary = "영양제 삭제", description = "사용자가 영양제를 삭제하기 위한 API 입니다.") + @ApiResponse(responseCode = "200", description = "영양제 삭제 성공") + @ApiResponse(responseCode = "403", description = "해당 영양제의 작성자가 아닌 경우", + content = @Content(mediaType = "application/json", examples = { + @ExampleObject(value = """ + { + "timestamp": "2024-07-19T17:56:39.188+00:00", + "name": "ACCESS_DENIED_TO_MEDICINE", + "cause": "해당 영양제에 접근 권한이 없습니다." + } + """), + })) + @ApiResponse(responseCode = "404", description = "영양제가 존재하지 않는 경우", + content = @Content(mediaType = "application/json", examples = { + @ExampleObject(value = """ + { + "timestamp": "2024-07-19T17:56:39.188+00:00", + "name": "MEDICINE_NOT_FOUND", + "cause": "해당 영양제가 존재하지 않습니다." + } + """), + })) + ResponseEntity delete(HttpServletRequest request, @PathVariable Long medicineId); + + + @PostMapping("/{medicineId}/history") @Operation(summary = "영양제 복용 기록 생성", description = "사용자가 영양제를 복용했다는 기록을 생성하기 위한 API 입니다.") @ApiResponse(responseCode = "200", description = "영양제 복용 기록 생성 성공") @ApiResponse(responseCode = "403", description = "해당 영양제의 작성자가 아닌 경우", @@ -59,7 +85,7 @@ public interface MedicineApi { })) ResponseEntity taken(HttpServletRequest request, @PathVariable Long medicineId); - @DeleteMapping("/{medicineId}") + @DeleteMapping("/{medicineId}/history") @Operation(summary = "영양제 복용 기록 삭제", description = "사용자가 영양제 복용으로 생성된 영양제 복용 기록을 삭제하기 위한 API 입니다.") @ApiResponse(responseCode = "200", description = "영양제 복용 기록 삭제 성공") @ApiResponse(responseCode = "403", description = "해당 영양제의 작성자가 아닌 경우", diff --git a/src/main/java/com/example/holing/bounded_context/medicine/controller/MedicineController.java b/src/main/java/com/example/holing/bounded_context/medicine/controller/MedicineController.java index e8a88a7..5541aed 100644 --- a/src/main/java/com/example/holing/bounded_context/medicine/controller/MedicineController.java +++ b/src/main/java/com/example/holing/bounded_context/medicine/controller/MedicineController.java @@ -37,7 +37,15 @@ public ResponseEntity create(HttpServletRequest request, @Valid @Request User user = userService.read(Long.parseLong(userId)); Medicine response = medicineService.create(user, MedicineRequestDto.toEntity(medicineRequestDto)); - return ResponseEntity.ok().body("약이 성공적으로 생성되었습니다."); + return ResponseEntity.ok().body("영양제가 성공적으로 생성되었습니다."); + } + + public ResponseEntity delete(HttpServletRequest request, @PathVariable Long medicineId) { + String accessToken = jwtProvider.getToken(request); + String userId = jwtProvider.getUserId(accessToken); + + Medicine response = medicineService.delete(Long.parseLong(userId), medicineId); + return ResponseEntity.ok().body("영양제가 성공적으로 삭제되었습니다."); } public ResponseEntity> read(HttpServletRequest request) { @@ -58,7 +66,7 @@ public ResponseEntity taken(HttpServletRequest request, @PathVariable Lo if (medicine.getUser().getId() != Long.parseLong(userId)) throw new GlobalException(MedicineExceptionCode.ACCESS_DENIED_TO_MEDICINE); medicineHistoryService.taken(medicineId); - return ResponseEntity.ok("약 복용 기록이 저장되었습니다."); + return ResponseEntity.ok("영양제 복용 기록이 저장되었습니다."); } public ResponseEntity skip(HttpServletRequest request, @PathVariable Long medicineId) { @@ -69,6 +77,6 @@ public ResponseEntity skip(HttpServletRequest request, @PathVariable Lon if (medicine.getUser().getId() != Long.parseLong(userId)) throw new GlobalException(MedicineExceptionCode.ACCESS_DENIED_TO_MEDICINE); medicineHistoryService.skip(medicineId); - return ResponseEntity.ok("약 복용 기록이 삭제되었습니다."); + return ResponseEntity.ok("영양제 복용 기록이 삭제되었습니다."); } } diff --git a/src/main/java/com/example/holing/bounded_context/medicine/serivce/MedicineService.java b/src/main/java/com/example/holing/bounded_context/medicine/serivce/MedicineService.java index 3fa3f83..cfe6a8f 100644 --- a/src/main/java/com/example/holing/bounded_context/medicine/serivce/MedicineService.java +++ b/src/main/java/com/example/holing/bounded_context/medicine/serivce/MedicineService.java @@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Objects; @Service @RequiredArgsConstructor @@ -23,6 +24,15 @@ public Medicine create(User user, Medicine medicine) { return medicineRepository.save(medicine); } + @Transactional + public Medicine delete(Long userId, Long medicineId) { + Medicine medicine = readById(medicineId); + if (!Objects.equals(medicine.getUser().getId(), userId)) + throw new GlobalException(MedicineExceptionCode.ACCESS_DENIED_TO_MEDICINE); + medicineRepository.delete(medicine); + return medicine; + } + public List readAll(Long userId) { return medicineRepository.findAllByUserId(userId); }