Skip to content

Commit

Permalink
✨[feat]: validate if the quote is already liked by the user
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyesooo committed Oct 26, 2023
1 parent d52f71b commit 0e43b5e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.pingpong.quoteBakery.app.dto.LikeDto;
import com.pingpong.quoteBakery.app.dto.QuoteSingleSearchDto;
import com.pingpong.quoteBakery.app.resource.LikeResource;
import com.pingpong.quoteBakery.app.resource.QuoteConverter;
import com.pingpong.quoteBakery.app.resource.RandomQuoteResource;
import com.pingpong.quoteBakery.app.resource.RandomQuoteSearchResource;
import com.pingpong.quoteBakery.app.resource.*;
import com.pingpong.quoteBakery.app.service.QuoteService;
import com.pingpong.quoteBakery.com.api.response.ApiRes;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -78,7 +75,7 @@ public ApiRes<RandomQuoteResource> bakeRandomQuote(
@Operation(summary = "명언 좋아요", description = "명언 좋아요",
responses = {@ApiResponse(responseCode = "200", description = "등록 성공", content = @Content(schema = @Schema(type = "number", description = "좋아요ID")))}
)
public ApiRes<Long> likeQuote(@RequestBody @io.swagger.v3.oas.annotations.parameters.RequestBody LikeResource createResource){
public ApiRes<Long> likeQuote(@RequestBody @io.swagger.v3.oas.annotations.parameters.RequestBody LikeCreateResource createResource){
return ApiRes.createSuccess(quoteService.saveLike(quoteConverter.convertToGeneric(createResource, LikeDto.class)));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pingpong.quoteBakery.app.persistence;

import com.pingpong.quoteBakery.app.domain.Like;
import com.pingpong.quoteBakery.app.domain.Quote;
import com.pingpong.quoteBakery.sys.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -11,6 +13,8 @@
public interface LikeRepository extends JpaRepository<Like, Long> {
List<Like> findAllByUser_Id(Long userId);

boolean existsByUserAndQuote(User user, Quote quote);

@Modifying
@Query("DELETE FROM Like l WHERE l.user.id = :userId")
void deleteAllByUserId(@Param("userId") Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public Long saveLike(LikeDto likeDto) {
Quote quote = quoteRepository.findById(likeDto.getQuoteId())
.orElseThrow(() -> new BusinessInvalidValueException("해당 ID에 대한 정보가 없습니다."));

boolean existYn = likeRepository.existsByUserAndQuote(user, quote);
if(existYn) throw new BusinessInvalidValueException("이미 좋아요 등록된 명언입니다.");

return likeRepository.save(Like.toEntity(user, quote)).getLikeId();
}

Expand Down

0 comments on commit 0e43b5e

Please sign in to comment.