From 2f1e344995a59cfc204f220216dce4f82898649a Mon Sep 17 00:00:00 2001 From: BK Date: Fri, 27 Oct 2023 23:54:20 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20Record=20=EC=88=98=EC=A0=95,=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BE/controller/RecordController.java | 17 +++++++++++++- src/main/java/igoMoney/BE/domain/Record.java | 7 ++++++ .../BE/dto/request/RecordUpdateRequest.java | 23 +++++++++++++++++++ .../igoMoney/BE/service/ImageService.java | 4 ---- .../igoMoney/BE/service/RecordService.java | 19 ++++++++++++++- 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java diff --git a/src/main/java/igoMoney/BE/controller/RecordController.java b/src/main/java/igoMoney/BE/controller/RecordController.java index cd2b1d4..b698d20 100644 --- a/src/main/java/igoMoney/BE/controller/RecordController.java +++ b/src/main/java/igoMoney/BE/controller/RecordController.java @@ -1,6 +1,7 @@ package igoMoney.BE.controller; import igoMoney.BE.dto.request.RecordSaveRequest; +import igoMoney.BE.dto.request.RecordUpdateRequest; import igoMoney.BE.dto.response.IdResponse; import igoMoney.BE.dto.response.RecordResponse; import igoMoney.BE.service.RecordService; @@ -9,7 +10,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import java.io.IOException; @@ -54,4 +54,19 @@ public ResponseEntity> getUserDailyRecordList(@PathVariable return ResponseEntity.status(HttpStatus.OK).body(response); } + // record 삭제 + @DeleteMapping("delete/{recordId}") + public ResponseEntity deleteRecord(@PathVariable("recordId") Long recordId ) { + + recordService.deleteRecord(recordId); + return new ResponseEntity(HttpStatus.OK); + } + + // record 수정 + @PatchMapping("edit") + public ResponseEntity updateRecord(@Valid RecordUpdateRequest request) throws IOException { + + recordService.updateRecord(request); + return new ResponseEntity(HttpStatus.OK); + } } diff --git a/src/main/java/igoMoney/BE/domain/Record.java b/src/main/java/igoMoney/BE/domain/Record.java index d49ef83..fba8987 100644 --- a/src/main/java/igoMoney/BE/domain/Record.java +++ b/src/main/java/igoMoney/BE/domain/Record.java @@ -34,4 +34,11 @@ public class Record extends BaseEntity { private String image; @Column(nullable = false) private LocalDate date; + + public void updateRecord(String title, String content, Integer cost, String image){ + this.title = title; + this.content = content; + this.cost = cost; + this.image = image; + } } diff --git a/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java b/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java new file mode 100644 index 0000000..26cca47 --- /dev/null +++ b/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java @@ -0,0 +1,23 @@ +package igoMoney.BE.dto.request; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.*; +import org.springframework.web.multipart.MultipartFile; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class RecordUpdateRequest { + + @NotNull + private Long recordId; + @NotNull @Size(min=5, max=15) + private String title; + @Size(max=300) + private String content; + private Integer cost; + private MultipartFile image; +} diff --git a/src/main/java/igoMoney/BE/service/ImageService.java b/src/main/java/igoMoney/BE/service/ImageService.java index 579266c..f4ce909 100644 --- a/src/main/java/igoMoney/BE/service/ImageService.java +++ b/src/main/java/igoMoney/BE/service/ImageService.java @@ -20,10 +20,6 @@ @Transactional public class ImageService { - - - - private final S3Client s3Client; // Upload 하고자 하는 버킷의 이름 diff --git a/src/main/java/igoMoney/BE/service/RecordService.java b/src/main/java/igoMoney/BE/service/RecordService.java index b1da486..09e0745 100644 --- a/src/main/java/igoMoney/BE/service/RecordService.java +++ b/src/main/java/igoMoney/BE/service/RecordService.java @@ -6,6 +6,7 @@ import igoMoney.BE.domain.Record; import igoMoney.BE.domain.User; import igoMoney.BE.dto.request.RecordSaveRequest; +import igoMoney.BE.dto.request.RecordUpdateRequest; import igoMoney.BE.dto.response.RecordResponse; import igoMoney.BE.repository.ChallengeRepository; import igoMoney.BE.repository.RecordRepository; @@ -111,6 +112,22 @@ public void deleteAllUserRecords(Long userId) { } } + // record 삭제 + public void deleteRecord(Long recordId) { + + Record findRecord = getRecordOrThrow(recordId); + recordRepository.delete(findRecord); + } + + // record 수정 + public void updateRecord(RecordUpdateRequest request) throws IOException { + + Record findRecord = getRecordOrThrow(request.getRecordId()); + checkExistsImage(request.getImage()); + String image = imageService.uploadImage(request.getImage()); + findRecord.updateRecord(request.getTitle(), request.getContent(), request.getCost(), image); + } + // 예외 처리 - 존재하는 record 인가 private Record getRecordOrThrow(Long id) { @@ -144,7 +161,7 @@ private void checkPermission(User user, Long ChallengeId) { // 예외 처리 - 이미지 존재여부 private void checkExistsImage(MultipartFile image) { - if (image.isEmpty()) { + if ((image == null) || image.isEmpty()) { throw new CustomException(ErrorCode.SHOULD_EXIST_IMAGE); } }