Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] 좋아요 누를 시 좋아요 개수 반환하도록 변경 & 태그유저의 프로필과 팔로우 유무를 같이반환하도록 변경 #152

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -51,7 +51,7 @@ public SnapPagingResDto findSnapPage(String reqEmail, Long pageNum){
String snapPhotoURL = urlComponent.makePhotoURL(tuple.get(snap.fileName),false);

return SnapDetailInfoResDto.toDto(tuple,profilePhotoURL,snapPhotoURL,
snapTagService.findTagUsers(snapId),
snapTagService.findTagUsers(snapId, reqEmail),
snapLikeService.findSnapLikeCnt(snapId),
snapLikeService.isLikedSnap(snapId, reqEmail));
}).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public SnapDetailInfoResDto findSnap(Long id, String userEmail) {
throw new CustomException(ExceptionCode.SNAP_IS_PRIVATE);
}
}
String snapPhotoUrl = urlComponent.makePhotoURL(foundSnap.getFileName(), foundSnap.isPrivate());
String profilePhotoUrl = urlComponent.makeProfileURL(foundSnap.getUser().getProfilePhoto().getProfilePhotoId());
List<TagUserFindResDto> tagUserFindResDtos = snapTagService.findTagUsers(foundSnap.getId());

String snapPhotoUrl = urlComponent.makePhotoURL(foundSnap.getFileName(), foundSnap.isPrivate());
String profilePhotoUrl = urlComponent.makeProfileURL(foundSnap.getUser().getProfilePhoto().getProfilePhotoId());
List<TagUserFindResDto> tagUserFindResDtos = snapTagService.findTagUsers(foundSnap.getId(), userEmail);

Long likeCnt = snapLikeService.findSnapLikeCnt(foundSnap.getId());
boolean isLikedSnap = snapLikeService.isLikedSnap(foundSnap.getId(), userEmail);
return SnapDetailInfoResDto.toDto(foundSnap, profilePhotoUrl, snapPhotoUrl, tagUserFindResDtos, likeCnt, isLikedSnap);
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
10 changes: 8 additions & 2 deletions src/main/java/me/snaptime/snapTag/dto/res/TagUserFindResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
public record TagUserFindResDto(

String tagUserEmail,
String tagUserName
String tagUserName,
String tagName,
String tagUserProfileUrl,
boolean isFollow

) {
public static TagUserFindResDto toDto(SnapTag snapTag){
public static TagUserFindResDto toDto(SnapTag snapTag, String tagUserProfileUrl, boolean isFollow){
return TagUserFindResDto.builder()
.tagUserEmail(snapTag.getTagUser().getEmail())
.tagUserName(snapTag.getTagUser().getName())
.tagName(snapTag.getTagUser().getName())
.tagUserProfileUrl(tagUserProfileUrl)
.isFollow(isFollow)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface SnapTagService {
void deleteAllTagUser(Snap snap);

// 스냅에 태그된 유저들의 정보를 가져옵니다.
List<TagUserFindResDto> findTagUsers(Long snapId);
List<TagUserFindResDto> findTagUsers(Long snapId, String reqEmail);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import lombok.RequiredArgsConstructor;
import me.snaptime.alarm.common.AlarmType;
import me.snaptime.alarm.service.AlarmAddService;
import me.snaptime.component.url.UrlComponent;
import me.snaptime.exception.CustomException;
import me.snaptime.exception.ExceptionCode;
import me.snaptime.friend.service.FriendService;
import me.snaptime.snap.domain.Snap;
import me.snaptime.snap.repository.SnapRepository;
import me.snaptime.snapTag.domain.SnapTag;
Expand All @@ -30,6 +32,8 @@ public class SnapTagServiceImpl implements SnapTagService {
private final UserRepository userRepository;
private final SnapRepository snapRepository;
private final AlarmAddService alarmAddService;
private final UrlComponent urlComponent;
private final FriendService friendService;

@Override
@Transactional
Expand Down Expand Up @@ -79,12 +83,21 @@ public void deleteAllTagUser(Snap snap){
}

@Override
public List<TagUserFindResDto> findTagUsers(Long snapId){
public List<TagUserFindResDto> findTagUsers(Long snapId, String reqEmail){
Snap snap = snapRepository.findById(snapId)
.orElseThrow(() -> new CustomException(ExceptionCode.SNAP_NOT_EXIST));
User reqUser = userRepository.findByEmail(reqEmail)
.orElseThrow(() -> new CustomException(ExceptionCode.USER_NOT_EXIST));

List<SnapTag> snapTags = snapTagRepository.findBySnap(snap);
return snapTags.stream().map( snapTag -> TagUserFindResDto.toDto(snapTag)).collect(Collectors.toList());

return snapTags.stream().map( snapTag ->{
User tagUser = snapTag.getTagUser();

String tagUserProfileUrl = urlComponent.makeProfileURL(tagUser.getProfilePhoto().getProfilePhotoId());
boolean isFollow = friendService.checkIsFollow(reqUser, tagUser);
return TagUserFindResDto.toDto(snapTag, tagUserProfileUrl, isFollow);
}).collect(Collectors.toList());
}

// 삭제된 태그유저 추출
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
Loading