Skip to content

Commit

Permalink
Merge pull request #36 from IT-Cotato/feat/21-post
Browse files Browse the repository at this point in the history
[REFACTOR] Post Create 로직 수정 + Post 엔티티 수정
  • Loading branch information
goalSetter09 authored Jan 19, 2025
2 parents 21e16ff + ce1ecd0 commit af027f1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -41,6 +40,7 @@ public ResponseEntity<DataResponse<PostCreateResponse>> createPost(
request.title(),
request.content(),
request.postCategory(),
request.anonymity(),
images
)
)
Expand All @@ -51,6 +51,7 @@ public ResponseEntity<DataResponse<PostCreateResponse>> createPost(
public ResponseEntity<DataResponse<PostDeleteResponse>> deletePost(
@PathVariable Long postId
){

return ResponseEntity.ok(DataResponse.from(
PostDeleteResponse.of(
postService.deletePost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public class PostAppender {
private final PostRepository postRepository;

@Transactional
public Long append(Long boardId, String title, String content, String postCategory){
public Long append(
Long boardId,
String title,
String content,
String postCategory,
Anonymity anonymity
){

Long userId = apiUserResolver.getUserId();

Post post = Post.builder()
Expand All @@ -31,9 +38,10 @@ public Long append(Long boardId, String title, String content, String postCatego
.content(content)
.likes(0L)
.scraps(0L)
.anonymity(Anonymity.ANONYMOUS)
.anonymity(anonymity)
.postStatus(PostStatus.PUBLISHED)
.postCategory(postCategory)
.nextAnonymousNumber(1L)
.build();

return postRepository.save(post).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
public class PostFinder {
private final PostRepository postRepository;

public Post getById(Long postId){
public Post getPost(Long postId){

return postRepository.findById(postId)
.orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND));
}

public Long getAuthorId(Long postId){
Post post = postRepository.findById(postId)
.orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND));

return post.getUserId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cotato.kampus.domain.common.application.ApiUserResolver;
import com.cotato.kampus.domain.common.enums.Anonymity;
import com.cotato.kampus.domain.product.application.PostDeleter;
import com.cotato.kampus.global.error.ErrorCode;
import com.cotato.kampus.global.error.exception.AppException;
import com.cotato.kampus.global.error.exception.ImageException;
import com.cotato.kampus.global.util.s3.S3Uploader;

Expand All @@ -21,30 +25,50 @@ public class PostService {
private final PostDeleter postDeleter;
private final PostImageAppender postImageAppender;
private static final String PRODUCT_IMAGE_FOLDER = "post";
private final ApiUserResolver apiUserResolver;
private final PostFinder postFinder;

@Transactional
public Long createPost(
Long boardId,
String title,
String content,
String postCategory,
Anonymity anonymity,
List<MultipartFile> images
) throws ImageException {
// s3에 이미지 업로드
List<String> imageUrls = (images == null || images.isEmpty()) ?
List.of() : s3Uploader.uploadFiles(images, PRODUCT_IMAGE_FOLDER);
List.of() :
s3Uploader.uploadFiles(
images.stream()
.filter(image -> image != null && image.getOriginalFilename() != null && !image.getOriginalFilename().isEmpty())
.toList(),
PRODUCT_IMAGE_FOLDER
);

// 게시글 추가
Long postId = postAppender.append(boardId, title, content, postCategory);
Long postId = postAppender.append(boardId, title, content, postCategory, anonymity);

// 게시글 이미지 추가
postImageAppender.appendAll(postId, imageUrls);
if(!imageUrls.isEmpty()) {
postImageAppender.appendAll(postId, imageUrls);
}

return postId;
}

@Transactional
public Long deletePost(Long postId){
// 작성자 검증: 현재 사용자가 게시글 작성자인지 확인
Long userId = apiUserResolver.getUserId();
Long authorId = postFinder.getAuthorId(postId);

// 작성자가 아닌 경우 예외 처리
if(userId != authorId)
throw new AppException(ErrorCode.POST_NOT_AUTHOR);

// 게시글 삭제
postDeleter.delete(postId);

return postId;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/cotato/kampus/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ public class Post extends BaseTimeEntity {
@Column(name = "post_category", nullable = false)
private String postCategory;

@Column(name = "next_ananymous_number", nullable = false)
private Long nextAnonymousNumber = 1L;

@Builder
public Post(Long userId, Long boardId, String title, String content,
Long likes, Long scraps, Anonymity anonymity,
PostStatus postStatus, String postCategory) {
PostStatus postStatus, String postCategory, Long nextAnonymousNumber) {
this.userId = userId;
this.boardId = boardId;
this.title = title;
Expand All @@ -70,5 +73,6 @@ public Post(Long userId, Long boardId, String title, String content,
this.anonymity = anonymity;
this.postStatus = postStatus;
this.postCategory = postCategory;
this.nextAnonymousNumber = nextAnonymousNumber;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.cotato.kampus.domain.post.dto.request;

import com.cotato.kampus.domain.common.enums.Anonymity;

public record PostCreateRequest(
Long boardId,
String title,
String content,
String postCategory
String postCategory,
Anonymity anonymity
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ public class PostDeleter {
private final PostFinder postFinder;

public void delete(Long postId){
User user = apiUserResolver.getUser();

Post post = postFinder.getById(postId);

if(post.getUserId() != user.getId()){
throw new AppException(ErrorCode.POST_NOT_AUTHOR);
}
Post post = postFinder.getPost(postId);
postRepository.delete(post);
}
}

0 comments on commit af027f1

Please sign in to comment.