diff --git a/http/suggestion/SuggestionControllerHttpRequest.http b/http/suggestion/SuggestionControllerHttpRequest.http index 2bf8d09..bb8a3b0 100644 --- a/http/suggestion/SuggestionControllerHttpRequest.http +++ b/http/suggestion/SuggestionControllerHttpRequest.http @@ -46,4 +46,9 @@ Authorization: Bearer {{access_token}} { "content": "{{suggestion.API_6_5.content}}" -} \ No newline at end of file +} + +### 6.6 게시글 좋아요 +// @no-log +PUT {{host_url}}/api/v1/boards/{{suggestion.API_6_6.id}}/likes +Authorization: Bearer {{access_token}} \ No newline at end of file diff --git a/src/main/java/com/daon/onjung/suggestion/application/controller/command/.keep b/src/main/java/com/daon/onjung/suggestion/application/controller/command/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java b/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java index 68226d8..1d67e51 100644 --- a/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java +++ b/src/main/java/com/daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.java @@ -6,6 +6,7 @@ import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; import com.daon.onjung.suggestion.application.usecase.CreateBoardUseCase; import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase; +import com.daon.onjung.suggestion.application.usecase.CreateOrDeleteLikeUseCase; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; @@ -20,6 +21,7 @@ public class SuggestionCommandV1Controller { private final CreateBoardUseCase createBoardUseCase; private final CreateCommentUseCase createCommentUseCase; + private final CreateOrDeleteLikeUseCase createOrDeleteLikeUseCase; @PostMapping(value = "/api/v1/boards", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseDto createBoard( @@ -41,4 +43,15 @@ public ResponseDto createComment( return ResponseDto.created(null); } + @PutMapping("/api/v1/boards/{id}/likes") + public ResponseDto likeBoard( + @AccountID UUID accountId, + @PathVariable Long id + ) { + if (createOrDeleteLikeUseCase.execute(accountId, id)) { + return ResponseDto.created(null); + } else { + return ResponseDto.ok(null); + } + } } diff --git a/src/main/java/com/daon/onjung/suggestion/application/service/CreateOrDeleteLikeService.java b/src/main/java/com/daon/onjung/suggestion/application/service/CreateOrDeleteLikeService.java new file mode 100644 index 0000000..775f9e2 --- /dev/null +++ b/src/main/java/com/daon/onjung/suggestion/application/service/CreateOrDeleteLikeService.java @@ -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; + } + } +} diff --git a/src/main/java/com/daon/onjung/suggestion/application/usecase/CreateOrDeleteLikeUseCase.java b/src/main/java/com/daon/onjung/suggestion/application/usecase/CreateOrDeleteLikeUseCase.java new file mode 100644 index 0000000..dadff5f --- /dev/null +++ b/src/main/java/com/daon/onjung/suggestion/application/usecase/CreateOrDeleteLikeUseCase.java @@ -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); +} diff --git a/src/main/java/com/daon/onjung/suggestion/domain/service/LikeService.java b/src/main/java/com/daon/onjung/suggestion/domain/service/LikeService.java index 9afe744..e824f3a 100644 --- a/src/main/java/com/daon/onjung/suggestion/domain/service/LikeService.java +++ b/src/main/java/com/daon/onjung/suggestion/domain/service/LikeService.java @@ -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(); + } } diff --git a/src/main/java/com/daon/onjung/suggestion/repository/mysql/LikeRepository.java b/src/main/java/com/daon/onjung/suggestion/repository/mysql/LikeRepository.java index 10eeaa6..1d84903 100644 --- a/src/main/java/com/daon/onjung/suggestion/repository/mysql/LikeRepository.java +++ b/src/main/java/com/daon/onjung/suggestion/repository/mysql/LikeRepository.java @@ -6,10 +6,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface LikeRepository extends JpaRepository { Integer countByBoard(Board board); Boolean existsByBoardAndUser(Board board, User user); + + Optional findByBoardAndUser(Board board, User user); }