Skip to content

Commit

Permalink
feat: 팔로잉 목록 조회 API 구현 (#71)
Browse files Browse the repository at this point in the history
* style: Response 패키지 위치 변경 (#64)

* feat: 팔로잉 목록 조회 API 구현 (#64)

* style: UserRepository 패키지 구조 변경 (#64)
  • Loading branch information
kdkdhoho authored Feb 6, 2024
1 parent 9063bd8 commit 98a4861
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.listywave.auth.infra.kakao.response.KakaoTokenResponse;
import com.listywave.auth.presentation.dto.LoginResponse;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.UserRepository;
import com.listywave.user.repository.user.UserRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/listywave/common/util/UserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.UserRepository;
import com.listywave.user.repository.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.listywave.list.repository.ListRepository;
import com.listywave.list.repository.ReplyRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.UserRepository;
import com.listywave.user.repository.user.UserRepository;
import jakarta.transaction.Transactional;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -46,7 +46,7 @@ public CommentCreateResponse create(Long listId, String accessToken, String cont
public CommentFindResponse getComments(Long listId, int size, Long cursorId) {
Lists list = listRepository.getById(listId);
Long totalCount = commentRepository.countByList(list);

List<Comment> comments = commentRepository.getComments(listId, size, cursorId);

boolean hasNext = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.listywave.list.presentation.dto.response.ListCreateResponse;
import com.listywave.list.repository.ListRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.UserRepository;
import com.listywave.user.repository.user.UserRepository;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.listywave.list.repository.ListRepository;
import com.listywave.list.repository.ReplyRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.UserRepository;
import com.listywave.user.repository.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/listywave/user/application/domain/Follow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.listywave.user.application.domain;

import com.listywave.common.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Follow extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "following_user_id")
private User followingUser;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "follower_user_id")
private User followerUser;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.listywave.user.presentation.dto.response;
package com.listywave.user.application.dto;

import com.listywave.list.application.domain.Lists;
import com.listywave.user.application.dto.FeedListsResponse;
import java.util.List;

public record AllUserListsResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.listywave.user.application.dto;

import com.listywave.user.application.domain.User;
import java.util.List;
import lombok.Builder;

public record FollowingsResponse(
List<FollowingUserInfo> followings
) {

public static FollowingsResponse of(List<User> users) {
return new FollowingsResponse(FollowingUserInfo.toList(users));
}
}

@Builder
record FollowingUserInfo(
Long id,
String nickname,
String profileImageUrl
) {

public static List<FollowingUserInfo> toList(List<User> users) {
return users.stream()
.map(FollowingUserInfo::of)
.toList();
}

public static FollowingUserInfo of(User user) {
return FollowingUserInfo.builder()
.id(user.getId())
.nickname(user.getNickname())
.profileImageUrl(user.getProfileImageUrl())
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import com.listywave.common.util.UserUtil;
import com.listywave.list.application.domain.CategoryType;
import com.listywave.list.application.domain.Lists;
import com.listywave.user.application.domain.Follow;
import com.listywave.user.application.domain.User;
import com.listywave.user.application.dto.AllUserListsResponse;
import com.listywave.user.application.dto.AllUserResponse;
import com.listywave.user.application.dto.FollowingsResponse;
import com.listywave.user.application.dto.UserInfoResponse;
import com.listywave.user.presentation.dto.response.AllUserListsResponse;
import com.listywave.user.repository.UserRepository;
import com.listywave.user.repository.follow.FollowRepository;
import com.listywave.user.repository.user.UserRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,9 +25,10 @@
@RequiredArgsConstructor
public class UserService {

private final JwtManager jwtManager;
private final UserUtil userUtil;
private final JwtManager jwtManager;
private final UserRepository userRepository;
private final FollowRepository followRepository;

@Transactional(readOnly = true)
public UserInfoResponse getUserInfo(Long userId, String accessToken) {
Expand All @@ -42,7 +46,6 @@ public UserInfoResponse getUserInfo(Long userId, String accessToken) {
return UserInfoResponse.of(foundUser, false, false);
}


private boolean isSignedIn(String accessToken) {
return accessToken.isBlank();
}
Expand Down Expand Up @@ -75,4 +78,15 @@ public AllUserResponse getAllUser() {
List<User> allUser = userRepository.findAll();
return AllUserResponse.of(allUser);
}

public FollowingsResponse getFollowings(String accessToken) {
Long loginUserId = jwtManager.read(accessToken);
User user = userRepository.getById(loginUserId);
List<Follow> follows = followRepository.getAllByFollowerUser(user);

List<User> followingUsers = follows.stream()
.map(Follow::getFollowingUser)
.toList();
return FollowingsResponse.of(followingUsers);
}
}
13 changes: 11 additions & 2 deletions src/main/java/com/listywave/user/presentation/UserController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.listywave.user.presentation;

import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import com.listywave.list.application.domain.CategoryType;
import com.listywave.user.application.dto.AllUserListsResponse;
import com.listywave.user.application.dto.AllUserResponse;
import com.listywave.user.application.dto.FollowingsResponse;
import com.listywave.user.application.dto.UserInfoResponse;
import com.listywave.user.application.service.UserService;
import com.listywave.user.presentation.dto.response.AllUserListsResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -22,7 +25,7 @@ public class UserController {
@GetMapping("/users/{userId}")
ResponseEntity<UserInfoResponse> getUserInfo(
@PathVariable(value = "userId") Long userId,
@RequestHeader(value = "Authorization", defaultValue = "") String accessToken
@RequestHeader(value = AUTHORIZATION, defaultValue = "") String accessToken
) {
UserInfoResponse userInfoResponse = userService.getUserInfo(userId, accessToken);
return ResponseEntity.ok(userInfoResponse);
Expand All @@ -45,4 +48,10 @@ ResponseEntity<AllUserListsResponse> getAllUserLists(
AllUserListsResponse allUserListsResponse = userService.getAllListOfUser(userId, type, category, cursorId, size);
return ResponseEntity.ok(allUserListsResponse);
}

@GetMapping("/followings")
ResponseEntity<FollowingsResponse> getFollowings(@RequestHeader(value = AUTHORIZATION) String accessToken) {
FollowingsResponse response = userService.getFollowings(accessToken);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.listywave.user.repository.follow;

import com.listywave.user.application.domain.Follow;
import com.listywave.user.application.domain.User;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FollowRepository extends JpaRepository<Follow, Long> {

List<Follow> getAllByFollowerUser(User user);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.listywave.user.repository;
package com.listywave.user.repository.user;

import static com.listywave.common.exception.ErrorCode.NOT_FOUND;

import com.listywave.common.exception.CustomException;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.custom.UserRepositoryCustom;
import com.listywave.user.repository.user.custom.CustomUserRepository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {
public interface UserRepository extends JpaRepository<User, Long>, CustomUserRepository {

Optional<User> findByOauthId(Long oauthId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.listywave.user.repository.custom;
package com.listywave.user.repository.user.custom;

import com.listywave.list.application.domain.CategoryType;
import com.listywave.list.application.domain.Lists;
import java.util.List;

public interface UserRepositoryCustom {
public interface CustomUserRepository {

List<Lists> findFeedLists(Long userId, String type, CategoryType category, Long cursorId, int size);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.listywave.user.repository.custom.impl;
package com.listywave.user.repository.user.custom.impl;

import static com.listywave.list.application.domain.QItem.item;
import static com.listywave.list.application.domain.QLists.lists;

import com.listywave.list.application.domain.CategoryType;
import com.listywave.list.application.domain.Lists;
import com.listywave.user.repository.custom.UserRepositoryCustom;
import com.listywave.user.repository.user.custom.CustomUserRepository;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepositoryCustom {
public class CustomUserRepositoryImpl implements CustomUserRepository {

private final JPAQueryFactory queryFactory;

Expand Down

0 comments on commit 98a4861

Please sign in to comment.