Skip to content

Commit

Permalink
Type: Record 수정, 삭제 API 구현 (#61)
Browse files Browse the repository at this point in the history
[Feat] Record 수정, 삭제 API
  • Loading branch information
pingowl authored Oct 27, 2023
2 parents 02efee3 + 2f1e344 commit 512155c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/main/java/igoMoney/BE/controller/RecordController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -54,4 +54,19 @@ public ResponseEntity<List<RecordResponse>> getUserDailyRecordList(@PathVariable
return ResponseEntity.status(HttpStatus.OK).body(response);
}

// record 삭제
@DeleteMapping("delete/{recordId}")
public ResponseEntity<Void> deleteRecord(@PathVariable("recordId") Long recordId ) {

recordService.deleteRecord(recordId);
return new ResponseEntity(HttpStatus.OK);
}

// record 수정
@PatchMapping("edit")
public ResponseEntity<Void> updateRecord(@Valid RecordUpdateRequest request) throws IOException {

recordService.updateRecord(request);
return new ResponseEntity(HttpStatus.OK);
}
}
7 changes: 7 additions & 0 deletions src/main/java/igoMoney/BE/domain/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 0 additions & 4 deletions src/main/java/igoMoney/BE/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
@Transactional
public class ImageService {





private final S3Client s3Client;

// Upload 하고자 하는 버킷의 이름
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/igoMoney/BE/service/RecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 512155c

Please sign in to comment.