Skip to content

Commit

Permalink
[fix] #63 post api enum code 적용 후 enum dto로 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuKwanKon committed Jan 24, 2024
1 parent 80eeb31 commit e6428d2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.PostDetailResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto;
Expand Down Expand Up @@ -58,7 +57,7 @@ public ResponseEntity<SuccessResponse<?>> getMyPosts(@UserId final Long userId,
@PatchMapping("/{id}")
public ResponseEntity<SuccessResponse<?>> updateMyPost(@UserId final Long userId,
@PathVariable("id") final Long postId,
@RequestBody @Valid final PostUpdateRequestDto requestDto) {
@RequestBody @Valid final PostRequestDto requestDto) {
PostUpdateResponseDto responseDto = postService.updateMyPost(userId, postId, requestDto);
return SuccessResponse.ok(responseDto);
}
Expand All @@ -73,7 +72,7 @@ public ResponseEntity<SuccessResponse<?>> updatePostState(@UserId final Long use

@PostMapping
public ResponseEntity<SuccessResponse<?>> createPost(@UserId final Long userId,
@RequestBody @Valid final PostCreateRequestDto requestDto) {
@RequestBody @Valid final PostRequestDto requestDto) {
postService.createPost(userId, requestDto);
return SuccessResponse.created(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.gachon.checkmate.domain.post.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.gachon.checkmate.domain.checkList.dto.request.CheckListRequestDto;
import org.gachon.checkmate.domain.post.dto.support.PostEnumDto;
import org.gachon.checkmate.domain.post.entity.DormitoryType;
import org.gachon.checkmate.domain.post.entity.ImportantKeyType;
import org.gachon.checkmate.domain.post.entity.RoomType;
import org.gachon.checkmate.domain.post.entity.SimilarityKeyType;

import java.time.LocalDate;

import static org.gachon.checkmate.global.utils.EnumValueUtils.toEntityCode;

public record PostRequestDto(
@NotBlank(message = "제목을 입력해주세요") String title,
@NotBlank(message = "내용을 입력해주세요") String content,
@NotNull(message = "중요 키워드를 입력해주세요") String importantKey,
@NotNull(message = "유사도를 입력해주세요") String similarityKey,
@NotNull(message = "호실을 입력해주세요") String roomType,
@NotNull(message = "기숙사 유형을 입력해주세요") String dormitoryType,
@NotNull(message = "모집 마감기간을 입력해주세요") LocalDate endDate,
@NotNull(message = "체크리스트를 입력해주세요") CheckListRequestDto checkList
) {
public static PostEnumDto toEnumDto(PostRequestDto requestDto) {
return PostEnumDto.builder()
.title(requestDto.title())
.content(requestDto.content())
.importantKey(toEntityCode(ImportantKeyType.class, requestDto.importantKey()))
.similarityKey(toEntityCode(SimilarityKeyType.class, requestDto.similarityKey()))
.roomType(toEntityCode(RoomType.class, requestDto.roomType()))
.dormitoryType(toEntityCode(DormitoryType.class, requestDto.dormitoryType()))
.endDate(requestDto.endDate())
.checkList(CheckListRequestDto.toEnumDto(requestDto.checkList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.gachon.checkmate.domain.post.dto.support;

import lombok.Builder;
import org.gachon.checkmate.domain.checkList.dto.support.CheckListEnumDto;
import org.gachon.checkmate.domain.post.entity.DormitoryType;
import org.gachon.checkmate.domain.post.entity.ImportantKeyType;
import org.gachon.checkmate.domain.post.entity.RoomType;
import org.gachon.checkmate.domain.post.entity.SimilarityKeyType;

import java.time.LocalDate;

@Builder
public record PostEnumDto(
String title,
String content,
ImportantKeyType importantKey,
SimilarityKeyType similarityKey,
RoomType roomType,
DormitoryType dormitoryType,
LocalDate endDate,
CheckListEnumDto checkList
) {
}
22 changes: 11 additions & 11 deletions src/main/java/org/gachon/checkmate/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.gachon.checkmate.domain.checkList.entity.PostCheckList;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.post.converter.*;
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.support.PostEnumDto;
import org.gachon.checkmate.domain.scrap.entity.Scrap;
import org.gachon.checkmate.global.common.BaseTimeEntity;

Expand Down Expand Up @@ -47,16 +47,16 @@ public class Post extends BaseTimeEntity {
@Builder.Default
private List<Scrap> scrapList = new ArrayList<>();

public static Post createPost(PostCreateRequestDto postCreateRequestDto, User user) {
public static Post createPost(PostEnumDto postRequestDto, User user) {
Post post = Post.builder()
.title(postCreateRequestDto.title())
.content(postCreateRequestDto.content())
.endDate(postCreateRequestDto.endDate())
.title(postRequestDto.title())
.content(postRequestDto.content())
.endDate(postRequestDto.endDate())
.postState(PostState.RECRUITING)
.roomType(postCreateRequestDto.roomType())
.dormitoryType(postCreateRequestDto.dormitoryType())
.importantKeyType(postCreateRequestDto.importantKey())
.similarityKeyType(postCreateRequestDto.similarityKey())
.roomType(postRequestDto.roomType())
.dormitoryType(postRequestDto.dormitoryType())
.importantKeyType(postRequestDto.importantKey())
.similarityKeyType(postRequestDto.similarityKey())
.user(user)
.build();
user.addPost(post);
Expand All @@ -71,7 +71,7 @@ public void addScrap(Scrap scrap) {
this.scrapList.add(scrap);
}

public void updatePost(PostUpdateRequestDto postUpdateRequestDto) {
public void updatePost(PostEnumDto postUpdateRequestDto) {
this.title = postUpdateRequestDto.title();
this.content = postUpdateRequestDto.content();
this.importantKeyType = postUpdateRequestDto.importantKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.checkList.dto.request.CheckListRequestDto;
import org.gachon.checkmate.domain.checkList.dto.response.CheckListResponseDto;
import org.gachon.checkmate.domain.checkList.dto.support.CheckListEnumDto;
import org.gachon.checkmate.domain.checkList.entity.CheckList;
import org.gachon.checkmate.domain.checkList.entity.PostCheckList;
import org.gachon.checkmate.domain.checkList.repository.CheckListRepository;
import org.gachon.checkmate.domain.checkList.repository.PostCheckListRepository;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.member.repository.UserRepository;
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.*;
import org.gachon.checkmate.domain.post.dto.support.PostDetailDto;
import org.gachon.checkmate.domain.post.dto.support.PostPagingSearchCondition;
import org.gachon.checkmate.domain.post.dto.support.PostSearchCondition;
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;
import org.gachon.checkmate.domain.post.dto.support.*;
import org.gachon.checkmate.domain.post.entity.Post;
import org.gachon.checkmate.domain.post.repository.PostRepository;
import org.gachon.checkmate.domain.post.utils.PostSortingUtils;
Expand Down Expand Up @@ -49,12 +46,13 @@ public class PostService {
private final PostCheckListRepository postCheckListRepository;
private final ScrapRepository scrapRepository;

public void createPost(Long userId, PostCreateRequestDto requestDto) {
validateDuplicateTitle(requestDto.title());
validateAvailableEndDate(requestDto.endDate());
public void createPost(Long userId, PostRequestDto requestDto) {
PostEnumDto postEnumDto = PostRequestDto.toEnumDto(requestDto);
validateDuplicateTitle(postEnumDto.title());
validateAvailableEndDate(postEnumDto.endDate());
User user = getUserOrThrow(userId);
Post post = createPostAndSave(requestDto, user);
createPostCheckListAndSave(requestDto.checkList(), post);
Post post = createPostAndSave(postEnumDto, user);
createPostCheckListAndSave(postEnumDto.checkList(), post);
}

public PostSearchResponseDto getMyPosts(Long userId, Pageable pageable) {
Expand Down Expand Up @@ -91,12 +89,13 @@ public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable p
return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

public PostUpdateResponseDto updateMyPost(Long userId, Long postId, PostUpdateRequestDto requestDto) {
public PostUpdateResponseDto updateMyPost(Long userId, Long postId, PostRequestDto requestDto) {
User user = getUserOrThrow(userId);
Post post = getPostOrThrow(postId);
PostEnumDto postEnumDto = PostRequestDto.toEnumDto(requestDto);
validatePostWriter(user, post);
validateAvailableEndDate(requestDto.endDate());
post.updatePost(requestDto);
validateAvailableEndDate(postEnumDto.endDate());
post.updatePost(postEnumDto);
CheckListResponseDto checkListResponseDto = CheckListResponseDto.ofPostCheckList(post.getPostCheckList());
return PostUpdateResponseDto.of(post, checkListResponseDto);
}
Expand Down Expand Up @@ -140,13 +139,13 @@ private void validateAvailableEndDate(LocalDate endDate) {
throw new InvalidValueException(INVALID_POST_DATE);
}

private Post createPostAndSave(PostCreateRequestDto postCreateRequestDto, User user) {
Post post = Post.createPost(postCreateRequestDto, user);
private Post createPostAndSave(PostEnumDto postRequestDto, User user) {
Post post = Post.createPost(postRequestDto, user);
postRepository.save(post);
return post;
}

private void createPostCheckListAndSave(CheckListRequestDto checkListRequestDto, Post post) {
private void createPostCheckListAndSave(CheckListEnumDto checkListRequestDto, Post post) {
PostCheckList postCheckList = PostCheckList.createPostCheckList(checkListRequestDto, post);
postCheckListRepository.save(postCheckList);
}
Expand Down

0 comments on commit e6428d2

Please sign in to comment.