-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 세미나/공지사항 게시물 like 관련 비즈니스 로직 리팩터링
- Loading branch information
Showing
41 changed files
with
258 additions
and
207 deletions.
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
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
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
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
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
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
61 changes: 21 additions & 40 deletions
61
src/main/java/com/example/demo/domain/board/service/implement/LikeHandler.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,66 +1,47 @@ | ||
package com.example.demo.domain.board.service.implement; | ||
|
||
import com.example.demo.domain.board.service.service.LikeNotificationService; | ||
import com.example.demo.infra.board.Repository.BoardJpaRepository; | ||
import com.example.demo.infra.board.Repository.LikeRepository; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.example.demo.domain.base.page.GlobalPageableDto; | ||
import com.example.demo.domain.board.service.entity.BoardTitleInfo; | ||
import com.example.demo.infra.board.entity.Board; | ||
import com.example.demo.infra.board.entity.Like; | ||
import com.example.demo.domain.user.domain.User; | ||
import com.example.demo.domain.user.repository.UserJpaRepository; | ||
import com.example.demo.global.base.dto.page.GlobalPageResponse; | ||
import com.example.demo.domain.board.service.repository.LikeRepository; | ||
import com.example.demo.global.base.exception.ErrorCode; | ||
import com.example.demo.global.base.exception.ServiceException; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class LikeHandler { | ||
private final LikeRepository likeRepository; | ||
private final BoardJpaRepository boardJpaRepository; | ||
private final UserJpaRepository userJpaRepository; | ||
private final LikeNotificationService likeNotificationService; | ||
|
||
@Transactional | ||
public void increaseLike(Long userId, Long boardId) { | ||
Board board = (Board) boardJpaRepository.findByIdWithUser(boardId).orElseThrow | ||
(() -> new ServiceException(ErrorCode.BOARD_NOT_FOUND)); | ||
User user = validateUser(userId); | ||
if (likeRepository.existsByBoardIdAndUserId(boardId, userId)) { | ||
throw new ServiceException(ErrorCode.USER_ALREADY_LIKE_BOARD); | ||
} | ||
Like like = new Like(user, board); | ||
likeRepository.save(like); | ||
likeNotificationService.saveLikeNotification(like); | ||
likeRepository.saveLike(userId,boardId); | ||
} | ||
|
||
|
||
@Transactional(readOnly = true) | ||
public GlobalPageResponse<BoardTitleInfo> getLikes(Long userId, Pageable pageable) { | ||
return GlobalPageResponse.create(likeRepository.findBoardsByUserId(userId, pageable)); | ||
public GlobalPageableDto<BoardTitleInfo> getLikes(Long userId, GlobalPageableDto pageableDto) { | ||
return likeRepository.findLikedBoardPageByUserId(userId, pageableDto); | ||
} | ||
|
||
private User validateUser(Long userId) { | ||
return userJpaRepository.findById(userId) | ||
.orElseThrow(() -> new ServiceException(ErrorCode.LIKE_USER_NOT_FOUND)); | ||
|
||
@Transactional | ||
public void decreaseLike(Long userId, Long boardId) { | ||
likeRepository.deleteLike(userId,boardId); | ||
} | ||
|
||
private Board validateBoard(Long boardId) { | ||
return boardJpaRepository.findById(boardId) | ||
.orElseThrow(() -> new ServiceException(ErrorCode.BOARD_NOT_FOUND)); | ||
public void validateExistLike(Long boardId, Long userId) { | ||
if (likeRepository.existsByBoardIdAndUserId(boardId, userId)) { | ||
throw new ServiceException(ErrorCode.USER_ALREADY_LIKE_BOARD); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void decreaseLike(Long userId, Long boardId) { | ||
Board board = validateBoard(boardId); | ||
User user = validateUser(userId); | ||
Like like = likeRepository.findByBoardIdAndUserId(boardId, userId) | ||
.orElseThrow(() -> new ServiceException(ErrorCode.USER_NOT_LIKE_BOARD)); | ||
board.getLikes().remove(like); | ||
user.getLikes().remove(like); | ||
likeRepository.delete(like); | ||
public void validateNonExistLike(Long boardId, Long userId) { | ||
if (!likeRepository.existsByBoardIdAndUserId(boardId, userId)) { | ||
throw new ServiceException(ErrorCode.USER_NOT_LIKE_BOARD); | ||
} | ||
} | ||
} |
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
2 changes: 0 additions & 2 deletions
2
src/main/java/com/example/demo/domain/board/service/repository/BoardCategoryRepository.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
3 changes: 0 additions & 3 deletions
3
src/main/java/com/example/demo/domain/board/service/repository/ImageFileRepository.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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/demo/domain/board/service/repository/LikeRepository.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,14 @@ | ||
package com.example.demo.domain.board.service.repository; | ||
|
||
import com.example.demo.domain.base.page.GlobalPageableDto; | ||
import com.example.demo.domain.board.service.entity.BoardTitleInfo; | ||
|
||
public interface LikeRepository { | ||
void saveLike(Long userId, Long boardId); | ||
|
||
boolean existsByBoardIdAndUserId(Long boardId, Long userId); | ||
|
||
void deleteLike(Long userId, Long boardId); | ||
|
||
GlobalPageableDto<BoardTitleInfo> findLikedBoardPageByUserId(Long userId, GlobalPageableDto pageableDto); | ||
} |
2 changes: 1 addition & 1 deletion
2
...rd/service/usecase/BoardAdminService.java → ...rd/service/service/BoardAdminService.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
2 changes: 1 addition & 1 deletion
2
...ard/service/usecase/BoardFileService.java → ...ard/service/service/BoardFileService.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
2 changes: 1 addition & 1 deletion
2
...n/board/service/usecase/BoardService.java → ...n/board/service/service/BoardService.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
2 changes: 1 addition & 1 deletion
2
...oard/service/usecase/CategoryService.java → ...oard/service/service/CategoryService.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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/example/demo/domain/board/service/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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example.demo.domain.board.service.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.example.demo.domain.base.page.GlobalPageableDto; | ||
import com.example.demo.domain.board.service.entity.BoardTitleInfo; | ||
import com.example.demo.domain.board.service.implement.BoardValidator; | ||
import com.example.demo.domain.board.service.implement.LikeHandler; | ||
import com.example.demo.domain.user.implement.UserReader; | ||
import com.example.demo.global.base.exception.ErrorCode; | ||
import com.example.demo.global.base.exception.ServiceException; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LikeService { | ||
private final LikeHandler likeHandler; | ||
private final UserReader userReader; | ||
private final BoardValidator boardValidator; | ||
|
||
public void likeBoard(Long userId, Long boardId) { | ||
boardValidator.validateExistBoard(boardId); | ||
userReader.findUser(userId) | ||
.orElseThrow(() -> new ServiceException(ErrorCode.LIKE_USER_NOT_FOUND)); | ||
likeHandler.validateExistLike(boardId, userId); | ||
likeHandler.increaseLike(userId, boardId); | ||
// likeNotificationHandler.saveLikeNotification(); TODO : LikeNotificationHandler 구현 필요 | ||
} | ||
|
||
public GlobalPageableDto<BoardTitleInfo> getLikes(Long userId, GlobalPageableDto pageableDto) { | ||
return likeHandler.getLikes(userId, pageableDto); | ||
} | ||
|
||
public void unlikeBoard(Long userId, Long boardId) { | ||
boardValidator.validateExistBoard(boardId); | ||
userReader.findUser(userId) | ||
.orElseThrow(() -> new ServiceException(ErrorCode.LIKE_USER_NOT_FOUND)); | ||
likeHandler.validateNonExistLike(boardId, userId); | ||
|
||
likeHandler.decreaseLike(userId, boardId); | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/main/java/com/example/demo/domain/board/service/usecase/LikeService.java
This file was deleted.
Oops, something went wrong.
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
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
27 changes: 0 additions & 27 deletions
27
src/main/java/com/example/demo/infra/board/Repository/LikeRepository.java
This file was deleted.
Oops, something went wrong.
4 changes: 1 addition & 3 deletions
4
...in/java/com/example/demo/infra/board/category/repository/BoardCategoryRepositoryImpl.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
Oops, something went wrong.