-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REFACTOR] 윤한 담당 API 리팩토링 #167
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ public ResponseEntity<ApiResponse> updateUserProfile(Principal principal, @Reque | |
, content = @Content(schema = @Schema(implementation = MemberGetResponseDTO.class)))}) | ||
@GetMapping("/me") | ||
public ResponseEntity<ApiResponse> getUserProfile(Principal principal) { | ||
MemberGetResponseDTO response = memberService.getMember(getMemberId(principal)); | ||
MemberGetResponseDTO response = memberService.getMemberProfile(getMemberId(principal)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Entity명을 제외하기로 했으니 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 것 같아요 수정하겠습니다! |
||
return ResponseEntity.ok(success(SUCCESS_GET_USER.getMessage(), response)); | ||
} | ||
|
||
|
@@ -73,7 +73,7 @@ public ResponseEntity<ApiResponse> checkDuplicatedName(@Parameter(description = | |
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "사용자 푸쉬 알람 동의 여부 수정 성공")}) | ||
@PatchMapping("/push") | ||
public ResponseEntity<ApiResponse> updateUserPush(Principal principal, @RequestBody MemberPushUpdateRequestDTO request) { | ||
memberService.updateMemberPush(getMemberId(principal), request); | ||
memberService.updateHasAlarm(getMemberId(principal), request); | ||
return ResponseEntity.ok(success(SUCCESS_UPDATE_USER_PUSH.getMessage())); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.smeme.server.service; | ||
|
||
import com.smeme.server.model.Member; | ||
import com.smeme.server.model.badge.Badge; | ||
import com.smeme.server.model.badge.MemberBadge; | ||
import com.smeme.server.repository.badge.MemberBadgeRepository; | ||
import com.smeme.server.util.message.ErrorMessage; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MemberBadgeService { | ||
|
||
private final MemberBadgeRepository memberBadgeRepository; | ||
|
||
protected void save(Member member, Badge badge) { | ||
MemberBadge memberBadge = MemberBadge.builder() | ||
.member(member) | ||
.badge(badge) | ||
.build(); | ||
memberBadgeRepository.save(memberBadge); | ||
} | ||
|
||
public void deleteAllByMember(Member member) { | ||
memberBadgeRepository.deleteAll(member.getBadges()); | ||
} | ||
|
||
protected Badge getBadgeByMemberId(Long memberId) { | ||
return memberBadgeRepository.findFirstByMemberIdOrderByCreatedAtDesc(memberId).orElseThrow( | ||
() -> new EntityNotFoundException(ErrorMessage.EMPTY_BADGE.getMessage())).getBadge(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,14 @@ | |
|
||
import com.smeme.server.config.ValueConfig; | ||
import com.smeme.server.dto.badge.BadgeResponseDTO; | ||
import com.smeme.server.dto.goal.GoalResponseDTO; | ||
import com.smeme.server.dto.member.*; | ||
import com.smeme.server.dto.training.TrainingTimeResponseDTO; | ||
import com.smeme.server.model.Member; | ||
import com.smeme.server.model.badge.Badge; | ||
import com.smeme.server.model.badge.MemberBadge; | ||
import com.smeme.server.model.goal.Goal; | ||
import com.smeme.server.model.goal.GoalType; | ||
import com.smeme.server.model.training.DayType; | ||
import com.smeme.server.model.training.TrainingTime; | ||
import com.smeme.server.repository.badge.BadgeRepository; | ||
import com.smeme.server.repository.badge.MemberBadgeRepository; | ||
import com.smeme.server.repository.MemberRepository; | ||
import com.smeme.server.repository.goal.GoalRepository; | ||
import com.smeme.server.repository.trainingTime.TrainingTimeRepository; | ||
import com.smeme.server.util.message.ErrorMessage; | ||
import jakarta.persistence.EntityExistsException; | ||
import jakarta.persistence.EntityNotFoundException; | ||
|
@@ -39,59 +33,52 @@ | |
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final TrainingTimeRepository trainingTimeRepository; | ||
private final MemberBadgeRepository memberBadgeRepository; | ||
private final GoalRepository goalRepository; | ||
private final BadgeRepository badgeRepository; | ||
|
||
private final TrainingTimeService trainingTimeService; | ||
private final GoalService goalService; | ||
private final BadgeService badgeService; | ||
private final MemberBadgeService memberBadgeService; | ||
|
||
private final ValueConfig valueConfig; | ||
|
||
@Transactional | ||
public MemberUpdateResponseDTO updateMember(Long memberId, MemberUpdateRequestDTO request) { | ||
checkMemberDuplicate(request.username()); | ||
Member member = getMemberById(memberId); | ||
Member member = get(memberId); | ||
|
||
if (nonNull(request.termAccepted())) { | ||
member.updateTermAccepted(request.termAccepted()); | ||
} | ||
|
||
ArrayList<Badge> badges = new ArrayList<>(); | ||
if (isNull(member.getUsername())) { | ||
Badge welcomeBadge = getBadgeById(valueConfig.getWELCOME_BADGE_ID()); | ||
memberBadgeRepository.save(new MemberBadge(member, welcomeBadge)); | ||
Badge welcomeBadge = badgeService.get(valueConfig.getWELCOME_BADGE_ID()); | ||
memberBadgeService.save(member, welcomeBadge); | ||
badges.add(welcomeBadge); | ||
} | ||
member.updateUsername(request.username()); | ||
return MemberUpdateResponseDTO.of(badges); | ||
} | ||
|
||
public MemberGetResponseDTO getMember(Long memberId) { | ||
Member member = getMemberById(memberId); | ||
Goal goal = getGoal(member.getGoal()); | ||
List<TrainingTime> trainingTimeList = getTrainingTimeByMemberId(memberId); | ||
|
||
// TODO: beta test이후 리팩토링 | ||
if( getTrainingTimeByMemberId(memberId).isEmpty()) { | ||
TrainingTimeResponseDTO trainingTimeResponseDTO = TrainingTimeResponseDTO.builder() | ||
.day("") | ||
.hour(22) | ||
.minute(0) | ||
.build(); | ||
return MemberGetResponseDTO.of(goal, member, trainingTimeResponseDTO, BadgeResponseDTO.of(getBadge(memberId))); | ||
public MemberGetResponseDTO getMemberProfile(Long memberId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 Entity 이름은 메소드명에서 제외하기로 했으니 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오오 감사합니다! |
||
Member member = get(memberId); | ||
GoalResponseDTO goal = goalService.getByType(member.getGoal()); | ||
List<TrainingTime> trainingTimeList = trainingTimeService.getAllByMemberId(memberId); | ||
|
||
// 기본 시간 설정 | ||
if (trainingTimeService.getAllByMemberId(memberId).isEmpty()) { | ||
TrainingTimeResponseDTO trainingTimeResponseDTO = TrainingTimeResponseDTO.of("", 22, 0); | ||
return MemberGetResponseDTO.of(goal, member, trainingTimeResponseDTO, BadgeResponseDTO.of(memberBadgeService.getBadgeByMemberId(memberId))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별 거 아니긴 한데,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 것 같아요 수정하겠습니다! |
||
} | ||
|
||
TrainingTime trainingTime = getOneTrainingTime(getTrainingTimeByMemberId(memberId)); | ||
TrainingTimeResponseDTO trainingTimeResponseDTO = TrainingTimeResponseDTO.builder() | ||
.day(getDays(trainingTimeList)) | ||
.hour(trainingTime.getHour()) | ||
.minute(trainingTime.getMinute()) | ||
.build(); | ||
return MemberGetResponseDTO.of(goal, member,trainingTimeResponseDTO ,BadgeResponseDTO.of(getBadge(memberId))); | ||
TrainingTime trainingTime = getOneTrainingTime(trainingTimeList); | ||
TrainingTimeResponseDTO trainingTimeResponseDTO = TrainingTimeResponseDTO.of(getDays(trainingTimeList), trainingTime.getHour(), trainingTime.getMinute()); | ||
return MemberGetResponseDTO.of(goal, member, trainingTimeResponseDTO, BadgeResponseDTO.of(memberBadgeService.getBadgeByMemberId(memberId))); | ||
} | ||
|
||
|
||
@Transactional | ||
public void updateMemberPlan(Long memberId, MemberPlanUpdateRequestDTO request) { | ||
Member member = getMemberById(memberId); | ||
Member member = get(memberId); | ||
|
||
if (nonNull(request.target())) { | ||
member.updateGoal(request.target()); | ||
|
@@ -102,13 +89,13 @@ public void updateMemberPlan(Long memberId, MemberPlanUpdateRequestDTO request) | |
} | ||
|
||
if (nonNull(request.trainingTime()) && StringUtils.hasText(request.trainingTime().day())) { | ||
updateMemberTrainingTime(member, request); | ||
updateTrainingTime(member, request); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void updateMemberPush(Long memberId, MemberPushUpdateRequestDTO request) { | ||
Member member = getMemberById(memberId); | ||
public void updateHasAlarm(Long memberId, MemberPushUpdateRequestDTO request) { | ||
Member member = get(memberId); | ||
member.updateHasAlarm(request.hasAlarm()); | ||
} | ||
|
||
|
@@ -122,16 +109,16 @@ protected Member get(Long id) { | |
.orElseThrow(() -> new EntityNotFoundException(INVALID_MEMBER.getMessage())); | ||
} | ||
|
||
private void updateMemberTrainingTime(Member member, MemberPlanUpdateRequestDTO request) { | ||
trainingTimeRepository.deleteAll(member.getTrainingTimes()); | ||
private void updateTrainingTime(Member member, MemberPlanUpdateRequestDTO request) { | ||
trainingTimeService.deleteAll(member); | ||
for (String day : parseDay(request.trainingTime().day())) { | ||
TrainingTime trainingTime = TrainingTime.builder() | ||
.day(DayType.valueOf(day)) | ||
.hour(request.trainingTime().hour()) | ||
.minute(request.trainingTime().minute()) | ||
.member(member) | ||
.build(); | ||
trainingTimeRepository.save(trainingTime); | ||
trainingTimeService.save(trainingTime); | ||
} | ||
} | ||
|
||
|
@@ -140,16 +127,6 @@ private String[] parseDay(String day) { | |
} | ||
|
||
|
||
private Goal getGoal(GoalType goalType) { | ||
return goalRepository.findOneByType(goalType).orElseThrow( | ||
() -> new EntityNotFoundException(ErrorMessage.EMPTY_GOAL.getMessage())); | ||
} | ||
|
||
private Badge getBadge(Long memberId) { | ||
return memberBadgeRepository.findFirstByMemberIdOrderByCreatedAtDesc(memberId).orElseThrow( | ||
() -> new EntityNotFoundException(ErrorMessage.EMPTY_BADGE.getMessage())).getBadge(); | ||
} | ||
|
||
private TrainingTime getOneTrainingTime(List<TrainingTime> trainingTimeList) { | ||
return trainingTimeList.stream().findFirst().orElseThrow( | ||
() -> new EntityNotFoundException(ErrorMessage.EMPTY_TRAINING_TIME.getMessage())); | ||
|
@@ -162,23 +139,9 @@ private String getDays(List<TrainingTime> trainingTimeList) { | |
.collect(Collectors.joining(",")); | ||
} | ||
|
||
private List<TrainingTime> getTrainingTimeByMemberId(Long memberId) { | ||
return trainingTimeRepository.findAllByMemberId(memberId); | ||
} | ||
|
||
private Member getMemberById(Long id) { | ||
return memberRepository.findById(id).orElseThrow( | ||
() -> new EntityNotFoundException(ErrorMessage.EMPTY_MEMBER.getMessage())); | ||
} | ||
private void checkMemberDuplicate(String username) { | ||
if (memberRepository.existsByUsername(username)) { | ||
throw new EntityExistsException(ErrorMessage.DUPLICATE_USERNAME.getMessage()); | ||
} | ||
} | ||
|
||
private Badge getBadgeById(Long id) { | ||
return badgeRepository.findById(id).orElseThrow( | ||
() -> new EntityNotFoundException(ErrorMessage.EMPTY_BADGE.getMessage()) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳입니다~