Skip to content

Commit

Permalink
♻️ [REFACTOR] 마이페이지 조회 로직 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
dong2ast committed Mar 19, 2024
1 parent 3656d46 commit dec663c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.sopt.lequuServer.domain.book.repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.global.exception.enums.ErrorType;
import org.sopt.lequuServer.global.exception.model.CustomException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.query.Param;

public interface BookRepository extends JpaRepository<Book, Long> {

Expand All @@ -21,6 +21,9 @@ default Book findByIdOrThrow(Long id) {
() -> new CustomException(ErrorType.NOT_FOUND_BOOK_ERROR));
}

@Query("select b from Book b where b.member.id = :memberId")
List<Book> findByMemberId(@Param("memberId") Long id);

Optional<Book> findByUuid(String uuid);

default Book findByUuidOrThrow(String uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.favorite.model.Favorite;
import org.sopt.lequuServer.domain.member.model.Member;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
List<Favorite> findByMemberOrderByCreatedAtDesc(Member member);

Optional<Favorite> findByMemberAndBook(Member member, Book book);

List<Favorite> findByMember(Member member);

@EntityGraph(attributePaths = {"book"})
@Query("select f from Favorite f where f.member.id = :memberId")
List<Favorite> findByMemberId(@Param("memberId") Long memberId);
}

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.sopt.lequuServer.domain.member.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.member.model.Member;
import static java.util.Comparator.comparing;

import io.swagger.v3.oas.annotations.media.Schema;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static java.util.Comparator.comparing;
import org.sopt.lequuServer.domain.book.model.Book;

