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

[Feat] #20 인플루언서 좋아요와 반영하여 반환하도록 했어요 #72

Merged
merged 14 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@NoArgsConstructor(access = PROTECTED)
@Entity
public class FavoriteInfluencer {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
Expand All @@ -33,21 +34,9 @@ public class FavoriteInfluencer {
@JoinColumn(name = "influencer_id")
private Influencer influencer;
@Column
private boolean like = false;

public void check(boolean check) {
if (check) {
like();
return;
}
dislike();
}

private void like() {
this.like = true;
}
private boolean isLiked = false;

private void dislike() {
this.like = false;
public void updateLike(boolean like) {
this.isLiked = like;
}
}
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 객체로 호출하는지 여줘봐도 될까요?

}
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;
Expand All @@ -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;
Expand All @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -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();
}
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transaction이 있는데 save가 필요할까요?

Copy link
Contributor Author

@wndlthsk wndlthsk Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.orElseGet(() -> new FavoriteInfluencer(user, influencer)) 여기서 새 객체 생성되는 경우에는 save 필요하지 않나요?!?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 저 부분을 놓쳤네요! 굳입니다.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public record InfluencerInfo(
boolean likes
) {

public static InfluencerInfo from(Influencer influencer) {
public static InfluencerInfo from(Influencer influencer, boolean isLiked) {
return new InfluencerInfo(
influencer.getId(),
influencer.getName(),
influencer.getImgUrl(),
influencer.getJob(),
false // 좋아요 기능 추가할 때 로직 추가 예정
isLiked
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -16,9 +15,9 @@
import team7.inplace.influencer.application.InfluencerService;
import team7.inplace.influencer.application.dto.InfluencerCommand;
import team7.inplace.influencer.application.dto.InfluencerInfo;
import team7.inplace.influencer.presentation.dto.InfluencerLikeRequest;
import team7.inplace.influencer.presentation.dto.InfluencerListResponse;
import team7.inplace.influencer.presentation.dto.InfluencerRequest;
import team7.inplace.influencer.presentation.dto.InfluencerRequestParam;
import team7.inplace.influencer.presentation.dto.InfluencerResponse;

@RequiredArgsConstructor
Expand All @@ -32,32 +31,30 @@ public class InfluencerController implements InfluencerControllerApiSpec {
public ResponseEntity<InfluencerListResponse> getAllInfluencers() {
List<InfluencerInfo> influencersDtoList = influencerService.getAllInfluencers();
List<InfluencerResponse> influencers = influencersDtoList.stream()
.map(InfluencerResponse::from)
.toList();
.map(InfluencerResponse::from)
.toList();
InfluencerListResponse response = new InfluencerListResponse(influencers);

return new ResponseEntity<>(response, HttpStatus.OK);
}

@PostMapping()
public ResponseEntity<Long> createInfluencer(@RequestBody InfluencerRequest request) {
InfluencerCommand influencerCommand = new InfluencerCommand(
request.influencerName(),
request.influencerImgUrl(),
request.influencerJob()
);
InfluencerCommand influencerCommand = InfluencerRequest.to(request);
Long savedId = influencerService.createInfluencer(influencerCommand);

return new ResponseEntity<>(savedId, HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<Long> updateInfluencer(@PathVariable Long id,
@RequestBody InfluencerRequest request) {
public ResponseEntity<Long> updateInfluencer(
@PathVariable Long id,
@RequestBody InfluencerRequest request
) {
InfluencerCommand influencerCommand = new InfluencerCommand(
request.influencerName(),
request.influencerImgUrl(),
request.influencerJob()
request.influencerName(),
request.influencerImgUrl(),
request.influencerJob()
);
Long updatedId = influencerService.updateInfluencer(id, influencerCommand);

Expand All @@ -72,7 +69,7 @@ public ResponseEntity<Long> deleteInfluencer(@PathVariable Long id) {
}

@PostMapping("/likes")
public ResponseEntity<Void> likeToInfluencer(@ModelAttribute InfluencerRequestParam param) {
public ResponseEntity<Void> likeToInfluencer(@RequestBody InfluencerLikeRequest param) {
influencerService.likeToInfluencer(param);
return new ResponseEntity<>(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface InfluencerControllerApiSpec {

@Operation(summary = "인플루언서들 리스트 반환", description = "토큰 유무에 따라 좋아요한 인플루언서 반영 여부가 다릅니다.")
@Operation(summary = "인플루언서들 리스트 반환", description = "토큰이 있는 경우 좋아요된 인플루언서가 먼저 반환됩니다.")
ResponseEntity<InfluencerListResponse> getAllInfluencers();

@Operation(summary = "인플루언서 등록", description = "새 인플루언서를 등록합니다.")
Expand Down
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.

Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.influencer.persistence.InfluencerRepository;

@DataJpaTest
@SpringBootTest
public class InfluencerRepositoryTest {

@Autowired
private InfluencerRepository influencerRepository;

@Test
public void findAllTest() {
Influencer influencer1 = new Influencer("influencer1", "imgUrl1", "job1");
Influencer influencer2 = new Influencer("influencer2", "imgUrl2", "job2");
Influencer influencer4 = new Influencer("influencer4", "imgUrl1", "job1");
Influencer influencer5 = new Influencer("influencer5", "imgUrl2", "job2");

influencerRepository.save(influencer1);
influencerRepository.save(influencer2);
influencerRepository.save(influencer4);
influencerRepository.save(influencer5);

List<Influencer> savedInfluencers = influencerRepository.findAll();

assertThat(savedInfluencers.get(0)).usingRecursiveComparison().isEqualTo(influencer1);
assertThat(savedInfluencers.get(1)).usingRecursiveComparison().isEqualTo(influencer2);
assertThat(savedInfluencers.get(3)).usingRecursiveComparison().isEqualTo(influencer4);
assertThat(savedInfluencers.get(4)).usingRecursiveComparison().isEqualTo(influencer5);
}

}
Loading