Skip to content

Commit

Permalink
feat: 게시글 이미지 업로드 및 수정 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhacandy committed Sep 10, 2024
1 parent b70e086 commit 901fe3e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -38,10 +40,10 @@ public class PostController {

@Operation(summary = "게시글 등록", description = "게시글 등록을 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@PostMapping("")
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Response<?> registerPost(@Valid @RequestBody PostRequest request,
@AuthenticationPrincipal Long memberId) {
public Response<?> registerPost(@RequestPart("postRequest") @Valid PostRequest request,
@AuthenticationPrincipal Long memberId) throws IOException {
log.info("게시글 등록한 memberId: {}", memberId);
postService.registerPost(request, memberId);
return Response.createSuccessWithNoData("포스트 생성 완료");
Expand Down Expand Up @@ -87,11 +89,11 @@ public Response<?> deletePost(@PathVariable Long postId,

@Operation(summary = "게시글 수정", description = "게시글 수정을 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@PostMapping("/{postId}/update")
@PostMapping(value = "/{postId}/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Response<?> registerPost(@PathVariable Long postId,
@RequestBody PostRequest request,
@AuthenticationPrincipal Long memberId) {
@RequestPart("postRequest") @Valid PostRequest request,
@AuthenticationPrincipal Long memberId) throws IOException {
log.info("게시글 {} 수정한 memberId: {}", postId, memberId);
postService.updatePost(postId, request,memberId);
return Response.createSuccessWithNoData("포스트 수정 완료");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cotato.growingpain.post.PostCategory;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import org.springframework.web.multipart.MultipartFile;

public record PostRequest(

Expand All @@ -14,7 +15,7 @@ public record PostRequest(
@Size(max = 3000)
String content,

String imageUrl,
MultipartFile postImage,

PostCategory category
){
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/cotato/growingpain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import cotato.growingpain.post.repository.PostLikeRepository;
import cotato.growingpain.post.repository.PostRepository;
import cotato.growingpain.replycomment.repository.ReplyCommentRepository;
import cotato.growingpain.s3.S3Uploader;
import java.io.IOException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
Expand All @@ -28,13 +31,21 @@ public class PostService {
private final MemberRepository memberRepository;
private final CommentRepository commentRepository;
private final ReplyCommentRepository replyCommentRepository;
private final S3Uploader s3Uploader;

@Transactional
public void registerPost(PostRequest request, Long memberId) {
public void registerPost(PostRequest request, Long memberId) throws IOException {
Member member = memberRepository.getReferenceById(memberId);
PostCategory parentCategory = request.category().getParent();

String imageUrl = null;
MultipartFile imageFile = request.postImage(); // postImage를 가져옴
if (imageFile != null && !imageFile.isEmpty()) { // null 체크 추가
imageUrl = s3Uploader.uploadFileToS3(imageFile, "post");
}

postRepository.save(
Post.of(member, request.title(), request.content(), request.imageUrl(), parentCategory,
Post.of(member, request.title(), request.content(), imageUrl, parentCategory,
request.category())
);
}
Expand Down Expand Up @@ -72,14 +83,20 @@ public void deletePost(Long postId, Long memberId) {
}

@Transactional
public void updatePost(Long postId, PostRequest request, Long memberId) {
public void updatePost(Long postId, PostRequest request, Long memberId) throws IOException {
Post post = findByPostIdAndMemberId(postId, memberId);

if (post.isDeleted()) {
throw new AppException(ErrorCode.ALREADY_DELETED);
}

post.updatePost(request.title(), request.content(), request.imageUrl(), request.category());
String imageUrl = null;
MultipartFile imageFile = request.postImage(); // postImage를 가져옴
if (imageFile != null && !imageFile.isEmpty()) { // null 체크 추가
imageUrl = s3Uploader.uploadFileToS3(imageFile, "post");
}

post.updatePost(request.title(), request.content(), imageUrl, request.category());
postRepository.save(post);
}

Expand Down

0 comments on commit 901fe3e

Please sign in to comment.