Skip to content

Commit

Permalink
refactor(PostCrudController): 포토카드 저장안됨 이슈 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jiminseon committed Dec 6, 2023
1 parent 98fa209 commit a8d090b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.myadd.myadd.post.crud.controller;

import com.myadd.myadd.fileUpload.service.FileUploadService;
import com.myadd.myadd.post.crud.dto.PostBackDto;
import com.myadd.myadd.post.crud.service.PostCrudService;
import com.myadd.myadd.post.domain.PostEntity;
Expand All @@ -8,6 +9,7 @@
import com.myadd.myadd.user.security.service.PrincipalDetails;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -23,17 +25,20 @@
public class PostCrudController {

private final PostCrudService postCrudService;

@Autowired
private final FileUploadService fileUploadService;
//포토카드 작성 multipartFile 사용시 RequestPart 사용해야함.
@PostMapping(value = "/posts/add-post",consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public BaseResponse<PostBackDto> create(@Valid @RequestPart PostBackDto post, @RequestPart(value = "image", required = false)MultipartFile image) throws IOException {
@PostMapping(value = "/posts/add-post",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public BaseResponse<PostBackDto> create(@RequestPart PostBackDto post, @RequestPart(value = "image", required = false)MultipartFile image) throws IOException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if(authentication == null)
return new BaseResponse<>(BaseResponseStatus.FAILED_NOT_AUTHENTICATION);

Long id = ((PrincipalDetails) authentication.getPrincipal()).getId();
postCrudService.savePost(post, image, id);

String S3FileName = fileUploadService.upload(image);
postCrudService.savePost(post, S3FileName, id);

return new BaseResponse<>(post, BaseResponseStatus.SUCCESS_CREATE_POST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.IOException;

@Service
Expand All @@ -20,13 +19,13 @@ public class PostCrudService {
private final PostCrudRepository postCrudRepository;
private final FileUploadService fileUploadService;

public PostEntity savePost(PostBackDto postDto, MultipartFile image, Long id) throws IOException {
public String savePost(PostBackDto postDto, String image, Long id) throws IOException {
if(!image.isEmpty()) {
String storedFileName = fileUploadService.upload(image);
postDto.setImage(storedFileName);
postDto.setImage(image);
}
postDto.setUserId(id);
return postCrudRepository.save(postDto.toPostEntity(postDto));
postCrudRepository.save(postDto.toPostEntity(postDto));
return "Success: save post";
}


Expand Down

0 comments on commit a8d090b

Please sign in to comment.