Skip to content

Commit 4397b16

Browse files
authored
Feat: 프로필 화면 API (#79)
## #️⃣연관된 이슈 > 66 ## 📝작업 내용 * 프로필 화면 텍스트 정보(이름, 팔로워 수 등)조회 api * 프로필 화면 피드 미리보기(카드) 조회 api ### 스크린샷 (선택) ## 💬리뷰 요구사항(선택) > 리뷰어가 특별히 봐주었으면 하는 부분이 있다면 작성해주세요 > >
2 parents 97d7d65 + cdf3154 commit 4397b16

File tree

11 files changed

+176
-37
lines changed

11 files changed

+176
-37
lines changed

src/main/java/EatPic/spring/domain/card/controller/CardController.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package EatPic.spring.domain.card.controller;
22

33
import EatPic.spring.domain.card.dto.request.CardCreateRequest;
4+
import EatPic.spring.domain.card.dto.response.CardResponse;
45
import EatPic.spring.domain.card.dto.request.CardCreateRequest.CardUpdateRequest;
56
import EatPic.spring.domain.card.dto.response.CardResponse.CardDetailResponse;
67
import EatPic.spring.domain.card.dto.response.CardResponse.CardFeedResponse;
@@ -20,14 +21,7 @@
2021
import java.util.List;
2122
import lombok.RequiredArgsConstructor;
2223
import org.springframework.http.ResponseEntity;
23-
import org.springframework.web.bind.annotation.DeleteMapping;
24-
import org.springframework.web.bind.annotation.GetMapping;
25-
import org.springframework.web.bind.annotation.PathVariable;
26-
import org.springframework.web.bind.annotation.PostMapping;
27-
import org.springframework.web.bind.annotation.PutMapping;
28-
import org.springframework.web.bind.annotation.RequestBody;
29-
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.RestController;
24+
import org.springframework.web.bind.annotation.*;
3125

3226
@RestController
3327
@RequiredArgsConstructor
@@ -80,6 +74,14 @@ public ApiResponse<CardFeedResponse> getCardFeed(@PathVariable Long cardId) {
8074
return ApiResponse.onSuccess(cardService.getCardFeed(cardId, userId));
8175
}
8276

77+
78+
@GetMapping("/profile/{userId}/cards")
79+
@Operation(summary = "프로필 화면 피드 미리보기", description = "공유한 카드의 번호와 이미지 url 조회 API")
80+
public ApiResponse<CardResponse.profileCardListDTO> getProfileCardsList(@PathVariable Long userId,
81+
@RequestParam(required = false) Long cursor,
82+
@RequestParam(defaultValue = "15") int size) {
83+
return ApiResponse.onSuccess(cardService.getProfileCardList(userId,size,cursor));
84+
8385
@DeleteMapping("/{cardId}")
8486
@Operation(summary = "카드 삭제", description = "카드를 소프트 삭제 처리합니다.")
8587
public ApiResponse<Void> deleteCard(@PathVariable Long cardId) {

src/main/java/EatPic/spring/domain/card/converter/CardConverter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import EatPic.spring.domain.card.mapping.CardHashtag;
1313
import EatPic.spring.domain.reaction.entity.Reaction;
1414
import EatPic.spring.domain.user.entity.User;
15+
import org.springframework.context.annotation.Profile;
16+
import org.springframework.data.domain.Slice;
17+
1518
import java.util.List;
1619
import java.util.stream.Collectors;
1720

@@ -110,6 +113,23 @@ public static CardResponse.CardFeedResponse toFeedResponse(
110113
.build();
111114
}
112115

116+
public static CardResponse.ProfileCardDTO toProfileCardDto(Card card){
117+
return CardResponse.ProfileCardDTO.builder()
118+
.cardId(card.getId())
119+
.cardImageUrl(card.getCardImageUrl())
120+
.build();
121+
}
122+
123+
public static CardResponse.profileCardListDTO toProfileCardList(Long userId, Slice<Card> cardList){
124+
return CardResponse.profileCardListDTO.builder()
125+
.hasNext(cardList.hasNext())
126+
.nextCursor(cardList.hasNext()?cardList.getContent().get(cardList.getContent().size()-1).getId():null)
127+
.userId(userId)
128+
.cardsList(cardList.getContent().stream()
129+
.map(CardConverter::toProfileCardDto)
130+
.toList())
131+
.build();
132+
113133
public static TodayCardResponse toTodayCard(Card card) {
114134
return TodayCardResponse.builder()
115135
.cardId(card.getId())

src/main/java/EatPic/spring/domain/card/dto/response/CardResponse.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ public static class CardFeedUserDTO {
188188
@Getter
189189
@NoArgsConstructor
190190
@AllArgsConstructor
191+
public static class profileCardListDTO{
192+
private Long userId;
193+
private boolean hasNext;
194+
private Long nextCursor;
195+
private List<ProfileCardDTO> cardsList;
196+
}
197+
198+
@Builder
199+
@Getter
200+
@NoArgsConstructor
201+
@AllArgsConstructor
202+
public static class ProfileCardDTO {
203+
private Long cardId;
204+
private String cardImageUrl;
205+
}
206+
191207
@Schema(title = "TodayCardResponse: 오늘의 식사(카드) 현황 응답 dto")
192208
public static class TodayCardResponse {
193209
@Schema(description = "카드 ID", example = "12")
@@ -200,7 +216,4 @@ public static class TodayCardResponse {
200216
private Meal meal;
201217
}
202218

203-
204-
205-
206219
}

src/main/java/EatPic/spring/domain/card/repository/CardRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ public interface CardRepository extends JpaRepository<Card, Long> {
2525
ORDER BY c.id ASC
2626
""")
2727
Slice<Card> findByCursor(@Param("cursor") Long cursor, Pageable pageable);
28+
2829
boolean existsByUserIdAndMealAndCreatedAtBetween(Long userId, Meal meal, LocalDateTime start, LocalDateTime end);
2930

31+
Long countCardById(Long id);
32+
33+
Slice<Card> findByUserIdAndIsSharedTrueOrderByIdDesc(Long userId, Pageable pageable);
34+
Slice<Card> findByUserIdAndIsSharedTrueAndIdLessThanOrderByIdDesc(Long userId, Long cursor, Pageable pageable);
3035
Optional<Card> findByIdAndIsDeletedFalse(Long id);
3136

3237
List<Card> findAllByUserAndCreatedAtBetween(User user, LocalDateTime start, LocalDateTime end);

src/main/java/EatPic/spring/domain/card/service/CardService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
public interface CardService {
1313
CardResponse.CreateCardResponse createNewCard(CardCreateRequest.CreateCardRequest request, Long userId);
1414
CardDetailResponse getCardDetail(Long cardId, Long userId);
15-
CardFeedResponse getCardFeed(Long cardId, Long userId);
15+
CardFeedResponse getCardFeed(Long cardId, Long userId)
16+
CardResponse.profileCardListDTO getProfileCardList(Long userId, int size, Long cursor);
1617
void deleteCard(Long cardId, Long userId);
1718
List<TodayCardResponse> getTodayCards(Long userId);
1819
CardDetailResponse updateCard(Long cardId, Long userId, CardUpdateRequest request);

src/main/java/EatPic/spring/domain/card/service/CardServiceImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
import java.time.LocalTime;
2525
import java.util.Comparator;
2626
import java.util.List;
27-
import java.util.stream.Collectors;
27+
2828
import lombok.RequiredArgsConstructor;
2929
import lombok.extern.slf4j.Slf4j;
30+
import org.springframework.data.domain.PageRequest;
31+
import org.springframework.data.domain.Pageable;
32+
import org.springframework.data.domain.Slice;
3033
import org.springframework.stereotype.Service;
3134
import org.springframework.transaction.annotation.Transactional;
3235

@@ -146,6 +149,19 @@ public CardFeedResponse getCardFeed(Long cardId, Long userId) {
146149
}
147150

148151
@Override
152+
@Transactional(readOnly = true)
153+
public CardResponse.profileCardListDTO getProfileCardList(Long userId, int size, Long cursor) {
154+
155+
Slice<Card> cardSlice;
156+
Pageable pageable = PageRequest.of(0, size);
157+
158+
if (cursor == null) {
159+
cardSlice = cardRepository.findByUserIdAndIsSharedTrueOrderByIdDesc(userId, pageable);
160+
} else {
161+
cardSlice = cardRepository.findByUserIdAndIsSharedTrueAndIdLessThanOrderByIdDesc(userId, cursor, pageable);
162+
}
163+
return CardConverter.toProfileCardList(userId, cardSlice);
164+
149165
@Transactional
150166
public void deleteCard(Long cardId, Long userId) {
151167
Card card = cardRepository.findByIdAndIsDeletedFalse(cardId)

src/main/java/EatPic/spring/domain/user/controller/UserRestController.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class UserRestController {
1919
description = "페이지는 1부터 시작하며 total은 전체 항목 수 입니다.")
2020
@GetMapping("/users")
2121
@Tag(name = "User", description = "사용자 관련 API")
22-
public ApiResponse<UserResponseDTO.UserIconListResponseDto> followingUsers(
22+
public ApiResponse<UserResponseDTO.UserIconListResponseDto> followingUsersIcon(
2323
@RequestParam(defaultValue = "1") int page,
2424
@RequestParam(defaultValue = "15") int size) {
2525
//todo : userId -> 본인
@@ -30,7 +30,7 @@ public ApiResponse<UserResponseDTO.UserIconListResponseDto> followingUsers(
3030
summary = "커뮤니티 상단 팔로잉 유저 아이콘(나)")
3131
@GetMapping("/me")
3232
@Tag(name = "User", description = "사용자 관련 API")
33-
public ApiResponse<UserResponseDTO.ProfileDto> myIcon() {
33+
public ApiResponse<UserResponseDTO.ProfileIconDto> myIcon() {
3434
return ApiResponse.onSuccess(userService.getMyIcon());
3535
}
3636

@@ -41,4 +41,19 @@ public ApiResponse<UserResponseDTO.UserBlockResponseDto> blockUser(@PathVariable
4141
return ApiResponse.onSuccess(userService.blockUser(userId));
4242
}
4343

44+
@Operation(summary = "유저 프로필 조회")
45+
@PostMapping("/{userId}/profile")
46+
@Tag(name = "User", description = "사용자 관련 API")
47+
public ApiResponse<UserResponseDTO.DetailProfileDto> getProfile(@PathVariable Long userId) {
48+
return ApiResponse.onSuccess(userService.getProfile(userId));
49+
}
50+
51+
// @Operation(summary = "유저 프로필 조회(이미지)")
52+
// @PostMapping("/{userId}/profile/cards")
53+
// @Tag(name = "User", description = "사용자 관련 API")
54+
// public ApiResponse<UserResponseDTO.DetailProfileDto> getProfile(@PathVariable Long userId) {
55+
// return ApiResponse.onSuccess(userService.getProfile(userId));
56+
// }
57+
58+
4459
}

src/main/java/EatPic/spring/domain/user/converter/UserConverter.java

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,16 @@
1111
import org.springframework.data.domain.Page;
1212

1313
public class UserConverter {
14-
15-
public static UserResponseDTO.UserIconListResponseDto toUserIconListResponseDto(Page<UserFollow> followingPage){
16-
return UserResponseDTO.UserIconListResponseDto.builder()
17-
.total((int)followingPage.getTotalElements())
18-
.page(followingPage.getNumber()+1)
19-
.size(followingPage.getSize())
20-
.userIconList(followingPage.stream()
21-
.map(UserFollow::getTargetUser)
22-
.map(UserConverter::toProfileIconDto)
23-
.toList())
24-
.build();
25-
}
26-
27-
public static UserResponseDTO.ProfileDto toProfileIconDto(User user){
28-
return UserResponseDTO.ProfileDto.builder()
14+
public static UserResponseDTO.ProfileIconDto toProfileIconDto(User user) {
15+
return UserResponseDTO.ProfileIconDto.builder()
2916
.userId(user.getId())
3017
.profileImageUrl(user.getProfileImageUrl())
3118
.nameId(user.getNameId())
32-
.isFollowing(true)
19+
.nickname(user.getNickname())
3320
.build();
3421
}
35-
// todo: 두개 비슷함 -> 합치기
36-
public static UserResponseDTO.ProfileDto toProfileDto(User user, Boolean isFollowing){
22+
23+
public static UserResponseDTO.ProfileDto toProfileDto(User user, Boolean isFollowing) {
3724
return UserResponseDTO.ProfileDto.builder()
3825
.userId(user.getId())
3926
.profileImageUrl(user.getProfileImageUrl())
@@ -42,6 +29,38 @@ public static UserResponseDTO.ProfileDto toProfileDto(User user, Boolean isFollo
4229
.isFollowing(isFollowing)
4330
.build();
4431
}
32+
33+
public static UserResponseDTO.DetailProfileDto toDetailProfileDto(
34+
User user,
35+
Boolean isFollowing,
36+
Long totalCard,
37+
Long totalFollower,
38+
Long totalFollowing) {
39+
40+
return UserResponseDTO.DetailProfileDto.builder()
41+
.userId(user.getId())
42+
.profileImageUrl(user.getProfileImageUrl())
43+
.nameId(user.getNameId())
44+
.nickname(user.getNickname())
45+
.isFollowing(isFollowing)
46+
.introduce(user.getIntroduce())
47+
.totalCard(totalCard)
48+
.totalFollower(totalFollower)
49+
.totalFollowing(totalFollowing)
50+
.build();
51+
}
52+
53+
public static UserResponseDTO.UserIconListResponseDto toUserIconListResponseDto(Page<UserFollow> followingPage){
54+
return UserResponseDTO.UserIconListResponseDto.builder()
55+
.total((int)followingPage.getTotalElements())
56+
.page(followingPage.getNumber()+1)
57+
.size(followingPage.getSize())
58+
.userIconList(followingPage.stream()
59+
.map(UserFollow::getTargetUser)
60+
.map(UserConverter::toProfileIconDto)
61+
.toList())
62+
.build();
63+
}
4564

4665
public static ReactionResponseDTO.CardReactionUserListDto toCardReactionUsersListDto(Long cardId, ReactionType reactionType, Page<UserResponseDTO.ProfileDto> profileList){
4766
return ReactionResponseDTO.CardReactionUserListDto.builder()
@@ -70,4 +89,4 @@ public static UserResponseDTO.UserBlockResponseDto toUserBlockResponseDto(UserBl
7089
.targetUserId(userBlock.getBlockedUser().getId())
7190
.build();
7291
}
73-
}
92+
}

src/main/java/EatPic/spring/domain/user/dto/response/UserResponseDTO.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99

1010
public class UserResponseDTO {
1111

12+
@Getter
13+
@Builder
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public static class ProfileIconDto {
17+
private Long userId;
18+
private String profileImageUrl;
19+
private String nameId;
20+
private String nickname;
21+
private Boolean isFollowing;
22+
private String introduce;
23+
}
24+
1225
@Getter
1326
@Builder
1427
@AllArgsConstructor
@@ -21,6 +34,23 @@ public static class ProfileDto {
2134
private Boolean isFollowing;
2235
}
2336

37+
@Getter
38+
@Builder
39+
@AllArgsConstructor
40+
@NoArgsConstructor
41+
public static class DetailProfileDto {
42+
private Long userId;
43+
private String profileImageUrl;
44+
private String nameId;
45+
private String nickname;
46+
private Boolean isFollowing;
47+
private String introduce;
48+
private Long totalCard;
49+
private Long totalFollower;
50+
private Long totalFollowing;
51+
}
52+
53+
2454
@Getter
2555
@Builder
2656
@AllArgsConstructor
@@ -29,7 +59,7 @@ public static class UserIconListResponseDto{
2959
private int page;
3060
private int size;
3161
private int total;
32-
private List<ProfileDto> userIconList;
62+
private List<ProfileIconDto> userIconList;
3363
}
3464

3565
@Getter

src/main/java/EatPic/spring/domain/user/repository/UserFollowRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
public interface UserFollowRepository extends JpaRepository<UserFollow,Long> {
1313
Page<UserFollow> findByUser(User user, Pageable pageable);
1414
Boolean existsByUserAndTargetUser(User user, User targetUser);
15+
16+
Long countUserFollowByTargetUser(User targetUser);
17+
Long countUserFollowByUser(User user);
1518
}

0 commit comments

Comments
 (0)