Skip to content

Commit

Permalink
feat: 티어별 게시판을 하나의 Post에서 관리 (#5)
Browse files Browse the repository at this point in the history
Post 필드에 티어정보를 추가. 생성시 티어정보의 필드가 필수로 넘어오도록 설정
  • Loading branch information
toychip committed Dec 19, 2023
1 parent 93fcc8a commit faf6d8e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand Down Expand Up @@ -54,8 +55,8 @@ public class PostApiController {
schema = @Schema(implementation = ErrorType.class)))
})
@PostMapping("/post")
public SuccessResponse<PostResponse> save(final @ModelAttribute PostCreateRequest request) {
PostResponse postResponse = postService.save(request);
public SuccessResponse<PostResponse> register(final @Validated @ModelAttribute PostCreateRequest request) {
PostResponse postResponse = postService.register(request);
return new SuccessResponse<>(postResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static Post createDtoToEntity(final PostCreateRequest request, final Memb
return Post.builder()
.title(request.getTitle())
.member(currentMember)
.writer(request.getWriter())
.writer(currentMember.getGitName())
.postTier(request.getPostTier())
.build();
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/api/TaveShot/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down Expand Up @@ -36,6 +38,9 @@ public class Post extends BaseEntity {
@Column(columnDefinition = "TEXT", nullable = false)
private String content;

@Enumerated(EnumType.STRING)
private PostTier postTier;

/**
* image uri 로 변경
*/
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/api/TaveShot/domain/post/domain/PostTier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.api.TaveShot.domain.post.domain;

import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;

public enum PostTier {
BRONZE_SILVER("BronzeSilver"),
GOLD("Gold"),
PLATINUM("Platinum"),
HIGH("High");

private final String tier;

PostTier(String tier) {
this.tier = tier;
}

// 문자열을 enum으로 변환하는 메서드
public static PostTier findTier(String input) {
for (PostTier post : PostTier.values()) {
if (post.tier.equalsIgnoreCase(input)) {
return post;
}
}
throw new ApiException(ErrorType._POST_INVALID_TIER);
}

public static boolean isHighTier(PostTier tier) {
return tier == HIGH;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package com.api.TaveShot.domain.post.dto.request;

import com.api.TaveShot.domain.post.domain.PostTier;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

/** 게시글의 등록, 수정을 처리할 Request 클래스 **/
@Getter
@Builder
@AllArgsConstructor
public class PostCreateRequest {

@Schema(description = "게시글 제목", example = "게시글 제목 예시")
@NotEmpty
private String title;

@Schema(description = "게시글 내용", example = "게시글 내용 예시")
@NotEmpty
private String content;

@Schema(description = "게시글 작성자", example = "작성자 예시")

This comment has been minimized.

Copy link
@wjd4204

wjd4204 Dec 19, 2023

Member

작성자는 member에서 가져오기 때문에 삭제한 건가요?

This comment has been minimized.

Copy link
@toychip

toychip Dec 20, 2023

Author Member

request에서 매번 작성자를 보내는 것이 아닌, 백단에서 작성자를 추가하기 위함입니다.

private String writer;
@Schema(description = "게시판 등급", example = "BronzeSilver or Gold or Platinum or High")
@NotEmpty
private String postTier;

@Schema(description = "게시글 첨부 파일", type = "string", format = "binary")
private MultipartFile attachmentFile;

public PostTier getPostTier() {
// 문자열을 enum으로 변환
return PostTier.findTier(postTier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.api.TaveShot.domain.post.domain.Post;
import com.api.TaveShot.domain.post.dto.request.PostCreateRequest;
import com.api.TaveShot.domain.post.dto.request.PostEditRequest;
import com.api.TaveShot.domain.post.dto.request.PostSearchCondition;
import com.api.TaveShot.domain.post.dto.response.PostListResponse;
import com.api.TaveShot.domain.post.dto.response.PostResponse;
import com.api.TaveShot.domain.post.dto.request.PostSearchCondition;
import com.api.TaveShot.domain.post.editor.PostEditor;
import com.api.TaveShot.domain.post.repository.PostRepository;
import com.api.TaveShot.global.exception.ApiException;
Expand All @@ -31,13 +31,15 @@ public class PostService {

/* --------------------------------- CREATE --------------------------------- */
@Transactional
public PostResponse save(final PostCreateRequest request) {

public PostResponse register(final PostCreateRequest request) {
Member currentMember = getCurrentMember();
Post post = PostConverter.createDtoToEntity(request, currentMember);
postRepository.save(post);
PostResponse postResponse = PostConverter.entityToResponse(post);
return postResponse;
return postResponse(post);
}

private PostResponse postResponse(Post post) {
return PostConverter.entityToResponse(post);
}

private Member getCurrentMember() {
Expand All @@ -48,8 +50,7 @@ private Member getCurrentMember() {
/* --------------------------------- READ Single --------------------------------- */
public PostResponse getSinglePost(final Long postId) {
Post post = getPost(postId);
PostResponse postResponse = PostConverter.entityToResponse(post);
return postResponse;
return postResponse(post);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum ErrorType {


// ------------------------------------------ POST ------------------------------------------
_POST_INVALID_TIER(BAD_REQUEST, "POST_4000", "게시글의 등급이 잘못되었습니다."),
_POST_NOT_FOUND(NOT_FOUND, "POST_4040", "해당 게시글이 존재하지 않습니다."),

// ------------------------------------------ USER ------------------------------------------
Expand Down

2 comments on commit faf6d8e

@wjd4204
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

converter class를 이용하기 보단 service class에서 처리하는 게 더 효율적이지 않을까요?

@toychip
Copy link
Member Author

@toychip toychip commented on faf6d8e Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단일 책임 원칙을 적용하였습니다. Service에서는 비즈니스 로직만을 책임지고, Converter는 Dto-> Entity, Entity -> Dto, Dto -> Response 와 같이, 바꾸는 책임만을 갖기 위해 분리한 것입니다.

Please sign in to comment.