public record MypageBookResponseDto(

Expand All @@ -32,26 +30,21 @@ public record MypageBookResponseDto(
@Schema(description = "레큐노트 개수", example = "1974")
int noteNum
) {
public static List<MypageBookResponseDto> of(Member member, List<Book> books) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
public static List<MypageBookResponseDto> of(List<Book> books, List<Book> favoriteBooks) {

List<MypageBookResponseDto> bookList = books.stream()
return books.stream()
.sorted(comparing(Book::getCreatedAt).reversed())
.map(book -> {
boolean isFavorite = member.getFavorites().stream()
.anyMatch(favorite -> favorite.getBook().getId().equals(book.getId()));
return new MypageBookResponseDto(
.map(book ->
new MypageBookResponseDto(
book.getId(),
book.getUuid(),
isFavorite,
favoriteBooks.contains(book),
book.getFavoriteName(),
book.getTitle(),
book.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")),
book.getNotes().size()
);
})
)
)
.toList();

return bookList;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package org.sopt.lequuServer.domain.member.service;

import static org.sopt.lequuServer.global.exception.enums.ErrorType.INVALID_SOCIAL_ACCESS_TOKEN;
import static org.sopt.lequuServer.global.exception.enums.ErrorType.INVALID_TOKEN_HEADER_ERROR;
import static org.sopt.lequuServer.global.exception.enums.ErrorType.NOT_FOUND_MEMBER_ERROR;

import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.book.repository.BookRepository;
import org.sopt.lequuServer.domain.favorite.dto.response.FavoriteBookResponseDto;
import org.sopt.lequuServer.domain.favorite.model.Favorite;
import org.sopt.lequuServer.domain.favorite.repository.FavoriteRepository;
import org.sopt.lequuServer.domain.member.dto.request.MemberNicknameRequestDto;
import org.sopt.lequuServer.domain.member.dto.request.SocialLoginRequestDto;
import org.sopt.lequuServer.domain.member.dto.response.*;
import org.sopt.lequuServer.domain.member.dto.response.MemberLoginResponseDto;
import org.sopt.lequuServer.domain.member.dto.response.MemberNicknameResponseDto;
import org.sopt.lequuServer.domain.member.dto.response.MypageBookResponseDto;
import org.sopt.lequuServer.domain.member.dto.response.MypageNoteResponseDto;
import org.sopt.lequuServer.domain.member.dto.response.MypageResponseDto;
import org.sopt.lequuServer.domain.member.model.Member;
import org.sopt.lequuServer.domain.member.model.SocialPlatform;
import org.sopt.lequuServer.domain.member.repository.MemberRepository;
Expand All @@ -24,24 +35,28 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static org.sopt.lequuServer.global.exception.enums.ErrorType.*;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberService {

private final MemberRepository memberRepository;
private final BookRepository bookRepository;
private final FavoriteRepository favoriteRepository;

private final JwtProvider jwtProvider;
private final KakaoLoginService kakaoLoginService;
private final BadWordFilterService badWordFilterService;

private static String parseTokenString(String tokenString) {
String[] strings = tokenString.split(" ");
if (strings.length != 2) {
throw new CustomException(INVALID_TOKEN_HEADER_ERROR);
}
return strings[1];
}

@Transactional
public MemberLoginResponseDto login(String socialAccessToken, SocialLoginRequestDto request) {

Expand All @@ -53,8 +68,8 @@ public MemberLoginResponseDto login(String socialAccessToken, SocialLoginRequest
boolean isRegistered = isUserBySocialAndSocialId(socialPlatform, socialId);
if (!isRegistered) {
Member member = Member.builder()
.socialPlatform(socialPlatform)
.socialId(socialId).build();
.socialPlatform(socialPlatform)
.socialId(socialId).build();

memberRepository.save(member);
}
Expand All @@ -69,7 +84,8 @@ public MemberLoginResponseDto login(String socialAccessToken, SocialLoginRequest
log.info(LoggingMessage.memberRegisterLogMessage(loginMember));
}

TokenDto tokenDto = jwtProvider.issueToken(new UserAuthentication(loginMember.getId(), null, null));
TokenDto tokenDto = jwtProvider.issueToken(
new UserAuthentication(loginMember.getId(), null, null));

return MemberLoginResponseDto.of(loginMember, tokenDto);
}
Expand Down Expand Up @@ -99,7 +115,7 @@ private void validateMemberId(Long memberId) {

private Member getUserBySocialAndSocialId(SocialPlatform socialPlatform, String socialId) {
return memberRepository.findBySocialPlatformAndSocialId(socialPlatform, socialId)
.orElseThrow(() -> new CustomException(NOT_FOUND_MEMBER_ERROR));
.orElseThrow(() -> new CustomException(NOT_FOUND_MEMBER_ERROR));
}

private boolean isUserBySocialAndSocialId(SocialPlatform socialPlatform, String socialId) {
Expand All @@ -113,34 +129,26 @@ private String login(SocialPlatform socialPlatform, String socialAccessToken) {
};
}

private static String parseTokenString(String tokenString) {
String[] strings = tokenString.split(" ");
if (strings.length != 2) {
throw new CustomException(INVALID_TOKEN_HEADER_ERROR);
}
return strings[1];
}

@Transactional
public MemberNicknameResponseDto setMemberNickname(Long memberId, MemberNicknameRequestDto request) {
public MemberNicknameResponseDto setMemberNickname(Long memberId,
MemberNicknameRequestDto request) {
if (memberRepository.existsByNickname(request.nickname())) {
throw new CustomException(ErrorType.NICKNAME_DUP_ERROR);
}

Member member = memberRepository.findByIdOrThrow(memberId);
member.updateNickname(badWordFilterService.badWordChange(memberId, request.nickname().strip()));
member.updateNickname(
badWordFilterService.badWordChange(memberId, request.nickname().strip()));
return MemberNicknameResponseDto.of(memberId);
}

public List<MypageBookResponseDto> getMypageBook(Long memberId) {

// 회원 id 찾기
Member member = memberRepository.findByIdOrThrow(memberId);

// 회원이 소유한 Book 리스트 가져오기
List<Book> books = member.getBooks();
List<Book> byBook = bookRepository.findByMemberId(memberId);
List<Book> favoriteBooks = favoriteRepository.findByMemberId(memberId).stream()
.map(Favorite::getBook).toList();

return MypageBookResponseDto.of(member, books);
return MypageBookResponseDto.of(byBook, favoriteBooks);
}

public List<MypageNoteResponseDto> getMypageNote(Long memberId) {
Expand All @@ -159,8 +167,8 @@ public List<FavoriteBookResponseDto> getMypageFavorite(Long memberId) {
List<Favorite> favorites = favoriteRepository.findByMember(member);

return favorites.stream()
.map(favorite -> FavoriteBookResponseDto.of(favorite.getBook()))
.collect(Collectors.toList());
.map(favorite -> FavoriteBookResponseDto.of(favorite.getBook()))
.collect(Collectors.toList());
}

public MypageResponseDto getMypage(Long memberId) {
Expand Down

0 comments on commit dec663c

Please sign in to comment.