Skip to content

Commit

Permalink
Merge pull request Team-TenTen#123 from JaeyoungAhn/fix/cancelLike
Browse files Browse the repository at this point in the history
Fix/Like 존재하지 않는 좋아요 취소 시 진행되는 문제 수정
  • Loading branch information
JaeyoungAhn committed Nov 21, 2023
2 parents d836641 + 3a86eef commit 4cd4952
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public ResponseEntity<LikeCreateApiResponse> createLike(
description = "[JWT 필요] 링크에 누른 좋아요를 취소하는 기능입니다.",
responses = {
@ApiResponse(responseCode = "204", description = "좋아요 취소를 성공한 경우"),
@ApiResponse(responseCode = "404", description = "이미 삭제하거나 존재하지 않는 좋아요인 경우",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
})
@DeleteMapping(value = "/links/{linkId}/like", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> cancelLike(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
import com.tenten.linkhub.domain.space.service.dto.link.LinkUpdateRequest;
import com.tenten.linkhub.global.exception.DataNotFoundException;
import com.tenten.linkhub.global.exception.UnauthorizedAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
Expand Down Expand Up @@ -98,7 +97,7 @@ public Boolean createLike(Long linkId, Long memberId) {
public void cancelLike(Long linkId, Long memberId) {
Optional<Like> like = likeRepository.findByLinkIdAndMemberId(linkId, memberId);

like.ifPresent(likeRepository::delete);
likeRepository.delete(like.orElseThrow(() -> new DataNotFoundException("존재하지 않는 좋아요입니다.")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void updateLink_request_ThrowsUnauthorizedAccessException() {

@Test
@DisplayName("좋아요에 성공한다.")
void createLike_request_Success() {
void createLike_LinkIdAndMemberId_Success() {
//given & when
linkFacade.createLike(linkId, memberId1);

Expand All @@ -155,15 +155,15 @@ void createLike_request_Success() {

@Test
@DisplayName("존재하지 않는 링크에 좋아요를 실패한다.")
void createLike_request_ThrowsDataNotFoundException() {
void createLike_LinkIdAndMemberId_ThrowsDataNotFoundException() {
//given & when & then
Assertions.assertThatThrownBy(() -> linkFacade.createLike(999L, memberId1))
.isInstanceOf(DataNotFoundException.class);
}

@Test
@DisplayName("이미 좋아요한 링크에 좋아요를 실패한다.")
void createLike_request_ThrowsUnauthorizedAccessException() {
void createLike_LinkIdAndMemberId_ThrowsUnauthorizedAccessException() {
//given
linkFacade.createLike(linkId, memberId1);

Expand All @@ -174,7 +174,7 @@ void createLike_request_ThrowsUnauthorizedAccessException() {

@Test
@DisplayName("좋아요 한 링크의 좋아요를 취소한다.")
void cancelLike_request_noContent() {
void cancelLike_LinkIdAndMemberId_noContent() {
//given
linkFacade.createLike(linkId, memberId1);

Expand All @@ -186,6 +186,14 @@ void cancelLike_request_noContent() {
assertThat(like).isEmpty();
}

@Test
@DisplayName("좋아요하지 않은 좋아요를 취소하는데 실패한다.")
void cancelLike_LinkIdAndMemberId_DataNotFoundException() {
//given //when //then
Assertions.assertThatThrownBy(() -> linkFacade.cancelLike(linkId, memberId1))
.isInstanceOf(DataNotFoundException.class);
}

@Test
@DisplayName("사용자는 CAN_EDIT이나 OWNER 권한이 아닌 경우 링크를 삭제할 수 없다.")
void deleteLink_request_ThrowsUnauthorizedAccessException() {
Expand Down

0 comments on commit 4cd4952

Please sign in to comment.