Skip to content

Commit

Permalink
[feat] 마이페이지 족보 리스트 API (#58)
Browse files Browse the repository at this point in the history
* [refac] change variable name

* [feat] set repository

* [feat] add service logic

* [feat] add controller logic

* [refac] refactor entitiy code

* [refac] change JPQL to Query Method
  • Loading branch information
PicturePark1101 authored Jul 14, 2024
1 parent 053ed20 commit e397810
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public Favorite findById(final Long id) {
}

public List<Favorite> findAllByIds(final List<Long> ids) {
return favoriteRepository.findAllByIds(ids);
return favoriteRepository.findByIdIn(ids);
}

public List<Favorite> findAllByUserId(final Long userId) {
return favoriteRepository.findByUserIdOrderByCreatedAtDesc(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hankki.hankkiserver.api.user.service.UserCommandService;
import org.hankki.hankkiserver.api.user.service.UserQueryService;
import org.hankki.hankkiserver.api.user.service.command.UserUniversityPostCommand;
import org.hankki.hankkiserver.api.user.service.response.UserFavoritesGetResponse;
import org.hankki.hankkiserver.api.user.service.response.UserProfileAndNicknameResponse;
import org.hankki.hankkiserver.api.user.service.response.UserUniversityFindResponse;
import org.hankki.hankkiserver.auth.UserId;
Expand Down Expand Up @@ -35,6 +36,11 @@ public HankkiResponse<UserUniversityFindResponse> findUserUniversity(@UserId fin

@GetMapping("/users/me")
public HankkiResponse<UserProfileAndNicknameResponse> getUserProfileAndNickname(@UserId final Long userId) {
return HankkiResponse.success(CommonSuccessCode.OK, userQueryService.getUserProfileAndNickname(userId));
return HankkiResponse.success(CommonSuccessCode.OK, userQueryService.getUserProfileAndNickname(userId));
}

@GetMapping("/users/me/favorites")
public HankkiResponse<UserFavoritesGetResponse> findUserFavorites(@UserId final Long userId) {
return HankkiResponse.success(CommonSuccessCode.OK,userQueryService.findUserFavorites(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.auth.service.UserInfoFinder;
import org.hankki.hankkiserver.api.favorite.service.FavoriteFinder;
import org.hankki.hankkiserver.api.user.service.response.UserFavoritesGetResponse;
import org.hankki.hankkiserver.api.user.service.response.UserProfileAndNicknameResponse;
import org.hankki.hankkiserver.api.user.service.response.UserUniversityFindResponse;
import org.hankki.hankkiserver.common.code.UserUniversityErrorCode;
Expand All @@ -14,6 +16,7 @@
public class UserQueryService {

private final UserUniversityFinder userUniversityFinder;
private final FavoriteFinder favoriteFinder;
private final UserInfoFinder userInfoFinder;

@Transactional(readOnly = true)
Expand All @@ -22,8 +25,13 @@ public UserUniversityFindResponse findUserUniversity(Long userId) {
.orElseThrow(() -> new NotFoundException(UserUniversityErrorCode.USER_UNIVERSITY_NOT_FOUND)));
}

@Transactional(readOnly = true)
public UserFavoritesGetResponse findUserFavorites(final Long userId) {
return UserFavoritesGetResponse.of(favoriteFinder.findAllByUserId(userId));
}

@Transactional(readOnly = true)
public UserProfileAndNicknameResponse getUserProfileAndNickname(final Long userId) {
return UserProfileAndNicknameResponse.of(userInfoFinder.getUserInfo(userId));
return UserProfileAndNicknameResponse.of(userInfoFinder.getUserInfo(userId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hankki.hankkiserver.api.user.service.response;

import org.hankki.hankkiserver.domain.favorite.model.Favorite;

public record UserFavoriteResponse(
Long id,
String title,
String imageUrl
) {

public static UserFavoriteResponse of(Favorite favorite) {
return new UserFavoriteResponse(favorite.getId(), favorite.getName(), favorite.getImageUrl());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hankki.hankkiserver.api.user.service.response;

import java.util.List;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;

public record UserFavoritesGetResponse(
List<UserFavoriteResponse> favorites
) {

public static UserFavoritesGetResponse of(List<Favorite> favorites) {
return new UserFavoritesGetResponse(favorites.stream().map(UserFavoriteResponse::of).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Favorite extends BaseTimeEntity {
private String detail;

@Column(nullable = false)
private String image_url;
private String imageUrl;

@OneToMany(mappedBy = "favorite")
@BatchSize(size = 100)
Expand All @@ -51,6 +51,6 @@ private Favorite(User user, String name, String detail) {
this.user = user;
this.name = name;
this.detail = detail;
this.image_url = "default.com";
this.imageUrl = "default.com";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
@Query("delete from Favorite f where f in :favorites")
void deleteAll(@Param("favorites") List<Favorite> favorites);

@Query("select f from Favorite f where f.id in :favoriteId")
List<Favorite> findAllByIds(@Param("favoriteId") List<Long> favoriteId);
}
List<Favorite> findByIdIn(@Param("favoriteId") List<Long> favoriteId);

List<Favorite> findByUserIdOrderByCreatedAtDesc(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ public void decreaseHeartCount() {
public void increaseHeartCount() {
this.heartCount++;
}

}

0 comments on commit e397810

Please sign in to comment.