-
Notifications
You must be signed in to change notification settings - Fork 2
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
[Feat] #20 인플루언서 좋아요와 반영하여 반환하도록 했어요 #72
Changes from 10 commits
6b1f51d
1252b70
b0cf076
fb37b3d
10ad340
934b143
82840ae
4a60607
fa62990
8a9b168
95970e4
70a6438
0549b1d
2b2cbbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package team7.inplace.favoriteInfluencer.persistent; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import team7.inplace.favoriteInfluencer.domain.FavoriteInfluencer; | ||
import team7.inplace.influencer.domain.Influencer; | ||
import team7.inplace.user.domain.User; | ||
|
||
public interface FavoriteInfluencerRepository extends JpaRepository<FavoriteInfluencer, Long> { | ||
|
||
List<FavoriteInfluencer> findByUserId(Long userId); | ||
|
||
Optional<FavoriteInfluencer> findByUserAndInfluencer(User user, Influencer influencer); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package team7.inplace.influencer.application; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
@@ -13,7 +15,7 @@ | |
import team7.inplace.influencer.application.dto.InfluencerInfo; | ||
import team7.inplace.influencer.domain.Influencer; | ||
import team7.inplace.influencer.persistence.InfluencerRepository; | ||
import team7.inplace.influencer.presentation.dto.InfluencerRequestParam; | ||
import team7.inplace.influencer.presentation.dto.InfluencerLikeRequest; | ||
import team7.inplace.security.util.AuthorizationUtil; | ||
import team7.inplace.user.domain.User; | ||
import team7.inplace.user.persistence.UserRepository; | ||
|
@@ -28,9 +30,32 @@ public class InfluencerService { | |
|
||
@Transactional(readOnly = true) | ||
public List<InfluencerInfo> getAllInfluencers() { | ||
return influencerRepository.findAll().stream() | ||
.map(InfluencerInfo::from) | ||
List<Influencer> influencers = influencerRepository.findAll(); | ||
Long userId = AuthorizationUtil.getUserId(); | ||
|
||
// 로그인 안된 경우, likes를 모두 false로 설정 | ||
if (userId == null) { | ||
return influencers.stream() | ||
.map(influencer -> InfluencerInfo.from(influencer, false)) | ||
.toList(); | ||
} | ||
|
||
// 로그인 된 경우 | ||
Set<Long> likedInfluencerIds = favoriteRepository.findByUserId(userId).stream() | ||
.filter(FavoriteInfluencer::isLiked) | ||
.map(FavoriteInfluencer::getInfluencer) | ||
.map(Influencer::getId) | ||
.collect(Collectors.toSet()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set으로 하신 이유가 있을까요? |
||
|
||
List<InfluencerInfo> influencerInfos = influencers.stream() | ||
.map(influencer -> { | ||
boolean isLiked = likedInfluencerIds.contains(influencer.getId()); | ||
return InfluencerInfo.from(influencer, isLiked); | ||
}) | ||
.sorted((a, b) -> Boolean.compare(b.likes(), a.likes())) | ||
.toList(); | ||
|
||
return influencerInfos; | ||
} | ||
|
||
@Transactional | ||
|
@@ -43,7 +68,7 @@ public Long createInfluencer(InfluencerCommand command) { | |
public Long updateInfluencer(Long id, InfluencerCommand command) { | ||
Influencer influencer = influencerRepository.findById(id).orElseThrow(); | ||
influencer.update(command.influencerName(), command.influencerImgUrl(), | ||
command.influencerJob()); | ||
command.influencerJob()); | ||
|
||
return influencer.getId(); | ||
} | ||
|
@@ -56,17 +81,19 @@ public void deleteInfluencer(Long id) { | |
} | ||
|
||
@Transactional | ||
public void likeToInfluencer(InfluencerRequestParam param) { | ||
public void likeToInfluencer(InfluencerLikeRequest param) { | ||
String username = AuthorizationUtil.getUsername(); | ||
if (StringUtils.hasText(username)) { | ||
if (!StringUtils.hasText(username)) { | ||
throw InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY); | ||
} | ||
|
||
User user = userRepository.findByUsername(username).orElseThrow(); | ||
Influencer influencer = influencerRepository.findById(param.influencerId()).orElseThrow(); | ||
|
||
FavoriteInfluencer favorite = new FavoriteInfluencer(user, influencer); | ||
favorite.check(param.likes()); | ||
FavoriteInfluencer favorite = favoriteRepository.findByUserAndInfluencer(user, influencer) | ||
.orElseGet(() -> new FavoriteInfluencer(user, influencer)); // 존재하지 않으면 새로 생성 | ||
|
||
favorite.updateLike(param.likes()); | ||
favoriteRepository.save(favorite); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Transaction이 있는데 save가 필요할까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 저 부분을 놓쳤네요! 굳입니다. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package team7.inplace.influencer.presentation.dto; | ||
|
||
public record InfluencerLikeRequest( | ||
Long influencerId, | ||
Boolean likes | ||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
package team7.inplace.influencer.presentation.dto; | ||
|
||
import team7.inplace.influencer.application.dto.InfluencerCommand; | ||
|
||
public record InfluencerRequest( | ||
String influencerName, | ||
String influencerImgUrl, | ||
String influencerJob | ||
) { | ||
|
||
public static InfluencerCommand to(InfluencerRequest request) { | ||
return new InfluencerCommand( | ||
request.influencerName(), | ||
request.influencerImgUrl(), | ||
request.influencerJob() | ||
); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
왜 객체로 호출하는지 여줘봐도 될까요?