Skip to content

Commit

Permalink
style(Post) : 즐겨찾기/좋아요 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Sosohy committed May 9, 2024
1 parent 429dbc2 commit f0b9fa4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.transaction.Transactional;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.triumers.kmsback.post.command.Application.dto.*;
import org.triumers.kmsback.post.command.domain.aggregate.entity.*;
Expand Down Expand Up @@ -37,20 +38,20 @@ public CmdPostServiceImpl(ModelMapper mapper, CmdPostRepository cmdPostRepositor
public CmdPost registPost(CmdPostAndTagsDTO post) {

CmdPost registPost = new CmdPost(post.getTitle(), post.getContent(), post.getCreatedAt(),
post.getAuthorId(), post.getOriginId(), post.getTabRelationId());
post.getAuthorId(), post.getOriginId(), post.getTabRelationId());
cmdPostRepository.save(registPost);

registTag(post.getTags(), registPost.getId());

return registPost;
}

public void registTag(List<CmdTagDTO> tags, int postId){
public void registTag(List<CmdTagDTO> tags, int postId) {

for (int i = 0; i < tags.size(); i++) {
CmdTagDTO tagDTO = tags.get(i);

CmdTag tag = new CmdTag(tagDTO.getId(),tagDTO.getName());
CmdTag tag = new CmdTag(tagDTO.getId(), tagDTO.getName());
cmdTagRepository.save(tag);

CmdPostTag postTag = new CmdPostTag(tag.getId(), postId);
Expand Down Expand Up @@ -81,26 +82,35 @@ public CmdPost deletePost(int postId) {
@Override
public CmdLike likePost(CmdLikeDTO like) {

CmdLike likePost = new CmdLike(like.getId(), like.getEmployeeId(), like.getPostId());
if(likePost.getId() != null){ // unlike
cmdLikeRepository.deleteById(likePost.getId());
return likePost;
try {
CmdLike likePost = cmdLikeRepository.findByEmployeeIdAndPostId(like.getEmployeeId(), like.getPostId());

if (likePost != null) { //unlike
cmdLikeRepository.deleteById(likePost.getId());
return likePost;
}
} catch (Exception e) {

}

// like
CmdLike likePost = new CmdLike(like.getEmployeeId(), like.getPostId());
return cmdLikeRepository.save(likePost);
}

@Override
public CmdFavorites favoritePost(CmdFavoritesDTO favorite) {

CmdFavorites favoritePost = new CmdFavorites(favorite.getId(), favorite.getEmployeeId(), favorite.getPostId());
if(favoritePost.getId() != null){ // unfavorite
cmdFavoritesRepository.deleteById(favoritePost.getId());
return favoritePost;
try {
CmdFavorites favoritePost = cmdFavoritesRepository.findByEmployeeIdAndPostId(favorite.getEmployeeId(), favorite.getPostId());
if (favoritePost != null) { // unfavorite
cmdFavoritesRepository.deleteById(favoritePost.getId());
return favoritePost;
}
} catch (Exception e) {
}

// favorite
CmdFavorites favoritePost = new CmdFavorites(favorite.getEmployeeId(), favorite.getPostId());
return cmdFavoritesRepository.save(favoritePost);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.triumers.kmsback.post.command.Application.dto.CmdFavoritesDTO;
import org.triumers.kmsback.post.command.domain.aggregate.entity.CmdFavorites;

@Repository
public interface CmdFavoritesRepository extends JpaRepository<CmdFavorites, Integer> {


CmdFavorites findByEmployeeIdAndPostId(Integer employeeId, Integer postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.triumers.kmsback.post.command.Application.dto.CmdLikeDTO;
import org.triumers.kmsback.post.command.domain.aggregate.entity.CmdLike;
import org.triumers.kmsback.post.command.domain.aggregate.entity.CmdTag;

@Repository
public interface CmdLikeRepository extends JpaRepository<CmdLike, Integer> {
// CmdLike findByEmployeeIdAndPostId(CmdLikeDTO like);


CmdLike findByEmployeeIdAndPostId(Integer employeeId, Integer postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void modifyPost() {
tags.add(new CmdTagDTO("newtag5"));

CmdPostAndTagsDTO post = new CmdPostAndTagsDTO("modifyTitle", "modifyContent", LocalDate.now(),
1, 11, 1, tags);
1, 1, 1, tags);

CmdPost modifyPost = cmdPostService.modifyPost(post);

Expand All @@ -77,8 +77,8 @@ void deletePost(){
}

@Test
@DisplayName("게시글 좋아요")
void likePost(){
@DisplayName("게시글 좋아요/삭제")
void likePost() {

CmdLikeDTO like = new CmdLikeDTO(1, 1);
CmdLike likePost = cmdPostService.likePost(like);
Expand All @@ -87,17 +87,7 @@ void likePost(){
}

@Test
@DisplayName("게시글 좋아요 삭제")
void unlikePost(){

CmdLikeDTO like = new CmdLikeDTO(1, 1, 1);
CmdLike likePost = cmdPostService.likePost(like);

assertThat(likePost.getId()).isNotNull();
}

@Test
@DisplayName("게시글 즐겨찾기")
@DisplayName("게시글 즐겨찾기/삭제")
void favoritePost(){

CmdFavoritesDTO favorite = new CmdFavoritesDTO(1, 1);
Expand All @@ -106,14 +96,5 @@ void favoritePost(){
assertThat(likePost.getId()).isNotNull();
}

@Test
@DisplayName("게시글 즐겨찾기 삭제")
void unfavoritePost(){

CmdFavoritesDTO favorite = new CmdFavoritesDTO(1, 1, 1);
CmdFavorites likePost = cmdPostService.favoritePost(favorite);

assertThat(likePost.getId()).isNotNull();
}

}

0 comments on commit f0b9fa4

Please sign in to comment.