Skip to content

Commit

Permalink
refactor-requestpart대신 modelattribute (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
jms0324 authored May 15, 2024
1 parent fc6e4df commit c59d21f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ private File convertMultiPartToFile(MultipartFile file) throws IOException {
}
return convertedFile;
}
public void deleteFileFromS3(String fileUrl) {
// URL에서 파일 이름 추출
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);

// 파일 삭제
amazonS3Client.deleteObject(bucketName, fileName);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.kau.kkoolbeeServer.domain.advice.dto.AdviceResponseDto;
import org.kau.kkoolbeeServer.domain.diary.Diary;
import org.kau.kkoolbeeServer.domain.diary.Feeling;
import org.kau.kkoolbeeServer.domain.diary.dto.request.DiaryUpdateRequestDto;
import org.kau.kkoolbeeServer.domain.diary.dto.request.FeelingListRequestDto;
import org.kau.kkoolbeeServer.domain.diary.dto.response.*;
import org.kau.kkoolbeeServer.domain.diary.dto.request.CurrentDateRequestDto;
Expand Down Expand Up @@ -260,7 +261,7 @@ public ResponseEntity<ApiResponse<?>> createSlowTypeDiary(@RequestHeader(value =
}
}

@PatchMapping("/api/diary/update")
/* @PatchMapping("/api/diary/update")
public ResponseEntity<?> updateDiary(
@RequestHeader(value = "Authorization") String authHeader,
@RequestPart(value = "imageUrl", required = false) MultipartFile imageFile,
Expand Down Expand Up @@ -319,9 +320,65 @@ public ResponseEntity<?> updateDiary(
}*/
@PatchMapping("/api/diary/update")
public ResponseEntity<?> updateDiary(
@RequestHeader(value = "Authorization") String authHeader,
@ModelAttribute DiaryUpdateRequestDto requestDto ){
Long diaryId= requestDto.getDiaryId();
MultipartFile imageFile= requestDto.getImageUrl();
String diaryTitle= requestDto.getDiaryTitle();
String diaryContent= requestDto.getDiaryContent();



try{

String accessToken = null;
if (authHeader != null && authHeader.startsWith("Bearer ")) {
accessToken = authHeader.substring(7);
Long memberID= jwtProvider.getUserFromJwt(accessToken);
Diary diary=diaryService.findDiaryById(requestDto.getDiaryId()).orElseThrow(()->new NoSuchElementException("해당 ID의 일기를 찾을 수 없습니다."));
if(diary.getMember().getId()!=memberID){

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ErrorType.NOT_YOUR_DIARY));
}
}
else{
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ErrorType.INVALID_HTTP_REQUEST_ERROR));

}
String imageUrl=null;
if (imageFile!=null && !imageFile.isEmpty()){

imageUrl=s3UploaderService.upload(imageFile);

UpdateDiaryResponseDto responseDto=diaryService.updateDiary(diaryId,diaryContent,diaryTitle,imageUrl);
return ResponseEntity.ok().body(ApiResponse.success(SuccessType.PROCESS_SUCCESSED,responseDto));

}
else{
UpdateDiaryResponseDto responseDto=diaryService.updateDiaryWithoutImage(diaryId,diaryContent,diaryTitle);
return ResponseEntity.ok().body(ApiResponse.success(SuccessType.PROCESS_SUCCESSED,responseDto));


}


}
catch (Exception e){
logger.error("An error occurred while updating diary", e);
e.printStackTrace();

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.error(ErrorType.INTERNAL_SERVER_ERROR,e.getMessage()));
}




}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kau.kkoolbeeServer.domain.diary.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;

@Getter
@Setter
@AllArgsConstructor
public class DiaryUpdateRequestDto {
private MultipartFile imageUrl;
private Long diaryId;
private String diaryTitle;
private String diaryContent;


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kau.kkoolbeeServer.domain.diary.service;

import org.kau.kkoolbeeServer.S3.S3UploaderService;
import org.kau.kkoolbeeServer.domain.diary.Diary;
import org.kau.kkoolbeeServer.domain.diary.Feeling;
import org.kau.kkoolbeeServer.domain.diary.dto.response.UpdateDiaryResponseDto;
Expand All @@ -20,10 +21,12 @@
public class DiaryService {

private DiaryRepository diaryRepository;
private S3UploaderService s3UploaderService;

@Autowired
public DiaryService(DiaryRepository diaryRepository) {
public DiaryService(DiaryRepository diaryRepository,S3UploaderService s3UploaderService) {
this.diaryRepository = diaryRepository;
this.s3UploaderService=s3UploaderService;
}

public Optional<Diary> findDiaryById(Long diary_id){
Expand Down Expand Up @@ -60,6 +63,10 @@ public Diary saveDiary(Diary diary){

public UpdateDiaryResponseDto updateDiary(Long diaryId,String diaryContent,String diaryTitle,String imageUrl){
Diary diary=findDiaryById(diaryId).orElseThrow(()->new NoSuchElementException("해당 ID의 일기를 찾을 수 없습니다."));
if(diary.getImageurl()!=null){
s3UploaderService.deleteFileFromS3(diary.getImageurl());

}
diary.setImageurl(imageUrl);
if(!diary.getContent().equals(diaryContent)){
diary.setFeeling(null);
Expand Down

0 comments on commit c59d21f

Please sign in to comment.