Skip to content

Commit

Permalink
Merge pull request #108 from Likelion-YeungNam-Univ/feature-medicine
Browse files Browse the repository at this point in the history
Medicine 삭제 API 구현
  • Loading branch information
iampingu99 authored Aug 3, 2024
2 parents 968c839 + e93c8c6 commit 38dff18
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,33 @@ public interface MedicineApi {
@ApiResponse(responseCode = "200", description = "영양제 목록 조회 성공")
ResponseEntity<List<MedicineResponseDto>> 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<String> delete(HttpServletRequest request, @PathVariable Long medicineId);


@PostMapping("/{medicineId}/history")
@Operation(summary = "영양제 복용 기록 생성", description = "사용자가 영양제를 복용했다는 기록을 생성하기 위한 API 입니다.")
@ApiResponse(responseCode = "200", description = "영양제 복용 기록 생성 성공")
@ApiResponse(responseCode = "403", description = "해당 영양제의 작성자가 아닌 경우",
Expand All @@ -59,7 +85,7 @@ public interface MedicineApi {
}))
ResponseEntity<String> taken(HttpServletRequest request, @PathVariable Long medicineId);

@DeleteMapping("/{medicineId}")
@DeleteMapping("/{medicineId}/history")
@Operation(summary = "영양제 복용 기록 삭제", description = "사용자가 영양제 복용으로 생성된 영양제 복용 기록을 삭제하기 위한 API 입니다.")
@ApiResponse(responseCode = "200", description = "영양제 복용 기록 삭제 성공")
@ApiResponse(responseCode = "403", description = "해당 영양제의 작성자가 아닌 경우",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ public ResponseEntity<String> 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<String> 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<List<MedicineResponseDto>> read(HttpServletRequest request) {
Expand All @@ -58,7 +66,7 @@ public ResponseEntity<String> 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<String> skip(HttpServletRequest request, @PathVariable Long medicineId) {
Expand All @@ -69,6 +77,6 @@ public ResponseEntity<String> 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("영양제 복용 기록이 삭제되었습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<MedicineHistory> medicineHistoryList = new ArrayList<>();

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
Expand All @@ -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<Object[]> readAll(Long userId) {
return medicineRepository.findAllByUserId(userId);
}
Expand Down

0 comments on commit 38dff18

Please sign in to comment.