Skip to content

Commit

Permalink
[feat] #5 카테고리 중복 저장 방지 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SunwoongH committed Nov 9, 2023
1 parent 0e069b5 commit f914b05
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
31 changes: 15 additions & 16 deletions api/src/main/java/org/sopt/api/post/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.sopt.api.post.service;

import lombok.RequiredArgsConstructor;
import org.sopt.common.error.ConflictException;
import org.sopt.common.error.ErrorStatus;
import org.sopt.api.post.dto.request.PostSaveOrUpdateRequest;
import org.sopt.api.post.dto.response.PostGetResponse;
import org.sopt.api.post.dto.response.PostSaveResponse;
import org.sopt.common.error.EntityNotFoundException;
import org.sopt.domain.member.domain.Member;
import org.sopt.domain.member.repository.MemberRepository;
import org.sopt.domain.post.domain.Category;
import org.sopt.domain.post.domain.Post;
import org.sopt.api.post.dto.request.PostSaveOrUpdateRequest;
import org.sopt.api.post.dto.response.PostGetResponse;
import org.sopt.api.post.dto.response.PostSaveResponse;

import org.sopt.domain.post.repository.CategoryRepository;
import org.sopt.domain.post.repository.PostRepository;
import org.springframework.data.domain.Pageable;
Expand All @@ -33,8 +31,7 @@ public class PostService {
@Transactional
public PostSaveResponse savePost(Long memberId, PostSaveOrUpdateRequest postSaveOrUpdateRequest) {
Member findMember = findMember(memberId);
validateDuplicateCategory(postSaveOrUpdateRequest.categoryContent());
Category savedCategory = createCategoryAndGetSavedCategory(postSaveOrUpdateRequest);
Category savedCategory = createCategoryAndGetSavedCategoryOrGetCategory(postSaveOrUpdateRequest);
Post savedPost = createPostAndGetSavedPost(postSaveOrUpdateRequest, findMember, savedCategory);
return PostSaveResponse.of(savedPost);
}
Expand Down Expand Up @@ -67,17 +64,15 @@ private Member findMember(Long memberId) {
return memberRepository.findByIdOrThrow(memberId);
}

private void validateDuplicateCategory(String content) {
if (categoryRepository.existsByContent(content)) {
throw new ConflictException(ErrorStatus.DUPLICATE_CATEGORY);
private Category createCategoryAndGetSavedCategoryOrGetCategory(PostSaveOrUpdateRequest postSaveOrUpdateRequest) {
try {
return findCategory(postSaveOrUpdateRequest.categoryContent());
} catch (EntityNotFoundException e) {
Category category = createCategory(postSaveOrUpdateRequest.categoryContent());
return categoryRepository.save(category);
}
}

private Category createCategoryAndGetSavedCategory(PostSaveOrUpdateRequest postSaveOrUpdateRequest) {
Category category = createCategory(postSaveOrUpdateRequest.categoryContent());
return categoryRepository.save(category);
}

private Post createPostAndGetSavedPost(PostSaveOrUpdateRequest postSaveOrUpdateRequest, Member member, Category category) {
Post post = createPost(postSaveOrUpdateRequest.title(), postSaveOrUpdateRequest.postContent(), category, member);
return postRepository.save(post);
Expand All @@ -91,4 +86,8 @@ private void updatePostAndCategory(PostSaveOrUpdateRequest postSaveOrUpdateReque
post.updateTitleAndContent(postSaveOrUpdateRequest.title(), postSaveOrUpdateRequest.postContent());
category.updateContent(postSaveOrUpdateRequest.categoryContent());
}

private Category findCategory(String content) {
return categoryRepository.findByContentOrThrow(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ErrorStatus {
ENTITY_NOT_FOUND(HttpStatus.NOT_FOUND, "대상을 찾을 수 없습니다."),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "회원을 찾을 수 없습니다."),
POST_NOT_FOUND(HttpStatus.NOT_FOUND, "게시물을 찾을 수 없습니다."),
CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, "카테고리를 찾을 수 없습니다."),

/**
* 405 Method Not Allowed
Expand All @@ -30,7 +31,6 @@ public enum ErrorStatus {
*/
CONFLICT(HttpStatus.CONFLICT, "이미 존재하는 리소스입니다."),
DUPLICATE_MEMBER(HttpStatus.CONFLICT, "이미 존재하는 회원입니다."),
DUPLICATE_CATEGORY(HttpStatus.CONFLICT, "이미 존재하는 카테고리입니다."),

/**
* 500 Internal Server Error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package org.sopt.domain.post.repository;

import org.sopt.common.error.EntityNotFoundException;
import org.sopt.common.error.ErrorStatus;
import org.sopt.domain.post.domain.Category;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CategoryRepository extends JpaRepository<Category, Short> {
boolean existsByContent(String content);
Optional<Category> findByContent(String content);

default Category findByContentOrThrow(String content) {
return findByContent(content)
.orElseThrow(() -> new EntityNotFoundException(ErrorStatus.CATEGORY_NOT_FOUND));
}
}

0 comments on commit f914b05

Please sign in to comment.