Skip to content

Commit

Permalink
Merge pull request #316 from GEON-PPANG/chore/#313
Browse files Browse the repository at this point in the history
[CHORE] MemberService 분리
  • Loading branch information
seunghaLim authored Sep 22, 2024
2 parents ca6e32b + 36af106 commit bedc4b6
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.org.gunbbang.service.BakeryService;
import com.org.gunbbang.service.MemberService;
import com.org.gunbbang.service.ReviewService;
import com.org.gunbbang.service.*;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -29,11 +31,12 @@ public class MemberController {
private final BakeryService bakeryService;
private final JwtService jwtService;
private final AmplitudeService amplitudeService;
private final MemberTypeService memberTypeService;

@GetMapping(value = "", name = "유저_상세정보_조회")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<MemberDetailResponseDTO> getMemberDetail() {
MemberDetailResponseDTO memberDetailResponseDto = memberService.getMemberDetail();
MemberDetailResponseDTO memberDetailResponseDto = memberTypeService.getMemberDetail();
return ApiResponse.success(SuccessType.GET_MYPAGE_SUCCESS, memberDetailResponseDto);
}

Expand All @@ -44,14 +47,14 @@ public ApiResponse<MemberTypeResponseDTO> updateMemberTypes(
String nickname = SecurityUtil.getLoginMemberNickname();
return ApiResponse.success(
SuccessType.UPDATE_MEMBER_TYPES_SUCCESS,
memberService.updateMemberTypes(request, memberId, nickname));
memberTypeService.updateMemberTypes(request, memberId, nickname));
}

@GetMapping(value = "/types", name = "유저_필터칩_조회")
public ApiResponse<MemberTypeResponseDTO> getMemberTypes() {
Map<String, Object> loginMemberInfo = SecurityUtil.getLoginMemberInfo();
return ApiResponse.success(
SuccessType.GET_MEMBER_TYPES_SUCCESS, memberService.getMemberTypes(loginMemberInfo));
SuccessType.GET_MEMBER_TYPES_SUCCESS, memberTypeService.getMemberTypes(loginMemberInfo));
}

