Skip to content

Commit 79fa3b3

Browse files
authored
[Refactor] 멘토링 연관 도메인에 MentoringStorage, MemberStorage 적용 (#95)
* Refactor: 멘토링에 MemberStorage 적용 * Refactor: 멘토링에 MentorinStorage 적용 * Refactor: 멘토 슬롯에 MemberStorage 적용 * Refactor: 멘토 슬롯에 MentorinStorage 적용 * Refactor: MemberStorage에서 검증 로직 제거
1 parent 6bd168f commit 79fa3b3

File tree

8 files changed

+111
-113
lines changed

8 files changed

+111
-113
lines changed

back/src/main/java/com/back/domain/member/member/service/MemberStorage.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class MemberStorage {
1717
private final MentorRepository mentorRepository;
1818
private final MenteeRepository menteeRepository;
1919

20+
// ===== find 메서드 =====
21+
2022
public Mentor findMentorByMember(Member member) {
2123
return findMentorByMemberId(member.getId());
2224
}
@@ -30,4 +32,11 @@ public Mentee findMenteeByMember(Member member) {
3032
return menteeRepository.findByMemberId(member.getId())
3133
.orElseThrow(() -> new ServiceException(MemberErrorCode.NOT_FOUND_MENTEE));
3234
}
35+
36+
37+
// ==== exists 메서드 =====
38+
39+
public boolean existsMentorById(Long mentorId) {
40+
return mentorRepository.existsById(mentorId);
41+
}
3342
}

back/src/main/java/com/back/domain/mentoring/mentoring/controller/MentoringController.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.back.domain.mentoring.mentoring.controller;
22

3-
import com.back.domain.member.member.entity.Member;
3+
import com.back.domain.member.member.service.MemberStorage;
4+
import com.back.domain.member.mentor.entity.Mentor;
45
import com.back.domain.mentoring.mentoring.dto.MentoringWithTagsDto;
56
import com.back.domain.mentoring.mentoring.dto.request.MentoringRequest;
67
import com.back.domain.mentoring.mentoring.dto.response.MentoringPagingResponse;
@@ -21,8 +22,9 @@
2122
@RequiredArgsConstructor
2223
@Tag(name = "MentoringController", description = "멘토링 API")
2324
public class MentoringController {
24-
private final MentoringService mentoringService;
2525
private final Rq rq;
26+
private final MentoringService mentoringService;
27+
private final MemberStorage memberStorage;
2628

2729
@GetMapping
2830
@Operation(summary = "멘토링 목록 조회", description = "멘토링 목록을 조회합니다")
@@ -61,8 +63,8 @@ public RsData<MentoringResponse> getMentoring(
6163
public RsData<MentoringResponse> createMentoring(
6264
@RequestBody @Valid MentoringRequest reqDto
6365
) {
64-
Member member = rq.getActor();
65-
MentoringResponse resDto = mentoringService.createMentoring(reqDto, member);
66+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
67+
MentoringResponse resDto = mentoringService.createMentoring(reqDto, mentor);
6668

6769
return new RsData<>(
6870
"201",
@@ -77,8 +79,8 @@ public RsData<MentoringResponse> updateMentoring(
7779
@PathVariable Long mentoringId,
7880
@RequestBody @Valid MentoringRequest reqDto
7981
) {
80-
Member member = rq.getActor();
81-
MentoringResponse resDto = mentoringService.updateMentoring(mentoringId, reqDto, member);
82+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
83+
MentoringResponse resDto = mentoringService.updateMentoring(mentoringId, reqDto, mentor);
8284

8385
return new RsData<>(
8486
"200",
@@ -92,8 +94,8 @@ public RsData<MentoringResponse> updateMentoring(
9294
public RsData<Void> deleteMentoring(
9395
@PathVariable Long mentoringId
9496
) {
95-
Member member = rq.getActor();
96-
mentoringService.deleteMentoring(mentoringId, member);
97+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
98+
mentoringService.deleteMentoring(mentoringId, mentor);
9799

98100
return new RsData<>(
99101
"200",

back/src/main/java/com/back/domain/mentoring/mentoring/service/MentoringService.java

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.back.domain.mentoring.mentoring.service;
22

3-
import com.back.domain.member.member.entity.Member;
43
import com.back.domain.member.mentor.dto.MentorDetailDto;
54
import com.back.domain.member.mentor.entity.Mentor;
6-
import com.back.domain.member.mentor.repository.MentorRepository;
75
import com.back.domain.mentoring.mentoring.dto.MentoringDetailDto;
86
import com.back.domain.mentoring.mentoring.dto.MentoringWithTagsDto;
97
import com.back.domain.mentoring.mentoring.dto.request.MentoringRequest;
108
import com.back.domain.mentoring.mentoring.dto.response.MentoringResponse;
119
import com.back.domain.mentoring.mentoring.entity.Mentoring;
1210
import com.back.domain.mentoring.mentoring.error.MentoringErrorCode;
1311
import com.back.domain.mentoring.mentoring.repository.MentoringRepository;
14-
import com.back.domain.mentoring.reservation.repository.ReservationRepository;
15-
import com.back.domain.mentoring.slot.repository.MentorSlotRepository;
1612
import com.back.global.exception.ServiceException;
1713
import lombok.RequiredArgsConstructor;
1814
import org.springframework.data.domain.Page;
@@ -25,9 +21,7 @@
2521
@RequiredArgsConstructor
2622
public class MentoringService {
2723
private final MentoringRepository mentoringRepository;
28-
private final MentorRepository mentorRepository;
29-
private final ReservationRepository reservationRepository;
30-
private final MentorSlotRepository mentorSlotRepository;
24+
private final MentoringStorage mentoringStorage;
3125

3226
@Transactional(readOnly = true)
3327
public Page<MentoringWithTagsDto> getMentorings(String keyword, int page, int size) {
@@ -39,19 +33,16 @@ public Page<MentoringWithTagsDto> getMentorings(String keyword, int page, int si
3933

4034
@Transactional(readOnly = true)
4135
public MentoringResponse getMentoring(Long mentoringId) {
42-
Mentoring mentoring = findMentoring(mentoringId);
43-
Mentor mentor = mentoring.getMentor();
36+
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);
4437

4538
return new MentoringResponse(
4639
MentoringDetailDto.from(mentoring),
47-
MentorDetailDto.from(mentor)
40+
MentorDetailDto.from(mentoring.getMentor())
4841
);
4942
}
5043

5144
@Transactional
52-
public MentoringResponse createMentoring(MentoringRequest reqDto, Member member) {
53-
Mentor mentor = findMentor(member);
54-
45+
public MentoringResponse createMentoring(MentoringRequest reqDto, Mentor mentor) {
5546
// 멘토당 멘토링 1개 제한 체크 (추후 1:N 변경 시 제거 필요)
5647
if (mentoringRepository.existsByMentorId(mentor.getId())) {
5748
throw new ServiceException(MentoringErrorCode.ALREADY_EXISTS_MENTORING);
@@ -74,9 +65,8 @@ public MentoringResponse createMentoring(MentoringRequest reqDto, Member member)
7465
}
7566

7667
@Transactional
77-
public MentoringResponse updateMentoring(Long mentoringId, MentoringRequest reqDto, Member member) {
78-
Mentor mentor = findMentor(member);
79-
Mentoring mentoring = findMentoring(mentoringId);
68+
public MentoringResponse updateMentoring(Long mentoringId, MentoringRequest reqDto, Mentor mentor) {
69+
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);
8070

8171
validateOwner(mentoring, mentor);
8272

@@ -89,39 +79,24 @@ public MentoringResponse updateMentoring(Long mentoringId, MentoringRequest reqD
8979
}
9080

9181
@Transactional
92-
public void deleteMentoring(Long mentoringId, Member member) {
93-
Mentor mentor = findMentor(member);
94-
Mentoring mentoring = findMentoring(mentoringId);
82+
public void deleteMentoring(Long mentoringId, Mentor mentor) {
83+
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);
9584

9685
validateOwner(mentoring, mentor);
9786

9887
// 예약 이력이 있을 시 삭제 불가
99-
if (reservationRepository.existsByMentoringId(mentoring.getId())) {
88+
if (mentoringStorage.hasReservationsForMentoring(mentoring.getId())) {
10089
throw new ServiceException(MentoringErrorCode.CANNOT_DELETE_MENTORING);
10190
}
10291

10392
// 멘토 슬롯 있을 시 일괄 삭제 (추후 1:N 변경 시 제거 필요)
104-
if (mentorSlotRepository.existsByMentorId(mentor.getId())) {
105-
mentorSlotRepository.deleteAllByMentorId(mentor.getId());
93+
if (mentoringStorage.hasMentorSlotsForMentor(mentor.getId())) {
94+
mentoringStorage.deleteMentorSlotsData(mentor.getId());
10695
}
107-
// 멘토 삭제
10896
mentoringRepository.delete(mentoring);
10997
}
11098

11199

112-
// ===== 헬퍼 메서드 =====
113-
114-
private Mentor findMentor(Member member) {
115-
return mentorRepository.findByMemberId(member.getId())
116-
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTOR));
117-
}
118-
119-
private Mentoring findMentoring(Long mentoringId) {
120-
return mentoringRepository.findById(mentoringId)
121-
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING));
122-
}
123-
124-
125100
// ===== 유효성 검사 =====
126101

127102
private void validateOwner(Mentoring mentoring, Mentor mentor) {

back/src/main/java/com/back/domain/mentoring/mentoring/service/MentoringStorage.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.domain.mentoring.mentoring.service;
22

3+
import com.back.domain.member.mentor.entity.Mentor;
34
import com.back.domain.mentoring.mentoring.entity.Mentoring;
45
import com.back.domain.mentoring.mentoring.error.MentoringErrorCode;
56
import com.back.domain.mentoring.mentoring.repository.MentoringRepository;
@@ -11,6 +12,13 @@
1112
import lombok.RequiredArgsConstructor;
1213
import org.springframework.stereotype.Component;
1314

15+
import java.util.List;
16+
17+
/**
18+
* Mentoring 관련 도메인들의 데이터 접근을 캡슐화
19+
* - 도메인 간 직접 Repository 참조를 방지
20+
* - 연관 엔티티 조회/검증/삭제 로직 제공
21+
*/
1422
@Component
1523
@RequiredArgsConstructor
1624
public class MentoringStorage {
@@ -19,13 +27,50 @@ public class MentoringStorage {
1927
private final MentorSlotRepository mentorSlotRepository;
2028
private final ReservationRepository reservationRepository;
2129

30+
// ===== find 메서드 =====
31+
2232
public Mentoring findMentoring(Long mentoringId) {
2333
return mentoringRepository.findById(mentoringId)
2434
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING));
2535
}
2636

37+
// TODO : 멘토:멘토링 1:N으로 변경 시 삭제 예정
38+
public Mentoring findMentoringByMentor(Mentor mentor) {
39+
return findMentoringsByMentorId(mentor.getId()).getFirst();
40+
}
41+
42+
public List<Mentoring> findMentoringsByMentorId(Long mentorId) {
43+
List<Mentoring> mentorings = mentoringRepository.findByMentorId(mentorId);
44+
if (mentorings.isEmpty()) {
45+
throw new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING);
46+
}
47+
return mentorings;
48+
}
49+
2750
public MentorSlot findMentorSlot(Long slotId) {
2851
return mentorSlotRepository.findById(slotId)
2952
.orElseThrow(() -> new ServiceException(MentorSlotErrorCode.NOT_FOUND_MENTOR_SLOT));
3053
}
54+
55+
56+
// ==== exists 메서드 =====
57+
58+
public boolean hasReservationsForMentoring(Long mentoringId) {
59+
return reservationRepository.existsByMentoringId(mentoringId);
60+
}
61+
62+
public boolean hasMentorSlotsForMentor(Long mentorId) {
63+
return mentorSlotRepository.existsByMentorId(mentorId);
64+
}
65+
66+
public boolean hasReservationForMentorSlot(Long slotId) {
67+
return reservationRepository.existsByMentorSlotId(slotId);
68+
}
69+
70+
71+
// ===== 데이터 조작 메서드 =====
72+
73+
public void deleteMentorSlotsData(Long mentorId) {
74+
mentorSlotRepository.deleteAllByMentorId(mentorId);
75+
}
3176
}

back/src/main/java/com/back/domain/mentoring/reservation/controller/ReservationController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
@Tag(name = "ReservationController", description = "예약 API")
2424
public class ReservationController {
2525

26+
private final Rq rq;
2627
private final ReservationService reservationService;
2728
private final MemberStorage memberStorage;
28-
private final Rq rq;
2929

3030
@PostMapping
3131
@PreAuthorize("hasRole('MENTEE')")

back/src/main/java/com/back/domain/mentoring/slot/controller/MentorSlotController.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.back.domain.mentoring.slot.controller;
22

3-
import com.back.domain.member.member.entity.Member;
3+
import com.back.domain.member.member.service.MemberStorage;
4+
import com.back.domain.member.mentor.entity.Mentor;
5+
import com.back.domain.mentoring.mentoring.error.MentoringErrorCode;
46
import com.back.domain.mentoring.slot.dto.request.MentorSlotRepetitionRequest;
57
import com.back.domain.mentoring.slot.dto.request.MentorSlotRequest;
68
import com.back.domain.mentoring.slot.dto.response.MentorSlotDto;
79
import com.back.domain.mentoring.slot.dto.response.MentorSlotResponse;
810
import com.back.domain.mentoring.slot.service.MentorSlotService;
11+
import com.back.global.exception.ServiceException;
912
import com.back.global.rq.Rq;
1013
import com.back.global.rsData.RsData;
1114
import io.swagger.v3.oas.annotations.Operation;
@@ -26,8 +29,9 @@
2629
@Tag(name = "MentorSlotController", description = "멘토 슬롯(멘토의 예약 가능 일정) API")
2730
public class MentorSlotController {
2831

29-
private final MentorSlotService mentorSlotService;
3032
private final Rq rq;
33+
private final MentorSlotService mentorSlotService;
34+
private final MemberStorage memberStorage;
3135

3236
@GetMapping
3337
@PreAuthorize("hasRole('MENTOR')")
@@ -36,12 +40,12 @@ public RsData<List<MentorSlotDto>> getMyMentorSlots(
3640
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
3741
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
3842
) {
39-
Member member = rq.getActor();
43+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
4044

4145
LocalDateTime startDateTime = startDate.atStartOfDay();
4246
LocalDateTime endDateTime = endDate.atStartOfDay();
4347

44-
List<MentorSlotDto> resDtoList = mentorSlotService.getMyMentorSlots(member, startDateTime, endDateTime);
48+
List<MentorSlotDto> resDtoList = mentorSlotService.getMyMentorSlots(mentor, startDateTime, endDateTime);
4549

4650
return new RsData<>(
4751
"200",
@@ -57,6 +61,10 @@ public RsData<List<MentorSlotDto>> getAvailableMentorSlots(
5761
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
5862
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
5963
) {
64+
if (!memberStorage.existsMentorById(mentorId)) {
65+
throw new ServiceException(MentoringErrorCode.NOT_FOUND_MENTOR);
66+
}
67+
6068
LocalDateTime startDateTime = startDate.atStartOfDay();
6169
LocalDateTime endDateTime = endDate.atStartOfDay();
6270

@@ -89,8 +97,8 @@ public RsData<MentorSlotResponse> getMentorSlot(
8997
public RsData<MentorSlotResponse> createMentorSlot(
9098
@RequestBody @Valid MentorSlotRequest reqDto
9199
) {
92-
Member member = rq.getActor();
93-
MentorSlotResponse resDto = mentorSlotService.createMentorSlot(reqDto, member);
100+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
101+
MentorSlotResponse resDto = mentorSlotService.createMentorSlot(reqDto, mentor);
94102

95103
return new RsData<>(
96104
"201",
@@ -105,8 +113,8 @@ public RsData<MentorSlotResponse> createMentorSlot(
105113
public RsData<Void> createMentorSlotRepetition(
106114
@RequestBody @Valid MentorSlotRepetitionRequest reqDto
107115
) {
108-
Member member = rq.getActor();
109-
mentorSlotService.createMentorSlotRepetition(reqDto, member);
116+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
117+
mentorSlotService.createMentorSlotRepetition(reqDto, mentor);
110118

111119
return new RsData<>(
112120
"201",
@@ -120,8 +128,8 @@ public RsData<MentorSlotResponse> updateMentorSlot(
120128
@PathVariable Long slotId,
121129
@RequestBody @Valid MentorSlotRequest reqDto
122130
) {
123-
Member member = rq.getActor();
124-
MentorSlotResponse resDto = mentorSlotService.updateMentorSlot(slotId, reqDto, member);
131+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
132+
MentorSlotResponse resDto = mentorSlotService.updateMentorSlot(slotId, reqDto, mentor);
125133

126134
return new RsData<>(
127135
"200",
@@ -135,8 +143,8 @@ public RsData<MentorSlotResponse> updateMentorSlot(
135143
public RsData<Void> deleteMentorSlot(
136144
@PathVariable Long slotId
137145
) {
138-
Member member = rq.getActor();
139-
mentorSlotService.deleteMentorSlot(slotId, member);
146+
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
147+
mentorSlotService.deleteMentorSlot(slotId, mentor);
140148

141149
return new RsData<>(
142150
"200",

0 commit comments

Comments
 (0)