Skip to content

Commit

Permalink
refactor: 세미나/공지사항 게시물 like 관련 비즈니스 로직 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
kang20 committed Feb 27, 2025
1 parent b3f6d05 commit a782bb9
Show file tree
Hide file tree
Showing 41 changed files with 258 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.application.board.api.AdminBoardApi;
import com.example.demo.domain.board.service.usecase.BoardAdminService;
import com.example.demo.domain.board.service.service.BoardAdminService;
import com.example.demo.global.base.dto.ResponseBody;

import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.example.demo.domain.board.service.entity.vo.BoardType;
import com.example.demo.domain.base.page.GlobalPageableDto;
import com.example.demo.domain.board.service.entity.BoardInfo;
import com.example.demo.domain.board.service.usecase.BoardService;
import com.example.demo.domain.board.service.service.BoardService;
import com.example.demo.global.aop.AssignUserId;
import com.example.demo.global.base.dto.ResponseBody;
import com.example.demo.global.base.dto.page.GlobalPageResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.example.demo.application.board.api.BoardFileApi;
import com.example.demo.application.board.dto.request.FileRequest;
import com.example.demo.application.board.dto.request.PresignedUrlRequest;
import com.example.demo.domain.board.service.usecase.BoardFileService;
import com.example.demo.domain.board.service.service.BoardFileService;
import com.example.demo.global.aop.AssignUserId;
import com.example.demo.global.base.dto.ResponseBody;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.example.demo.application.board.api.CategoryApi;
import com.example.demo.domain.base.page.GlobalPageableDto;
import com.example.demo.domain.board.service.entity.BoardTitleInfo;
import com.example.demo.domain.board.service.usecase.CategoryService;
import com.example.demo.domain.board.service.service.CategoryService;
import com.example.demo.global.base.dto.ResponseBody;
import com.example.demo.global.base.dto.page.GlobalPageResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.application.board.api.LikeApi;
import com.example.demo.domain.base.page.GlobalPageableDto;
import com.example.demo.domain.board.service.entity.BoardTitleInfo;
import com.example.demo.domain.board.service.usecase.LikeService;
import com.example.demo.domain.board.service.service.LikeService;
import com.example.demo.global.aop.AssignUserId;
import com.example.demo.global.base.dto.ResponseBody;
import com.example.demo.global.base.dto.page.GlobalPageResponse;
Expand Down Expand Up @@ -52,6 +53,8 @@ public ResponseEntity<ResponseBody<Void>> deleteLike(Long userId,@PathVariable L
public ResponseEntity<ResponseBody<GlobalPageResponse<BoardTitleInfo>>> getLikes(
Long userId,
@PageableDefault(page=0, size=10,sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
return ResponseEntity.ok(createSuccessResponse(likeService.getLikes(userId,pageable)));
return ResponseEntity.ok(createSuccessResponse(
GlobalPageResponse.create(
likeService.getLikes(userId, GlobalPageableDto.create(pageable)))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.Getter;

@Getter
@EqualsAndHashCode
public class BoardInfo {
private final Long boardId;
private final BoardContent boardContent;
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.demo.domain.board.service.service;
package com.example.demo.domain.board.service.implement;

import com.example.demo.infra.board.entity.Like;
import com.example.demo.domain.notification.domain.entity.Notification;
Expand All @@ -14,7 +14,7 @@

@Service
@RequiredArgsConstructor
public class LikeNotificationService {
public class LikeNotificationHandler {
private final NotificationRepository notificationRepository;

@Async
Expand All @@ -28,9 +28,12 @@ public CompletableFuture<Notification> saveLikeNotification(Like like) {
return CompletableFuture.completedFuture(null);
}


@Async
@Transactional
public void deleteLikeNotification(Long id) {
notificationRepository.deleteByInvokerIdAndInvokerType(id, NotificationType.BOARD_LIKE);
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.demo.domain.board.service.repository;

import java.util.List;

import com.example.demo.domain.board.service.entity.BoardCategoryNames;
import com.example.demo.domain.board.service.entity.BoardInfo;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.example.demo.domain.board.service.repository;

import java.util.Optional;

import com.example.demo.domain.board.service.entity.BoardInfo;
import com.example.demo.infra.board.entity.ImageFile;

public interface ImageFileRepository {
void saveImageFile(String url, BoardInfo boardInfo);
Expand Down
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);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.demo.domain.board.service.usecase;
package com.example.demo.domain.board.service.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.demo.domain.board.service.usecase;
package com.example.demo.domain.board.service.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.demo.domain.board.service.usecase;
package com.example.demo.domain.board.service.service;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.demo.domain.board.service.usecase;
package com.example.demo.domain.board.service.service;

import java.util.List;

Expand Down
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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import org.springframework.stereotype.Service;

import com.example.demo.infra.board.Repository.BoardJpaRepository;
import com.example.demo.infra.board.repository.BoardJpaRepository;

import lombok.RequiredArgsConstructor;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/demo/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@ public class User extends BaseEntity {
// @JoinColumn(name = "news_letter_id")
// private Newsletter newsletter;

@OneToMany(mappedBy = "user")
private List<Board> boards = new ArrayList<>();

@OneToMany(mappedBy = "user")
private List<BoardComment> boardComments = new ArrayList<>();

@OneToMany(mappedBy = "user")
private List<RecruitmentBoardComment> recruitmentBoardComments = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.PERSIST)
private List<Like> likes = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SeminarApplication> seminarApplications = new ArrayList<>();
Expand Down Expand Up @@ -155,4 +151,8 @@ public boolean equals(Object obj) {
User user = (User) obj;
return id != null && id.equals(user.id);
}

public User(Long id) {
this.id = id;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.example.demo.infra.board.category.repository;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.domain.board.service.entity.BoardCategoryNames;
import com.example.demo.domain.board.service.entity.BoardInfo;
import com.example.demo.domain.board.service.repository.BoardCategoryRepository;
import com.example.demo.infra.board.Repository.BoardJpaRepository;
import com.example.demo.infra.board.repository.BoardJpaRepository;
import com.example.demo.infra.board.entity.Board;
import com.example.demo.infra.board.category.entity.BoardCategory;
import com.example.demo.infra.board.category.entity.Category;
Expand Down
Loading

0 comments on commit a782bb9

Please sign in to comment.