-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab98218
commit 15f3029
Showing
7 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/daon/onjung/suggestion/application/service/CreateOrDeleteLikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.daon.onjung.suggestion.application.service; | ||
|
||
import com.daon.onjung.account.domain.User; | ||
import com.daon.onjung.account.repository.mysql.UserRepository; | ||
import com.daon.onjung.core.exception.error.ErrorCode; | ||
import com.daon.onjung.core.exception.type.CommonException; | ||
import com.daon.onjung.suggestion.application.usecase.CreateOrDeleteLikeUseCase; | ||
import com.daon.onjung.suggestion.domain.Board; | ||
import com.daon.onjung.suggestion.domain.Like; | ||
import com.daon.onjung.suggestion.domain.service.LikeService; | ||
import com.daon.onjung.suggestion.repository.mysql.BoardRepository; | ||
import com.daon.onjung.suggestion.repository.mysql.LikeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CreateOrDeleteLikeService implements CreateOrDeleteLikeUseCase { | ||
|
||
private final BoardRepository boardRepository; | ||
private final UserRepository userRepository; | ||
private final LikeRepository likeRepository; | ||
private final LikeService likeService; | ||
|
||
@Override | ||
@Transactional | ||
public Boolean execute(UUID accountId, Long boardId) { | ||
|
||
// 유저 조회 | ||
User user = userRepository.findById(accountId) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
// 게시글 조회 | ||
Board board = boardRepository.findById(boardId) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
// 좋아요 여부 확인 | ||
Like like = likeRepository.findByBoardAndUser(board, user) | ||
.orElse(null); | ||
|
||
// 좋아요 생성 또는 삭제 | ||
if (like != null) { | ||
likeRepository.delete(like); | ||
return false; | ||
} else { | ||
likeRepository.save(likeService.createLike(user,board)); | ||
return true; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/daon/onjung/suggestion/application/usecase/CreateOrDeleteLikeUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.daon.onjung.suggestion.application.usecase; | ||
|
||
import com.daon.onjung.core.annotation.bean.UseCase; | ||
|
||
import java.util.UUID; | ||
|
||
@UseCase | ||
public interface CreateOrDeleteLikeUseCase { | ||
|
||
Boolean execute(UUID accountId, Long boardId); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/daon/onjung/suggestion/domain/service/LikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package com.daon.onjung.suggestion.domain.service; | ||
|
||
import com.daon.onjung.account.domain.User; | ||
import com.daon.onjung.suggestion.domain.Board; | ||
import com.daon.onjung.suggestion.domain.Like; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class LikeService { | ||
|
||
public Like createLike( | ||
User user, | ||
Board board | ||
) { | ||
return Like.builder() | ||
.user(user) | ||
.board(board) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters