-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…oMyInfluencer [Feat] #40 좋아요한 인플루언서에 대한 동작을 구현해보아요
- Loading branch information
Showing
10 changed files
with
159 additions
and
9 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/team7/inplace/influencer/application/InfluencerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,49 @@ | ||
package team7.inplace.influencer.application; | ||
|
||
import java.util.List; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.StringUtils; | ||
import team7.inplace.global.exception.InplaceException; | ||
import team7.inplace.global.exception.code.AuthorizationErrorCode; | ||
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.userFavoriteInfluencer.domain.UserFavoriteInfluencer; | ||
import team7.inplace.userFavoriteInfluencer.persistent.UserFavoriteInfluencerRepository; | ||
import team7.inplace.security.util.AuthorizationUtil; | ||
import team7.inplace.user.domain.User; | ||
import team7.inplace.user.persistence.UserRepository; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class InfluencerService { | ||
|
||
private final InfluencerRepository influencerRepository; | ||
private final UserFavoriteInfluencerRepository favoriteRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public List<InfluencerInfo> getAllInfluencers() { | ||
return influencerRepository.findAll().stream() | ||
.map(InfluencerInfo::from) | ||
.toList(); | ||
} | ||
|
||
public void likeToInfluencer(InfluencerRequestParam param){ | ||
String username = AuthorizationUtil.getUsername(); | ||
if(StringUtils.hasText(username)){ | ||
throw InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY); | ||
} | ||
|
||
User user = userRepository.findByUsername(username).orElseThrow(); | ||
Influencer influencer = influencerRepository.findById(param.influencerId()).orElseThrow(); | ||
|
||
UserFavoriteInfluencer favorite = new UserFavoriteInfluencer(user, influencer); | ||
favorite.check(param.likes()); | ||
favoriteRepository.save(favorite); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/team7/inplace/influencer/presentation/dto/InfluencerRequestParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package team7.inplace.influencer.presentation.dto; | ||
|
||
public record InfluencerRequestParam( | ||
Long influencerId, | ||
Boolean likes | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/team7/inplace/userFavoriteInfluencer/domain/UserFavoriteInfluencer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package team7.inplace.userFavoriteInfluencer.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import team7.inplace.influencer.domain.Influencer; | ||
import team7.inplace.user.domain.User; | ||
|
||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@NoArgsConstructor(access = PROTECTED) | ||
@Entity | ||
public class UserFavoriteInfluencer { | ||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private Long id; | ||
@ManyToOne | ||
@NonNull | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
@ManyToOne | ||
@NonNull | ||
@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 void dislike() { | ||
this.like = false; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ava/team7/inplace/userFavoriteInfluencer/persistent/UserFavoriteInfluencerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package team7.inplace.userFavoriteInfluencer.persistent; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import team7.inplace.userFavoriteInfluencer.domain.UserFavoriteInfluencer; | ||
import team7.inplace.user.domain.User; | ||
|
||
import java.util.List; | ||
|
||
public interface UserFavoriteInfluencerRepository extends JpaRepository<UserFavoriteInfluencer, Long> { | ||
List<UserFavoriteInfluencer> findByUserId(Long userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters