Skip to content

Commit

Permalink
✨ Feature/#171 - 6.6 게시글 좋아요 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyeomjang committed Nov 26, 2024
1 parent ab98218 commit 15f3029
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
7 changes: 6 additions & 1 deletion http/suggestion/SuggestionControllerHttpRequest.http
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ Authorization: Bearer {{access_token}}

{
"content": "{{suggestion.API_6_5.content}}"
}
}

### 6.6 게시글 좋아요
// @no-log
PUT {{host_url}}/api/v1/boards/{{suggestion.API_6_6.id}}/likes
Authorization: Bearer {{access_token}}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Void> createBoard(
Expand All @@ -41,4 +43,15 @@ public ResponseDto<Void> createComment(
return ResponseDto.created(null);
}

@PutMapping("/api/v1/boards/{id}/likes")
public ResponseDto<Void> likeBoard(
@AccountID UUID accountId,
@PathVariable Long id
) {
if (createOrDeleteLikeUseCase.execute(accountId, id)) {
return ResponseDto.created(null);
} else {
return ResponseDto.ok(null);
}
}
}
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;
}
}
}
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);
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Like, Long> {

Integer countByBoard(Board board);

Boolean existsByBoardAndUser(Board board, User user);

Optional<Like> findByBoardAndUser(Board board, User user);
}

0 comments on commit 15f3029

Please sign in to comment.