@GetMapping(value = "/reviews", name = "유저_리뷰_목록_조회")
Expand Down
113 changes: 0 additions & 113 deletions api/src/main/java/com/org/gunbbang/service/BakeryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,130 +27,17 @@
@Transactional
@RequiredArgsConstructor
public class BakeryService {
private final CategoryRepository categoryRepository;
private final BookMarkRepository bookMarkRepository;
private final MemberRepository memberRepository;
private final BakeryRepository bakeryRepository;
private final MenuRepository menuRepository;
private final BreadTypeRepository breadTypeRepository;
private final MemberBreadTypeRepository memberBreadTypeRepository;
private final MemberNutrientTypeRepository memberNutrientTypeRepository;
private final BakeryBreadTypeRepository bakeryBreadTypeRepository;
private final NutrientTypeRepository nutrientTypeRepository;

private final String BLANK_SPACE = " ";
private final int MAX_BEST_BAKERY_COUNT = 10;

// public Page<BakeryListResponseDTO> getBakeryList(
// String sortingOption,
// boolean personalFilter,
// boolean isHard,
// boolean isDessert,
// boolean isBrunch,
// PageRequest pageRequest) {
// List<Category> categoryList = getCategoryList(isHard, isDessert, isBrunch);
// Long memberId = personalFilter ? SecurityUtil.getUserId().orElse(null) : null;
//
// Page<Bakery> bakeryList =
// getFilteredAndSortedBakeryList(
// personalFilter, categoryList, sortingOption, memberId, pageRequest);
// return getBakeryListResponseDTOList(bakeryList);
// }
//
// private List<Category> getCategoryList(boolean isHard, boolean isDessert, boolean isBrunch) {
// List<Category> categoryList = new ArrayList<>();
//
// Map<CategoryType, Boolean> categoryMap = new HashMap<>();
// categoryMap.put(CategoryType.HARD_BREAD, isHard);
// categoryMap.put(CategoryType.DESSERT, isDessert);
// categoryMap.put(CategoryType.BRUNCH, isBrunch);
//
// for (Map.Entry<CategoryType, Boolean> entry : categoryMap.entrySet()) {
// if (entry.getValue()) { // true인 값만 해당되어 category에 추가된다
// Category category =
// categoryRepository
// .findByCategoryName(entry.getKey().getName())
// .orElseThrow(
// () ->
// new NotFoundException(
// ErrorType.NOT_FOUND_CATEGORY_EXCEPTION,
// ErrorType.NOT_FOUND_CATEGORY_EXCEPTION.getMessage()
// + entry.getKey().getName()));
// categoryList.add(category);
// }
// }
//
// if (categoryList.isEmpty()) { // 카테고리가 빈 경우
// for (CategoryType categoryType : CategoryType.values()) {
// Category category =
// categoryRepository
// .findByCategoryName(categoryType.getName())
// .orElseThrow(
// () ->
// new NotFoundException(
// ErrorType.NOT_FOUND_CATEGORY_EXCEPTION,
// ErrorType.NOT_FOUND_CATEGORY_EXCEPTION.getMessage()
// + categoryType.getName()));
// categoryList.add(category);
// }
// }
//
// return categoryList;
// }

// private Page<Bakery> getFilteredAndSortedBakeryList(
// boolean personalFilter,
// List<Category> categoryList,
// String sortingOption,
// Long memberId,
// PageRequest pageRequest) {
// Page<Bakery> getSortedByCategoryBakeryList;
// if (personalFilter) {
// final List<MemberBreadType> memberBreadType =
// memberBreadTypeRepository.findAllByMemberId(memberId);
//
// if (memberBreadType.isEmpty()) {
// throw new NotFoundException(
// ErrorType.REQUEST_VALIDATION_EXCEPTION,
// ErrorType.REQUEST_VALIDATION_EXCEPTION.getMessage());
// }
//
// final List<BreadType> breadType =
//
// memberBreadType.stream().map(MemberBreadType::getBreadType).collect(Collectors.toList());
// final List<MemberNutrientType> memberNutrientTypes =
// memberNutrientTypeRepository.findAllByMemberId(memberId);
// final MemberNutrientType memberNutrientType = memberNutrientTypes.get(0);
// final NutrientType bakeryNutrientType = memberNutrientType.getNutrientType();
//
// if ("review".equals(sortingOption)) {
// getSortedByCategoryBakeryList =
// bakeryRepository.findFilteredBakeriesSortByReview(
// categoryList, breadType, bakeryNutrientType, pageRequest);
// return getSortedByCategoryBakeryList;
// }
// getSortedByCategoryBakeryList =
// bakeryRepository.findFilteredBakeries(
// categoryList, breadType, bakeryNutrientType, pageRequest);
// return getSortedByCategoryBakeryList;
// }

// final List<BreadType> breadType = breadTypeRepository.findAll();
// NutrientType bakeryNutrientType =
// nutrientTypeRepository.findByNutrientTypeTag(NutrientTypeTag.NOT_OPEN).orElse(null);
//
// if ("review".equals(sortingOption)) {
// getSortedByCategoryBakeryList =
// bakeryRepository.findFilteredBakeriesSortByReview(
// categoryList, breadType, bakeryNutrientType, pageRequest);
// return getSortedByCategoryBakeryList;
// }
// getSortedByCategoryBakeryList =
// bakeryRepository.findFilteredBakeries(
// categoryList, breadType, bakeryNutrientType, pageRequest);
// return getSortedByCategoryBakeryList;
// }

public BakeryDetailResponseDTO getBakeryDetail(Long bakeryId) {
final Bakery bakery =
bakeryRepository
Expand Down
144 changes: 2 additions & 142 deletions api/src/main/java/com/org/gunbbang/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

import com.org.gunbbang.*;
import com.org.gunbbang.auth.jwt.service.AppleJwtService;
import com.org.gunbbang.auth.security.util.SecurityUtil;
import com.org.gunbbang.controller.DTO.request.MemberTypesRequestDTO;
import com.org.gunbbang.controller.DTO.response.*;
import com.org.gunbbang.entity.*;
import com.org.gunbbang.entity.BreadType;
import com.org.gunbbang.support.errorType.ErrorType;
import com.org.gunbbang.repository.*;
import com.org.gunbbang.support.exception.BadRequestException;
import com.org.gunbbang.support.exception.NotFoundException;
import com.org.gunbbang.util.mapper.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -28,145 +22,10 @@
public class MemberService {

private final MemberRepository memberRepository;
private final BreadTypeRepository breadTypeRepository;
private final NutrientTypeRepository nutrientTypeRepository;
private final AppleJwtService appleJWTService;
private final BookMarkRepository bookMarkRepository;
private final MemberBreadTypeRepository memberBreadTypeRepository;
private final MemberNutrientTypeRepository memberNutrientTypeRepository;

public MemberDetailResponseDTO getMemberDetail() {
String memberNickname = SecurityUtil.getLoginMemberNickname();
MainPurpose memberMainPurpose = SecurityUtil.getLoginMemberMainPurpose();
// Long memberBreadTypeId = SecurityUtil.getLoginMemberBreadTypeId();
Member foundMember =
memberRepository
.findByNickname(memberNickname)
.orElseThrow(
() ->
new NotFoundException(
ErrorType.NOT_FOUND_USER_EXCEPTION,
ErrorType.NOT_FOUND_USER_EXCEPTION.getMessage() + memberNickname));

List<MemberBreadType> breadType = memberBreadTypeRepository.findAllByMember(foundMember);
List<BreadTypeResponseDTO> breadTypeResponseDTO =
MemberBreadTypeMapper.INSTANCE.toBreadTypeResponseDTOList(breadType);

return MemberTypeMapper.INSTANCE.toMemberDetailResponseDTO(
memberNickname, memberMainPurpose, breadTypeResponseDTO);
}

public MemberTypeResponseDTO updateMemberTypes(
MemberTypesRequestDTO request, Long memberId, String nickname) {
Member foundMember =
memberRepository
.findById(memberId)
.orElseThrow(
() ->
new NotFoundException(
ErrorType.NOT_FOUND_USER_EXCEPTION,
ErrorType.NOT_FOUND_USER_EXCEPTION.getMessage() + memberId));

if (memberBreadTypeRepository.existsByMember(foundMember)) {
memberBreadTypeRepository.deleteAllByMember(foundMember); // 기존에 있던 MemberBreadType 전부 삭제
}

List<MemberBreadType> memberBreadTypes =
saveBreadTypes(request.getBreadTypeList(), foundMember);

if (memberNutrientTypeRepository.existsByMember(foundMember)) {
memberNutrientTypeRepository.deleteAllByMember(
foundMember); // 기존에 있던 MemberNutrientType 전부 삭제
}

List<MemberNutrientType> memberNutrientTypes =
saveNutrientTypes(request.getNutrientTypeList(), foundMember);

foundMember.updateMainPurpose(request.getMainPurpose());
memberRepository.saveAndFlush(foundMember);

List<BreadTypeResponseDTO> breadTypeResponseDTO =
MemberBreadTypeMapper.INSTANCE.toBreadTypeResponseDTOList(memberBreadTypes);
List<NutrientTypeResponseDTO> nutrientTypeResponseDTO =
MemberNutrientTypeMapper.INSTANCE.toNutrientTypeResponseDTOList(memberNutrientTypes);

return MemberTypeMapper.INSTANCE.toMemberTypeResponseDTO(
foundMember.getMemberId(),
foundMember.getMainPurpose(),
nickname,
breadTypeResponseDTO,
nutrientTypeResponseDTO);
}

private List<MemberBreadType> saveBreadTypes(List<Long> breadTypeIds, Member foundMember) {
List<MemberBreadType> memberBreadTypes = new ArrayList<>();

for (Long breadTypeId : breadTypeIds) {
memberBreadTypes.add(createMemberBreadTypeById(foundMember, breadTypeId));
}
return memberBreadTypeRepository.saveAllAndFlush(memberBreadTypes);
}

private MemberBreadType createMemberBreadTypeById(Member foundMember, Long breadTypeId) {
BreadType foundBreadType =
breadTypeRepository
.findByBreadTypeId(breadTypeId)
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_BREAD_TYPE_EXCEPTION));

MemberBreadType memberBreadType =
MemberBreadType.builder().member(foundMember).breadType(foundBreadType).build();
return memberBreadType;
}

private List<MemberNutrientType> saveNutrientTypes(
List<Long> nutrientTypeIds, Member foundMember) {
List<MemberNutrientType> memberNutrientTypes = new ArrayList<>();

for (Long nutrientTypeId : nutrientTypeIds) {
memberNutrientTypes.add(createMemberNutrientTypeById(foundMember, nutrientTypeId));
}

return memberNutrientTypeRepository.saveAllAndFlush(memberNutrientTypes);
}

private MemberNutrientType createMemberNutrientTypeById(Member foundMember, Long nutrientTypeId) {
NutrientType foundNutrientType =
nutrientTypeRepository
.findByNutrientTypeId(nutrientTypeId)
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_NUTRIENT_EXCEPTION));

