Skip to content

Commit

Permalink
[Refactor] 좋아요 토글 시 좋아요 개수도 같이 반환 #151
Browse files Browse the repository at this point in the history
  • Loading branch information
parseyong committed Sep 28, 2024
1 parent 6e693bf commit 25fa1aa
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 10 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.extern.slf4j.Slf4j;
import me.snaptime.common.CommonResponseDto;
import me.snaptime.email.service.EmailService;
import me.snaptime.profile.dto.res.UserProfileResDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import me.snaptime.common.CommonResponseDto;
import me.snaptime.snapLike.dto.res.SnapLikeResDto;
import me.snaptime.snapLike.service.SnapLikeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -28,11 +29,11 @@ public class SnapLikeController {
"특정 유저가 특정스냅에 좋아요를 눌렀다면 좋아요가 취소됩니다.<br>"+
"좋아요를 누르지 않았다면 좋아요가 추가됩니다.")
@Parameter(name = "snapId", description = "snap아이디를 입력해주세요.")
public ResponseEntity<CommonResponseDto<Void>> toggleSnapLike(
public ResponseEntity<CommonResponseDto<SnapLikeResDto>> toggleSnapLike(
@AuthenticationPrincipal final UserDetails userDetails,
@RequestParam final Long snapId){

String message = snapLikeService.toggleSnapLike(userDetails.getUsername(), snapId);
return ResponseEntity.status(HttpStatus.OK).body(new CommonResponseDto(message,null));
return ResponseEntity.status(HttpStatus.OK)
.body(new CommonResponseDto("좋아요 토글 성공",snapLikeService.toggleSnapLike(userDetails.getUsername(), snapId)));
}
}
18 changes: 18 additions & 0 deletions src/main/java/me/snaptime/snapLike/dto/res/SnapLikeResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.snaptime.snapLike.dto.res;

import lombok.Builder;

@Builder
public record SnapLikeResDto(
String message,
Long likeCnt
) {

public static SnapLikeResDto toDto(String message, Long likeCnt){

return SnapLikeResDto.builder()
.message(message)
.likeCnt(likeCnt)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.snaptime.snapLike.service;

import me.snaptime.snapLike.dto.res.SnapLikeResDto;

public interface SnapLikeService {

// 스냅 좋아요 토글기능
String toggleSnapLike(String reqLoginId, Long snapId);
SnapLikeResDto toggleSnapLike(String reqLoginId, Long snapId);

// 스냅에 달린 좋아요 개수 조회
Long findSnapLikeCnt(Long snapId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.snaptime.snap.domain.Snap;
import me.snaptime.snap.repository.SnapRepository;
import me.snaptime.snapLike.domain.SnapLike;
import me.snaptime.snapLike.dto.res.SnapLikeResDto;
import me.snaptime.snapLike.repository.SnapLikeRepository;
import me.snaptime.snapLike.service.SnapLikeService;
import me.snaptime.user.domain.User;
Expand All @@ -29,7 +30,7 @@ public class SnapLikeServiceImpl implements SnapLikeService {

@Override
@Transactional
public String toggleSnapLike(String reqEmail, Long snapId){
public SnapLikeResDto toggleSnapLike(String reqEmail, Long snapId){
User reqUser = userRepository.findByEmail(reqEmail)
.orElseThrow(() -> new CustomException(ExceptionCode.USER_NOT_EXIST));

Expand All @@ -46,11 +47,11 @@ public String toggleSnapLike(String reqEmail, Long snapId){
.build()
);
alarmAddService.createSnapAlarm(reqUser,snap.getUser(),snap, AlarmType.LIKE);
return "좋아요를 눌렀습니다.";
return SnapLikeResDto.toDto("좋아요를 눌렀습니다.", findSnapLikeCnt(snapId));
}
else{
snapLikeRepository.delete(snapLikeOptional.get());
return "좋아요를 취소하였습니다.";
return SnapLikeResDto.toDto("좋아요를 취소하였습니다.", findSnapLikeCnt(snapId));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
Expand Down

0 comments on commit 25fa1aa

Please sign in to comment.