Skip to content

Commit

Permalink
fix: pgId 추가 및 isAlreadyPoke 변경 (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunom authored Dec 20, 2023
2 parents a506246 + a671f04 commit 5133d62
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 18 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/sopt/app/application/poke/FriendService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.sopt.app.application.poke;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.sopt.app.common.exception.NotFoundException;
import org.sopt.app.common.response.ErrorCode;
import org.sopt.app.domain.entity.Friend;
Expand Down Expand Up @@ -51,7 +53,10 @@ private void registerFriendshipOf(Long userId, Long friendId) {
public void applyPokeCount(Long pokerId, Long pokedId) {
Friend friendship = friendRepository.findByUserIdAndFriendUserId(pokerId, pokedId)
.orElseThrow(() -> new NotFoundException(ErrorCode.FRIENDSHIP_NOT_FOUND.getMessage()));
Friend pokedFriendship = friendRepository.findByUserIdAndFriendUserId(pokedId, pokerId)
.orElseThrow(() -> new NotFoundException(ErrorCode.FRIENDSHIP_NOT_FOUND.getMessage()));
friendship.addPokeCount();
pokedFriendship.addPokeCount();
}

public boolean isFriendEachOther(Long pokerId, Long pokedId) {
Expand Down Expand Up @@ -91,7 +96,10 @@ public List<Long> getMutualFriendIds(Long pokerId, Long pokedId) {
}

public List<Long> getNotPokeFriendIdRandomly(Long userId, List<Long> pokedFriendIds, List<Long> pokeFriendIds) {
List<Long> notPokeFriendIds = friendRepository.findAllByUserIdAndFriendUserIdNotInAndFriendUserIdNotIn(userId, pokedFriendIds, pokeFriendIds);
val friendIds = new ArrayList<Long>();
friendIds.addAll(pokedFriendIds);
friendIds.addAll(pokeFriendIds);
List<Long> notPokeFriendIds = friendRepository.findAllByUserIdAndFriendUserIdNotInAndFriendUserIdNotIn(userId, friendIds);
Collections.shuffle(notPokeFriendIds);
if (notPokeFriendIds.isEmpty()) {
return List.of();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/sopt/app/application/poke/PokeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class Relationship {
@ToString
public static class PokedUserInfo {
private final Long userId;
private final Long playgroundId;
private final String name;
private final String profileImage;
private final Integer generation;
Expand Down
53 changes: 43 additions & 10 deletions src/main/java/org/sopt/app/facade/PokeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ private List<SimplePokeProfile> makeDummySimplePokeProfile(List<UserProfile> use

return SimplePokeProfile.of(
userProfile.getUserId(),
playgroundProfile.getId(),
playgroundProfile.getProfileImage(),
playgroundProfile.getName(),
"",
Integer.parseInt(generation),
part,
0,
"",
Friendship.NON_FRIEND.getFriendshipName(),
List.of(),
true,
false
Expand All @@ -96,15 +97,18 @@ private List<Long> pickRandomUserIds(
@Transactional(readOnly = true)
public List<PokeResponse.Friend> getRecommendFriendsOfUsersFriend(User user) {
val userId = user.getId();
val friendIds = friendService.findAllFriendIdsByUserIdRandomly(userId, 2);
return friendIds.stream().map(
friendId -> {
val friendProfile = playgroundAuthService.getPlaygroundMemberProfiles(user.getPlaygroundToken() ,List.of(friendId)).get(0);
val recommendUserProfiles = userService.findRandomFriendsOfFriends(userId, friendId, 2);
val friendsUserIds = friendService.findAllFriendIdsByUserIdRandomly(userId, 2);
return friendsUserIds.stream().map(
friendsUserId -> {
val friendUser = userService.getUserProfile(friendsUserId);
val friendProfile = playgroundAuthService.getPlaygroundMemberProfiles(user.getPlaygroundToken() ,List.of(
friendUser.getPlaygroundId())).get(0);
val recommendUserProfiles = userService.findRandomFriendsOfFriends(userId, friendsUserId, 2);
val playgroundProfiles = playgroundAuthService.getPlaygroundMemberProfiles(user.getPlaygroundToken(), recommendUserProfiles.stream().map(UserProfile::getPlaygroundId).toList());
val simpleProfiles = makeDummySimplePokeProfile(recommendUserProfiles, playgroundProfiles);
return PokeResponse.Friend.of(
friendId,
friendsUserId,
friendProfile.getId(),
friendProfile.getName(),
friendProfile.getProfileImage(),
simpleProfiles
Expand All @@ -118,7 +122,8 @@ public List<SimplePokeProfile> getAllPokeMeHistory(User user) {
List<PokeHistory> pokedHistories = pokeHistoryService.getAllPokedHistoryOrderByMostRecent(user.getId());
return pokedHistories.stream()
.filter(pokeHistory -> !pokeHistory.getIsReply())
.map(pokeHistory -> getPokeHistoryProfile(user, pokeHistory.getPokerId(),pokeHistory.getId()))
.map(pokeHistory ->
getPokeHistoryProfile(user, pokeHistory.getPokerId(),pokeHistory.getId()))
.distinct()
.toList();
}
Expand Down Expand Up @@ -178,8 +183,31 @@ public List<SimplePokeProfile> getFriend(User user) {
val friendUserProfile = userService.getUserProfileByUserId(friendId);
val friendPlaygroundIds = friendUserProfile.stream().map(UserProfile::getPlaygroundId).toList();
val friendProfile = playgroundAuthService.getPlaygroundMemberProfiles(user.getPlaygroundToken(), friendPlaygroundIds);
val friendRelationInfo = friendService.getRelationInfo(user.getId(), friendId.get(0));
val mutualFriendNames = friendService.getMutualFriendIds(user.getId(), friendId.get(0)).stream()
.map(id -> {
UserInfo.UserProfile friendProfile1 = userService.getUserProfile(id);
return friendProfile1.getName();
})
.toList();
val isAlreadyPoke = pokeHistoryService.getAllOfPokeBetween(user.getId(), friendId.get(0)).isEmpty();

return makeDummySimplePokeProfile(friendUserProfile, friendProfile);
return List.of(
SimplePokeProfile.of(
friendUserProfile.get(0).getUserId(),
friendProfile.get(0).getId(),
friendProfile.get(0).getProfileImage(),
friendProfile.get(0).getName(),
"",
Integer.parseInt(friendProfile.get(0).getActivities().get(0).getGeneration()),
friendProfile.get(0).getActivities().get(0).getPart(),
friendRelationInfo.getPokeCount(),
friendRelationInfo.getRelationName(),
mutualFriendNames,
false,
isAlreadyPoke
)
);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -221,11 +249,14 @@ public SimplePokeProfile getPokeHistoryProfile(User user, Long friendId, Long po
// 나에 대해 찌른 내역을 반환
PokeInfo.PokeDetail pokeDetail = getPokeInfo(pokeId);

val isAlreadyReply = pokeDetail.getPokerId().equals(user.getId());

PokeInfo.PokedUserInfo friendUserInfo = getFriendUserInfo(
user, friendId);

return SimplePokeProfile.of(
friendUserInfo.getUserId(),
friendUserInfo.getPlaygroundId(),
friendUserInfo.getProfileImage(),
friendUserInfo.getName(),
pokeDetail.getMessage(),
Expand All @@ -235,7 +266,7 @@ public SimplePokeProfile getPokeHistoryProfile(User user, Long friendId, Long po
friendUserInfo.getRelation().getRelationName(),
friendUserInfo.getMutualFriendNames(),
friendUserInfo.getRelation().getPokeCount() == 0,
pokeDetail.getIsReply()
isAlreadyReply
);
}

Expand All @@ -255,6 +286,7 @@ private PokeInfo.PokedUserInfo getFriendUserInfo(User user, Long friendUserId) {
val relationInfo = friendService.getRelationInfo(user.getId(), friendUserId);
return PokeInfo.PokedUserInfo.builder()
.userId(pokedUserProfile.getUserId())
.playgroundId(pokedUserPlaygroundProfile.getId())
.name(pokedUserPlaygroundProfile.getName())
.profileImage(pokedUserPlaygroundProfile.getProfileImage())
.generation(Integer.parseInt(latestActivity.getGeneration()))
Expand All @@ -265,6 +297,7 @@ private PokeInfo.PokedUserInfo getFriendUserInfo(User user, Long friendUserId) {
}
return PokeInfo.PokedUserInfo.builder()
.userId(pokedUserProfile.getUserId())
.playgroundId(pokedUserPlaygroundProfile.getId())
.name(pokedUserPlaygroundProfile.getName())
.profileImage(pokedUserPlaygroundProfile.getProfileImage())
.generation(Integer.parseInt(latestActivity.getGeneration()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.app.interfaces.postgres;

import java.util.List;
import org.sopt.app.domain.entity.Friend;

public interface FriendCustomRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public interface FriendRepository extends JpaRepository<Friend, Long>, FriendCustomRepository {
Optional<Friend> findByUserIdAndFriendUserId(Long userId, Long friendId);

@Query(value = "SELECT f.friendUserId FROM Friend as f WHERE f.userId = :userId AND f.friendUserId not in :pokedFriendIds AND f.friendUserId not in :pokeFriendIds")
List<Long> findAllByUserIdAndFriendUserIdNotInAndFriendUserIdNotIn(@Param("userId") Long userId, @Param("pokedFriendIds") List<Long> pokedFriendIds, @Param("pokeFriendIds") List<Long> pokeFriendIds);
@Query(value = "SELECT f.friendUserId FROM Friend as f WHERE f.userId = :userId AND f.friendUserId NOT IN (:pokedFriendIds)")
List<Long> findAllByUserIdAndFriendUserIdNotInAndFriendUserIdNotIn(@Param("userId") Long userId, @Param("pokedFriendIds") List<Long> pokedFriendIds);
@Query("SELECT f.friendUserId FROM Friend f WHERE f.userId = :userId")
List<Long> findAllOfFriendIdsByUserId(@Param("userId") Long userId);
@Query(value = "SELECT f FROM Friend f WHERE f.userId = :userId AND f.pokeCount BETWEEN :lowerLimit AND :upperLimit ORDER BY f.pokeCount")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.app.domain.entity.Friend;
import org.sopt.app.domain.entity.QFriend;

@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public interface PokeHistoryRepository extends JpaRepository<PokeHistory, Long>

List<PokeHistory> findAllByPokerIdAndPokedIdOrderByCreatedAtDesc(Long pokerId, Long pokedId);

@Query("SELECT ph FROM PokeHistory ph WHERE (ph.pokerId = :userId AND ph.pokedId = :friendId) OR (ph.pokerId = :friendId AND ph.pokedId = :userId) ORDER BY ph.createdAt DESC ")
@Query("SELECT ph FROM PokeHistory ph WHERE ((ph.pokerId = :userId AND ph.pokedId = :friendId) OR (ph.pokerId = :friendId AND ph.pokedId = :userId)) AND ph.isReply = false ORDER BY ph.createdAt DESC ")
List<PokeHistory> findAllWithFriendOrderByCreatedAtDesc(@Param("userId") Long userId, @Param("friendId") Long friendId);
}
13 changes: 9 additions & 4 deletions src/main/java/org/sopt/app/presentation/poke/PokeResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static IsNew of(Boolean isNew) {
}
}

// TODO 사용되지 않는 interface 책임 지우기
interface FriendList {
}
interface HistoryList{
Expand Down Expand Up @@ -137,6 +138,8 @@ public static class SimplePokeProfile {

@Schema(description = "유저 ID", example = "1")
private Long userId;
@Schema(description = "playgroundId", example = "1")
private Long playgroundId;
@Schema(description = "프로필 사진 URL", example = ".....")
private String profileImage;
@Schema(description = "유저 이름", example = "다혜다해")
Expand All @@ -159,12 +162,12 @@ public static class SimplePokeProfile {
private Boolean isAlreadyPoke;

public static SimplePokeProfile of(
Long userId, String profileImage, String name, String message,
Long userId, Long playgroundId, String profileImage, String name, String message,
Integer generation, String part, Integer pickNum, String relationName, List<String> mutual, Boolean isFirstMeet,
Boolean isAlreadyPoke
) {
return new SimplePokeProfile(
userId, profileImage, name, message, generation, part, pickNum, relationName, mutual, isFirstMeet,
userId, playgroundId, profileImage, name, message, generation, part, pickNum, relationName, mutual, isFirstMeet,
isAlreadyPoke
);
}
Expand All @@ -191,15 +194,17 @@ public static class PokeAlarmStatusResponse {
public static class Friend {
@Schema(description = "친구 ID", example = "1")
private Long friendId;
@Schema(description = "playgroundId", example = "1")
private Long playgroundId;
@Schema(description = "친구 이름", example = "제갈송현")
private String friendName;
@Schema(description = "친구 프로필 사진 URL", example = ".....")
private String friendProfileImage;
@Schema(description = "친구 프로필 리스트", example = "[{'userId': 1, 'profileImage': '...', 'name': '제갈송현', 'generation': 29, 'part': '안드로이드'}]")
private List<SimplePokeProfile> friendList;

public static Friend of(Long friendId, String friendName, String friendProfileImage, List<SimplePokeProfile> friendList) {
return new Friend(friendId, friendName, friendProfileImage, friendList);
public static Friend of(Long friendId, Long playgroundId, String friendName, String friendProfileImage, List<SimplePokeProfile> friendList) {
return new Friend(friendId, playgroundId, friendName, friendProfileImage, friendList);
}
}

Expand Down

0 comments on commit 5133d62

Please sign in to comment.