Skip to content
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

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -23,6 +23,7 @@
@RequiredArgsConstructor
@RequestMapping("/api/beta")
@Tag(name = "BetaAuth", description = "베타 테스트용 인증 API")
@Deprecated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳입니다~

public class BetaAuthController {

private final BetaAuthService betaAuthService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entity명을 제외하기로 했으니 getProfile이라는 메소드명은 어떨까요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 것 같아요 수정하겠습니다!

return ResponseEntity.ok(success(SUCCESS_GET_USER.getMessage(), response));
}

Expand Down Expand Up @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.smeme.server.dto.auth;


import com.smeme.server.dto.auth.token.TokenVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

Expand All @@ -17,4 +18,8 @@ public record SignInResponseDTO(
boolean hasPlan
) {

public static SignInResponseDTO of(TokenVO tokenVO, boolean isRegistered, boolean hasPlan) {
return new SignInResponseDTO(tokenVO.accessToken(), tokenVO.refreshToken(), isRegistered, hasPlan);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.smeme.server.dto.member;

import com.smeme.server.dto.badge.BadgeResponseDTO;
import com.smeme.server.dto.goal.GoalResponseDTO;
import com.smeme.server.dto.training.TrainingTimeResponseDTO;
import com.smeme.server.model.Member;
import com.smeme.server.model.goal.Goal;
Expand All @@ -24,12 +25,12 @@ public record MemberGetResponseDTO(
@Schema(description = "뱃지")
BadgeResponseDTO badge) {

public static MemberGetResponseDTO of(Goal goal, Member member, TrainingTimeResponseDTO trainingTime, BadgeResponseDTO badge) {
public static MemberGetResponseDTO of(GoalResponseDTO goal, Member member, TrainingTimeResponseDTO trainingTime, BadgeResponseDTO badge) {
return new MemberGetResponseDTO(
member.getUsername(),
member.getGoal().getDescription(),
goal.getWay(),
goal.getDetail(),
goal.way(),
goal.detail(),
member.getTargetLang().toString(),
member.isHasPushAlarm(),
trainingTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import lombok.Builder;

@Builder
public record TrainingTimeResponseDTO(String day, int hour, int minute) {

public static TrainingTimeResponseDTO of(String day, int hour, int minute) {
return new TrainingTimeResponseDTO(day, hour, minute);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/smeme/server/model/badge/MemberBadge.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -34,6 +35,8 @@ public class MemberBadge extends BaseTimeEntity {
@JoinColumn(name = "badge_id")
private Badge badge;


@Builder
public MemberBadge(Member member, Badge badge) {
setMember(member);
this.badge = badge;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.smeme.server.repository.diary;

import com.smeme.server.model.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import com.smeme.server.model.Diary;
Expand All @@ -8,7 +9,7 @@

public interface DiaryRepository extends JpaRepository<Diary, Long>, DiaryCustomRepository {

void deleteAllByMemberId(Long id);
void deleteAllByMember(Member member);

List<Diary> findAllByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.smeme.server.repository.trainingTime;

import com.smeme.server.model.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import com.smeme.server.model.training.TrainingTime;
Expand All @@ -10,6 +11,6 @@ public interface TrainingTimeRepository extends JpaRepository<TrainingTime, Long

List<TrainingTime> findAllByMemberId(Long memberId);

void deleteAllByMemberId(Long id);
void deleteAllByMember(Member member);

}
6 changes: 6 additions & 0 deletions src/main/java/com/smeme/server/service/CorrectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public void update(Long id, CorrectionRequestDTO request) {
correction.updateContent(request.content());
}

@Transactional
public void deleteAllByDiaryId(Long diaryId) {
correctionRepository.deleteAllByDiaryId(diaryId);
}


protected void deleteCorrections(List<Correction> corrections) {
correctionRepository.deleteAll(corrections);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/smeme/server/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public void deleteByExpiredDate() {
diaryRepository.findByExpiredDate().forEach(this::delete);
}

@Transactional
public void deleteAllByMember(Member member) {
diaryRepository.deleteAllByMember(member);
}

public List<Diary> getAllByMemberId(Long memberId) {
return diaryRepository.findAllByMemberId(memberId);
}

protected Diary get(Long id) {
Diary diary = diaryRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(INVALID_DIARY.getMessage()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/smeme/server/service/GoalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public GoalsResponseDTO getAll() {
public GoalResponseDTO getByType(GoalType goalType) {
Goal goal = goalRepository.findOneByType(goalType)
.orElseThrow(() -> new EntityNotFoundException(EMPTY_GOAL.getMessage()));
return new GoalResponseDTO(goalType.getDescription(), goal.getWay(), goal.getDetail());
return new GoalResponseDTO(goal.getType().getDescription(), goal.getWay(), goal.getDetail());
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/smeme/server/service/MemberBadgeService.java
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();
}
}
95 changes: 29 additions & 66 deletions src/main/java/com/smeme/server/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 Entity 이름은 메소드명에서 제외하기로 했으니 getProfile로 명명해도 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

별 거 아니긴 한데, BadgeResponseDTO.of(memberBadgeService.getBadgeByMemberId(memberId)) 인자를 다른 한 줄로 따로 빼면 가독성이 더 좋아질 것 같아요! (예시 아래)

badgeResponseDTO = BadgeResponseDTO.of(memberBadgeService.getBadgeByMemberId(memberId));
return return MemberGetResponseDTO.of(goal, member, trainingTimeResponseDTO, badgeResponseDTO);

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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());
Expand All @@ -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());
}

Expand All @@ -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);
}
}

Expand All @@ -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()));
Expand All @@ -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())
);
}
}
Loading
Loading