package com.rest.api.service.board; import com.rest.api.advice.exception.CNotOwnerException; import com.rest.api.advice.exception.CResourceNotExistException; import com.rest.api.advice.exception.CUserNotFoundException; import com.rest.api.entity.User; import com.rest.api.entity.board.Board; import com.rest.api.entity.board.Post; import com.rest.api.model.board.ParamsPost; import com.rest.api.repo.UserJpaRepo; import com.rest.api.repo.board.BoardJpaRepo; import com.rest.api.repo.board.PostJpaRepo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.List; @Slf4j @Service @Transactional @RequiredArgsConstructor public class BoardService { private final BoardJpaRepo boardJpaRepo; private final PostJpaRepo postJpaRepo; private final UserJpaRepo userJpaRepo; public Board findBoard(String boardName) { return boardJpaRepo.findByName(boardName); } public List findPosts(String boardName) { Board board = findBoard(boardName); return postJpaRepo.findByBoardId(board.getId()); } public Post getPost(long postId) { return postJpaRepo.findById(postId).orElseThrow(CResourceNotExistException::new); } public Post writePost(String uid, String boardName, ParamsPost paramsPost) { Board board = findBoard(boardName); Post post = new Post(userJpaRepo.findByUid(uid).orElseThrow(CUserNotFoundException::new), board.getId(), paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent()); return postJpaRepo.save(post); } public Post updatePost(long postId, String uid, ParamsPost paramsPost) { Post post = getPost(postId); User user = post.getUser(); if (!uid.equals(user.getUid())) throw new CNotOwnerException(); post.setUpdate(paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent()); return postJpaRepo.save(post); } public boolean deletePost(long postId, String uid) { Post post = getPost(postId); User user = post.getUser(); if (!uid.equals(user.getUid())) throw new CNotOwnerException(); postJpaRepo.delete(post); return true; } }