Skip to content

Commit

Permalink
Merge pull request #167 from Team-Smeme/unan_#165
Browse files Browse the repository at this point in the history
[REFACTOR] 윤한 담당 API 리팩토링
  • Loading branch information
unanchoi authored Oct 31, 2023
2 parents a0bdfab + 15a412a commit dd2412e
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 126 deletions.
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
public class BetaAuthController {

private final BetaAuthService betaAuthService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MemberController {
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "사용자 정보 수정 성공")})
@PatchMapping()
public ResponseEntity<ApiResponse> updateUserProfile(Principal principal, @RequestBody MemberUpdateRequestDTO request) {
public ResponseEntity<ApiResponse> updateProfile(Principal principal, @RequestBody MemberUpdateRequestDTO request) {
MemberUpdateResponseDTO response = memberService.updateMember(getMemberId(principal), request);
return ResponseEntity.ok(success(SUCCESS_UPDATE_USERNAME.getMessage(), response));
}
Expand All @@ -44,8 +44,8 @@ public ResponseEntity<ApiResponse> updateUserProfile(Principal principal, @Reque
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "사용자 정보 수정 성공"
, content = @Content(schema = @Schema(implementation = MemberGetResponseDTO.class)))})
@GetMapping("/me")
public ResponseEntity<ApiResponse> getUserProfile(Principal principal) {
MemberGetResponseDTO response = memberService.getMember(getMemberId(principal));
public ResponseEntity<ApiResponse> getProfile(Principal principal) {
MemberGetResponseDTO response = memberService.getProfile(getMemberId(principal));
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 @@ -8,8 +9,8 @@

public interface TrainingTimeRepository extends JpaRepository<TrainingTime, Long>, TrainingTimeCustomRepository {

List<TrainingTime> findAllByMemberId(Long memberId);
List<TrainingTime> findAllByMember(Member member);

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

0 comments on commit dd2412e

Please sign in to comment.