Skip to content

Commit

Permalink
feat: 이미지 수정/삭제 기능 추가
Browse files Browse the repository at this point in the history
closed #1
  • Loading branch information
DrumRobot committed Apr 7, 2024
1 parent 5366b53 commit 60720fd
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.selfstudy'
version = 'v0.0.1'
version = 'v0.0.2'

configurations {
compileOnly {
Expand Down
49 changes: 37 additions & 12 deletions src/main/java/picdiary/diary/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import picdiary.diary.domain.Diary;
import picdiary.diary.dto.request.DiaryCreateRequest;
import picdiary.diary.dto.request.DiaryUpdateRequest;
import picdiary.diary.dto.response.GetDiaryResponse;
import picdiary.diary.exception.DiaryErrorCode;
import picdiary.diary.repository.DiaryEntity;
import picdiary.diary.service.DiaryService;
import picdiary.diary.service.S3Service;
import picdiary.global.dto.response.ApplicationResponse;
import picdiary.global.exception.ApplicationException;
import picdiary.user.repository.UserEntity;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

@RestController
@RequiredArgsConstructor
@RequestMapping("/diaries")
Expand All @@ -37,21 +39,33 @@ public class DiaryController {
/**
* 다이어리 작성
*/
@PostMapping
public ResponseEntity<ApplicationResponse<Long>> createDiary(@Parameter(hidden = true) UserEntity user, @RequestParam(value = "imageFile", required = false) MultipartFile file, @RequestParam("content") String content, @RequestParam("date") String date, @RequestParam(value = "emotion", required = false) Diary.Emotion emotion) {
DiaryCreateRequest.DiaryCreateRequestBuilder builder = DiaryCreateRequest.builder().content(content).date(date)
.emotion(emotion);
@PostMapping(consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<ApplicationResponse<Long>> createDiary(@Parameter(hidden = true) UserEntity user, @RequestBody DiaryCreateRequest request) {
Long diaryId = diaryService.createDiary(user.getId(), request);
return ApplicationResponse.success(diaryId, "다이어리가 생성되었습니다.").entity();
}

/* AWS S3 파일 저장 */
/**
* AWS S3 파일 저장
*/
private void createDiaryImageFile(UserEntity user, DiaryUpdateRequest request, String date) {
var file = request.getImageFile();
if (file != null) try {
String prefix = String.format("%s/%s", user.getId(), date);
builder.imageFileName(s3Service.saveFile(prefix, file));
request.setImageFileName(s3Service.saveFile(prefix, file));
} catch (IOException e) {
// 파일 저장 중 오류 발생 시 처리
return ApplicationResponse.error("파일 저장 중 오류가 발생했습니다.", null);
throw new ApplicationException(DiaryErrorCode.SAVE_ERROR);
}
}

Long diaryId = diaryService.createDiary(user.getId(), builder.build());
/**
* Multipart 다이어리 작성
*/
@PostMapping(consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ApplicationResponse<Long>> createForm(@Parameter(hidden = true) UserEntity user, @ModelAttribute DiaryCreateRequest request) {
createDiaryImageFile(user, request, request.getDate());
Long diaryId = diaryService.createDiary(user.getId(), request);
return ApplicationResponse.success(diaryId, "다이어리가 생성되었습니다.").entity();
}

Expand All @@ -77,12 +91,23 @@ public GetDiaryResponse getDiaryInfo(@Parameter(hidden = true) UserEntity user,
/**
* 다이어리 수정
*/
@PatchMapping("/{diaryId}")
@PatchMapping(value = "/{diaryId}", consumes = APPLICATION_JSON_VALUE)
public ApplicationResponse<Long> updateDiary(@Parameter(hidden = true) UserEntity user, @PathVariable("diaryId") Long diaryId, @RequestBody DiaryUpdateRequest request) {
diaryService.updateDiary(user.getId(), diaryId, request);
return ApplicationResponse.success(diaryId, "다이어리가 수정되었습니다.");
}

/**
* 다이어리 수정
*/
@PatchMapping(value = "/{diaryId}", consumes = MULTIPART_FORM_DATA_VALUE)
public ApplicationResponse<Long> updateForm(@Parameter(hidden = true) UserEntity user, @PathVariable("diaryId") Long diaryId, @ModelAttribute DiaryUpdateRequest request) {
var diary = diaryService.findDiaryById(diaryId);
createDiaryImageFile(user, request, diary.getDate().format(formatter));
diaryService.updateDiary(user.getId(), diaryId, request);
return ApplicationResponse.success(diaryId, "다이어리가 수정되었습니다.");
}

/**
* 다이어리 삭제
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package picdiary.diary.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import picdiary.diary.domain.Diary;

@Data
@Builder
public final class DiaryCreateRequest {
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class DiaryCreateRequest extends DiaryUpdateRequest {
private String content;
private String date;
private Diary.Emotion emotion;
private String imageFileName;

}
16 changes: 15 additions & 1 deletion src/main/java/picdiary/diary/dto/request/DiaryUpdateRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package picdiary.diary.dto.request;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;
import picdiary.diary.domain.Diary;

public record DiaryUpdateRequest(String content, Diary.Emotion emotion) {
@Data
@NoArgsConstructor
public class DiaryUpdateRequest {
private String content;
protected Diary.Emotion emotion;
private MultipartFile imageFile;
private String imageFileName;

public DiaryUpdateRequest(String content, Diary.Emotion emotion) {
this.content = content;
this.emotion = emotion;
}
}
2 changes: 1 addition & 1 deletion src/main/java/picdiary/diary/exception/DiaryErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public enum DiaryErrorCode implements ErrorCode {
IS_NOT_WRITER(HttpStatus.FORBIDDEN, "권한이 없습니다."),
NO_DIARY(HttpStatus.BAD_REQUEST, "일기 정보가 존재하지 않습니다."),
EMPTY_DIARY(HttpStatus.BAD_REQUEST, "내용이 비어있습니다.");
SAVE_ERROR(HttpStatus.BAD_REQUEST, "파일 저장 중 오류가 발생했습니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/picdiary/diary/repository/DiaryEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import picdiary.diary.domain.Diary;
import picdiary.diary.dto.request.DiaryUpdateRequest;
import picdiary.diary.exception.DiaryErrorCode;
import picdiary.global.exception.ApplicationException;
import picdiary.global.repository.BaseEntity;
Expand Down Expand Up @@ -51,10 +52,17 @@ public DiaryEntity(String content, LocalDate date, Diary.Emotion emotion, String
this.user = user;
}

public void diaryUpdate(long userId, String content, Diary.Emotion emotion) {
public void diaryUpdate(long userId, DiaryUpdateRequest request) {
validateUserIsWriter(userId);
this.content = content;
this.emotion = emotion;
if (request.getContent() != null) {
this.content = request.getContent();
}
if (request.getEmotion() != null) {
this.emotion = request.getEmotion();
}
if (request.getImageFileName() != null) {
this.imageFileName = request.getImageFileName();
}
}

public void validateUserIsWriter(long userId) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/picdiary/diary/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Collection<DiaryEntity> getDiaryByMonth(Long userId, String month) {
public DiaryEntity updateDiary(Long userId, Long diaryId, DiaryUpdateRequest request) {
DiaryEntity savedDiary = findDiaryById(diaryId);

savedDiary.diaryUpdate(userId, request.content(), request.emotion());
savedDiary.diaryUpdate(userId, request);
return diaryRepository.save(savedDiary);
}

Expand Down

0 comments on commit 60720fd

Please sign in to comment.