Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,6 +1,7 @@
package EatPic.spring.domain.card.controller;

import EatPic.spring.domain.card.dto.request.CardCreateRequest;
import EatPic.spring.domain.card.dto.response.CardResponse;
import EatPic.spring.domain.card.dto.request.CardCreateRequest.CardUpdateRequest;
import EatPic.spring.domain.card.dto.response.CardResponse.CardDetailResponse;
import EatPic.spring.domain.card.dto.response.CardResponse.CardFeedResponse;
Expand All @@ -20,14 +21,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -80,6 +74,14 @@ public ApiResponse<CardFeedResponse> getCardFeed(@PathVariable Long cardId) {
return ApiResponse.onSuccess(cardService.getCardFeed(cardId, userId));
}


@GetMapping("/profile/{userId}/cards")
@Operation(summary = "프로필 화면 피드 미리보기", description = "공유한 카드의 번호와 이미지 url 조회 API")
public ApiResponse<CardResponse.profileCardListDTO> getProfileCardsList(@PathVariable Long userId,
@RequestParam(required = false) Long cursor,
@RequestParam(defaultValue = "15") int size) {
return ApiResponse.onSuccess(cardService.getProfileCardList(userId,size,cursor));

@DeleteMapping("/{cardId}")
@Operation(summary = "카드 삭제", description = "카드를 소프트 삭제 처리합니다.")
public ApiResponse<Void> deleteCard(@PathVariable Long cardId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import EatPic.spring.domain.card.mapping.CardHashtag;
import EatPic.spring.domain.reaction.entity.Reaction;
import EatPic.spring.domain.user.entity.User;
import org.springframework.context.annotation.Profile;
import org.springframework.data.domain.Slice;

import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -110,6 +113,23 @@ public static CardResponse.CardFeedResponse toFeedResponse(
.build();
}

public static CardResponse.ProfileCardDTO toProfileCardDto(Card card){
return CardResponse.ProfileCardDTO.builder()
.cardId(card.getId())
.cardImageUrl(card.getCardImageUrl())
.build();
}

public static CardResponse.profileCardListDTO toProfileCardList(Long userId, Slice<Card> cardList){
return CardResponse.profileCardListDTO.builder()
.hasNext(cardList.hasNext())
.nextCursor(cardList.hasNext()?cardList.getContent().get(cardList.getContent().size()-1).getId():null)
.userId(userId)
.cardsList(cardList.getContent().stream()
.map(CardConverter::toProfileCardDto)
.toList())
.build();

public static TodayCardResponse toTodayCard(Card card) {
return TodayCardResponse.builder()
.cardId(card.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ public static class CardFeedUserDTO {
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class profileCardListDTO{
private Long userId;
private boolean hasNext;
private Long nextCursor;
private List<ProfileCardDTO> cardsList;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ProfileCardDTO {
private Long cardId;
private String cardImageUrl;
}

@Schema(title = "TodayCardResponse: 오늘의 식사(카드) 현황 응답 dto")
public static class TodayCardResponse {
@Schema(description = "카드 ID", example = "12")
Expand All @@ -200,7 +216,4 @@ public static class TodayCardResponse {
private Meal meal;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public interface CardRepository extends JpaRepository<Card, Long> {
ORDER BY c.id ASC
""")
Slice<Card> findByCursor(@Param("cursor") Long cursor, Pageable pageable);

boolean existsByUserIdAndMealAndCreatedAtBetween(Long userId, Meal meal, LocalDateTime start, LocalDateTime end);

Long countCardById(Long id);

Slice<Card> findByUserIdAndIsSharedTrueOrderByIdDesc(Long userId, Pageable pageable);
Slice<Card> findByUserIdAndIsSharedTrueAndIdLessThanOrderByIdDesc(Long userId, Long cursor, Pageable pageable);
Optional<Card> findByIdAndIsDeletedFalse(Long id);

List<Card> findAllByUserAndCreatedAtBetween(User user, LocalDateTime start, LocalDateTime end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public interface CardService {
CardResponse.CreateCardResponse createNewCard(CardCreateRequest.CreateCardRequest request, Long userId);
CardDetailResponse getCardDetail(Long cardId, Long userId);
CardFeedResponse getCardFeed(Long cardId, Long userId);
CardFeedResponse getCardFeed(Long cardId, Long userId)
CardResponse.profileCardListDTO getProfileCardList(Long userId, int size, Long cursor);
void deleteCard(Long cardId, Long userId);
List<TodayCardResponse> getTodayCards(Long userId);
CardDetailResponse updateCard(Long cardId, Long userId, CardUpdateRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import java.time.LocalTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -146,6 +149,19 @@ public CardFeedResponse getCardFeed(Long cardId, Long userId) {
}

@Override
@Transactional(readOnly = true)
public CardResponse.profileCardListDTO getProfileCardList(Long userId, int size, Long cursor) {

Slice<Card> cardSlice;
Pageable pageable = PageRequest.of(0, size);

if (cursor == null) {
cardSlice = cardRepository.findByUserIdAndIsSharedTrueOrderByIdDesc(userId, pageable);
} else {
cardSlice = cardRepository.findByUserIdAndIsSharedTrueAndIdLessThanOrderByIdDesc(userId, cursor, pageable);
}
return CardConverter.toProfileCardList(userId, cardSlice);

@Transactional
public void deleteCard(Long cardId, Long userId) {
Card card = cardRepository.findByIdAndIsDeletedFalse(cardId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UserRestController {
description = "페이지는 1부터 시작하며 total은 전체 항목 수 입니다.")
@GetMapping("/users")
@Tag(name = "User", description = "사용자 관련 API")
public ApiResponse<UserResponseDTO.UserIconListResponseDto> followingUsers(
public ApiResponse<UserResponseDTO.UserIconListResponseDto> followingUsersIcon(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "15") int size) {
//todo : userId -> 본인
Expand All @@ -30,7 +30,7 @@ public ApiResponse<UserResponseDTO.UserIconListResponseDto> followingUsers(
summary = "커뮤니티 상단 팔로잉 유저 아이콘(나)")
@GetMapping("/me")
@Tag(name = "User", description = "사용자 관련 API")
public ApiResponse<UserResponseDTO.ProfileDto> myIcon() {
public ApiResponse<UserResponseDTO.ProfileIconDto> myIcon() {
return ApiResponse.onSuccess(userService.getMyIcon());
}

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

@Operation(summary = "유저 프로필 조회")
@PostMapping("/{userId}/profile")
@Tag(name = "User", description = "사용자 관련 API")
public ApiResponse<UserResponseDTO.DetailProfileDto> getProfile(@PathVariable Long userId) {
return ApiResponse.onSuccess(userService.getProfile(userId));
}

// @Operation(summary = "유저 프로필 조회(이미지)")
// @PostMapping("/{userId}/profile/cards")
// @Tag(name = "User", description = "사용자 관련 API")
// public ApiResponse<UserResponseDTO.DetailProfileDto> getProfile(@PathVariable Long userId) {
// return ApiResponse.onSuccess(userService.getProfile(userId));
// }


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,16 @@
import org.springframework.data.domain.Page;

public class UserConverter {

public static UserResponseDTO.UserIconListResponseDto toUserIconListResponseDto(Page<UserFollow> followingPage){
return UserResponseDTO.UserIconListResponseDto.builder()
.total((int)followingPage.getTotalElements())
.page(followingPage.getNumber()+1)
.size(followingPage.getSize())
.userIconList(followingPage.stream()
.map(UserFollow::getTargetUser)
.map(UserConverter::toProfileIconDto)
.toList())
.build();
}

public static UserResponseDTO.ProfileDto toProfileIconDto(User user){
return UserResponseDTO.ProfileDto.builder()
public static UserResponseDTO.ProfileIconDto toProfileIconDto(User user) {
return UserResponseDTO.ProfileIconDto.builder()
.userId(user.getId())
.profileImageUrl(user.getProfileImageUrl())
.nameId(user.getNameId())
.isFollowing(true)
.nickname(user.getNickname())
.build();
}
// todo: 두개 비슷함 -> 합치기
public static UserResponseDTO.ProfileDto toProfileDto(User user, Boolean isFollowing){

public static UserResponseDTO.ProfileDto toProfileDto(User user, Boolean isFollowing) {
return UserResponseDTO.ProfileDto.builder()
.userId(user.getId())
.profileImageUrl(user.getProfileImageUrl())
Expand All @@ -42,6 +29,38 @@ public static UserResponseDTO.ProfileDto toProfileDto(User user, Boolean isFollo
.isFollowing(isFollowing)
.build();
}

public static UserResponseDTO.DetailProfileDto toDetailProfileDto(
User user,
Boolean isFollowing,
Long totalCard,
Long totalFollower,
Long totalFollowing) {

return UserResponseDTO.DetailProfileDto.builder()
.userId(user.getId())
.profileImageUrl(user.getProfileImageUrl())
.nameId(user.getNameId())
.nickname(user.getNickname())
.isFollowing(isFollowing)
.introduce(user.getIntroduce())
.totalCard(totalCard)
.totalFollower(totalFollower)
.totalFollowing(totalFollowing)
.build();
}

public static UserResponseDTO.UserIconListResponseDto toUserIconListResponseDto(Page<UserFollow> followingPage){
return UserResponseDTO.UserIconListResponseDto.builder()
.total((int)followingPage.getTotalElements())
.page(followingPage.getNumber()+1)
.size(followingPage.getSize())
.userIconList(followingPage.stream()
.map(UserFollow::getTargetUser)
.map(UserConverter::toProfileIconDto)
.toList())
.build();
}

public static ReactionResponseDTO.CardReactionUserListDto toCardReactionUsersListDto(Long cardId, ReactionType reactionType, Page<UserResponseDTO.ProfileDto> profileList){
return ReactionResponseDTO.CardReactionUserListDto.builder()
Expand Down Expand Up @@ -70,4 +89,4 @@ public static UserResponseDTO.UserBlockResponseDto toUserBlockResponseDto(UserBl
.targetUserId(userBlock.getBlockedUser().getId())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@

public class UserResponseDTO {

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class ProfileIconDto {
private Long userId;
private String profileImageUrl;
private String nameId;
private String nickname;
private Boolean isFollowing;
private String introduce;
}

@Getter
@Builder
@AllArgsConstructor
Expand All @@ -21,6 +34,23 @@ public static class ProfileDto {
private Boolean isFollowing;
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class DetailProfileDto {
private Long userId;
private String profileImageUrl;
private String nameId;
private String nickname;
private Boolean isFollowing;
private String introduce;
private Long totalCard;
private Long totalFollower;
private Long totalFollowing;
}


@Getter
@Builder
@AllArgsConstructor
Expand All @@ -29,7 +59,7 @@ public static class UserIconListResponseDto{
private int page;
private int size;
private int total;
private List<ProfileDto> userIconList;
private List<ProfileIconDto> userIconList;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
public interface UserFollowRepository extends JpaRepository<UserFollow,Long> {
Page<UserFollow> findByUser(User user, Pageable pageable);
Boolean existsByUserAndTargetUser(User user, User targetUser);

Long countUserFollowByTargetUser(User targetUser);
Long countUserFollowByUser(User user);
}
19 changes: 17 additions & 2 deletions src/main/java/EatPic/spring/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package EatPic.spring.domain.user.service;

import EatPic.spring.domain.card.repository.CardRepository;
import EatPic.spring.domain.user.converter.UserConverter;
import EatPic.spring.domain.user.dto.response.UserResponseDTO;
import EatPic.spring.domain.user.entity.User;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class UserService {
private final UserFollowRepository userFollowRepository;

private final PasswordEncoder passwordEncoder;
private final CardRepository cardRepository;

public User signup(SignupRequestDTO request) {
// 이메일 중복 검사
Expand Down Expand Up @@ -85,9 +87,22 @@ public UserResponseDTO.UserIconListResponseDto followingUserIconList(Long userId
}

@Transactional
public UserResponseDTO.ProfileDto getMyIcon() {
public UserResponseDTO.ProfileIconDto getMyIcon() {
User me = userRepository.findUserById(1L);
return UserConverter.toProfileDto(me,true);
return UserConverter.toProfileIconDto(me);
}

public UserResponseDTO.DetailProfileDto getProfile(Long userId) {
// todo: 로그인 사용자
User me = userRepository.findUserById(1L);

User user = userRepository.findUserById(userId);
Boolean isFollowing = userFollowRepository.existsByUserAndTargetUser(me, user);
Long totalCard = cardRepository.countCardById(userId);
Long totalFollower = userFollowRepository.countUserFollowByTargetUser(me);
Long totalFollowing = userFollowRepository.countUserFollowByUser(user);

return UserConverter.toDetailProfileDto(user, isFollowing,totalCard,totalFollower,totalFollowing);
}

}