MemberNutrientType memberNutrientType =
MemberNutrientType.builder().member(foundMember).nutrientType(foundNutrientType).build();
return memberNutrientType;
}

public MemberTypeResponseDTO getMemberTypes(Map<String, Object> loginMemberInfo) {
Long memberId = Long.parseLong(loginMemberInfo.get("memberId").toString());
Member member =
memberRepository
.findById(memberId)
.orElseThrow(
() ->
new NotFoundException(
ErrorType.NOT_FOUND_USER_EXCEPTION,
ErrorType.NOT_FOUND_USER_EXCEPTION.getMessage() + memberId));
List<MemberBreadType> memberBreadTypes = memberBreadTypeRepository.findAllByMember(member);
List<MemberNutrientType> memberNutrientTypes =
memberNutrientTypeRepository.findAllByMemberId(memberId);

List<BreadTypeResponseDTO> breadTypeResponseDTO =
MemberBreadTypeMapper.INSTANCE.toBreadTypeResponseDTOList(memberBreadTypes);
List<NutrientTypeResponseDTO> nutrientTypeResponseDTO =
MemberNutrientTypeMapper.INSTANCE.toNutrientTypeResponseDTOList(memberNutrientTypes);

return MemberTypeMapper.INSTANCE.toMemberTypeResponseDTO(
memberId,
(MainPurpose) loginMemberInfo.get("mainPurpose"),
loginMemberInfo.get("nickname").toString(),
breadTypeResponseDTO,
nutrientTypeResponseDTO);
}

@Transactional(readOnly = true)
public ValidationResponseDTO checkDuplicatedNickname(String nickname) {
if (memberRepository.findByNickname(nickname).isPresent()) {
throw new BadRequestException(ErrorType.ALREADY_EXIST_NICKNAME_EXCEPTION);
Expand All @@ -175,6 +34,7 @@ public ValidationResponseDTO checkDuplicatedNickname(String nickname) {
return ValidationResponseDTO.builder().isAvailable(true).build();
}

@Transactional(readOnly = true)
public ValidationResponseDTO checkDuplicatedEmail(String email) {
if (!memberRepository.findAllByEmail(email).isEmpty()) {
throw new BadRequestException(ErrorType.ALREADY_EXIST_EMAIL_EXCEPTION);
Expand Down
Loading

0 comments on commit bedc4b6

Please sign in to comment.