Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -17,6 +17,8 @@ public class MemberStorage {
private final MentorRepository mentorRepository;
private final MenteeRepository menteeRepository;

// ===== find 메서드 =====

public Mentor findMentorByMember(Member member) {
return findMentorByMemberId(member.getId());
}
Expand All @@ -30,4 +32,11 @@ public Mentee findMenteeByMember(Member member) {
return menteeRepository.findByMemberId(member.getId())
.orElseThrow(() -> new ServiceException(MemberErrorCode.NOT_FOUND_MENTEE));
}


// ==== exists 메서드 =====

public boolean existsMentorById(Long mentorId) {
return mentorRepository.existsById(mentorId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.back.domain.mentoring.mentoring.controller;

import com.back.domain.member.member.entity.Member;
import com.back.domain.member.member.service.MemberStorage;
import com.back.domain.member.mentor.entity.Mentor;
import com.back.domain.mentoring.mentoring.dto.MentoringWithTagsDto;
import com.back.domain.mentoring.mentoring.dto.request.MentoringRequest;
import com.back.domain.mentoring.mentoring.dto.response.MentoringPagingResponse;
Expand All @@ -21,8 +22,9 @@
@RequiredArgsConstructor
@Tag(name = "MentoringController", description = "멘토링 API")
public class MentoringController {
private final MentoringService mentoringService;
private final Rq rq;
private final MentoringService mentoringService;
private final MemberStorage memberStorage;

@GetMapping
@Operation(summary = "멘토링 목록 조회", description = "멘토링 목록을 조회합니다")
Expand Down Expand Up @@ -61,8 +63,8 @@ public RsData<MentoringResponse> getMentoring(
public RsData<MentoringResponse> createMentoring(
@RequestBody @Valid MentoringRequest reqDto
) {
Member member = rq.getActor();
MentoringResponse resDto = mentoringService.createMentoring(reqDto, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
MentoringResponse resDto = mentoringService.createMentoring(reqDto, mentor);

return new RsData<>(
"201",
Expand All @@ -77,8 +79,8 @@ public RsData<MentoringResponse> updateMentoring(
@PathVariable Long mentoringId,
@RequestBody @Valid MentoringRequest reqDto
) {
Member member = rq.getActor();
MentoringResponse resDto = mentoringService.updateMentoring(mentoringId, reqDto, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
MentoringResponse resDto = mentoringService.updateMentoring(mentoringId, reqDto, mentor);

return new RsData<>(
"200",
Expand All @@ -92,8 +94,8 @@ public RsData<MentoringResponse> updateMentoring(
public RsData<Void> deleteMentoring(
@PathVariable Long mentoringId
) {
Member member = rq.getActor();
mentoringService.deleteMentoring(mentoringId, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
mentoringService.deleteMentoring(mentoringId, mentor);

return new RsData<>(
"200",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.back.domain.mentoring.mentoring.service;

import com.back.domain.member.member.entity.Member;
import com.back.domain.member.mentor.dto.MentorDetailDto;
import com.back.domain.member.mentor.entity.Mentor;
import com.back.domain.member.mentor.repository.MentorRepository;
import com.back.domain.mentoring.mentoring.dto.MentoringDetailDto;
import com.back.domain.mentoring.mentoring.dto.MentoringWithTagsDto;
import com.back.domain.mentoring.mentoring.dto.request.MentoringRequest;
import com.back.domain.mentoring.mentoring.dto.response.MentoringResponse;
import com.back.domain.mentoring.mentoring.entity.Mentoring;
import com.back.domain.mentoring.mentoring.error.MentoringErrorCode;
import com.back.domain.mentoring.mentoring.repository.MentoringRepository;
import com.back.domain.mentoring.reservation.repository.ReservationRepository;
import com.back.domain.mentoring.slot.repository.MentorSlotRepository;
import com.back.global.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -25,9 +21,7 @@
@RequiredArgsConstructor
public class MentoringService {
private final MentoringRepository mentoringRepository;
private final MentorRepository mentorRepository;
private final ReservationRepository reservationRepository;
private final MentorSlotRepository mentorSlotRepository;
private final MentoringStorage mentoringStorage;

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

@Transactional(readOnly = true)
public MentoringResponse getMentoring(Long mentoringId) {
Mentoring mentoring = findMentoring(mentoringId);
Mentor mentor = mentoring.getMentor();
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);

return new MentoringResponse(
MentoringDetailDto.from(mentoring),
MentorDetailDto.from(mentor)
MentorDetailDto.from(mentoring.getMentor())
);
}

@Transactional
public MentoringResponse createMentoring(MentoringRequest reqDto, Member member) {
Mentor mentor = findMentor(member);

public MentoringResponse createMentoring(MentoringRequest reqDto, Mentor mentor) {
// 멘토당 멘토링 1개 제한 체크 (추후 1:N 변경 시 제거 필요)
if (mentoringRepository.existsByMentorId(mentor.getId())) {
throw new ServiceException(MentoringErrorCode.ALREADY_EXISTS_MENTORING);
Expand All @@ -74,9 +65,8 @@ public MentoringResponse createMentoring(MentoringRequest reqDto, Member member)
}

@Transactional
public MentoringResponse updateMentoring(Long mentoringId, MentoringRequest reqDto, Member member) {
Mentor mentor = findMentor(member);
Mentoring mentoring = findMentoring(mentoringId);
public MentoringResponse updateMentoring(Long mentoringId, MentoringRequest reqDto, Mentor mentor) {
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);

validateOwner(mentoring, mentor);

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

@Transactional
public void deleteMentoring(Long mentoringId, Member member) {
Mentor mentor = findMentor(member);
Mentoring mentoring = findMentoring(mentoringId);
public void deleteMentoring(Long mentoringId, Mentor mentor) {
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);

validateOwner(mentoring, mentor);

// 예약 이력이 있을 시 삭제 불가
if (reservationRepository.existsByMentoringId(mentoring.getId())) {
if (mentoringStorage.hasReservationsForMentoring(mentoring.getId())) {
throw new ServiceException(MentoringErrorCode.CANNOT_DELETE_MENTORING);
}

// 멘토 슬롯 있을 시 일괄 삭제 (추후 1:N 변경 시 제거 필요)
if (mentorSlotRepository.existsByMentorId(mentor.getId())) {
mentorSlotRepository.deleteAllByMentorId(mentor.getId());
if (mentoringStorage.hasMentorSlotsForMentor(mentor.getId())) {
mentoringStorage.deleteMentorSlotsData(mentor.getId());
}
// 멘토 삭제
mentoringRepository.delete(mentoring);
}


// ===== 헬퍼 메서드 =====

private Mentor findMentor(Member member) {
return mentorRepository.findByMemberId(member.getId())
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTOR));
}

private Mentoring findMentoring(Long mentoringId) {
return mentoringRepository.findById(mentoringId)
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING));
}


// ===== 유효성 검사 =====

private void validateOwner(Mentoring mentoring, Mentor mentor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.back.domain.mentoring.mentoring.service;

import com.back.domain.member.mentor.entity.Mentor;
import com.back.domain.mentoring.mentoring.entity.Mentoring;
import com.back.domain.mentoring.mentoring.error.MentoringErrorCode;
import com.back.domain.mentoring.mentoring.repository.MentoringRepository;
Expand All @@ -11,6 +12,13 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* Mentoring 관련 도메인들의 데이터 접근을 캡슐화
* - 도메인 간 직접 Repository 참조를 방지
* - 연관 엔티티 조회/검증/삭제 로직 제공
*/
@Component
@RequiredArgsConstructor
public class MentoringStorage {
Expand All @@ -19,13 +27,50 @@ public class MentoringStorage {
private final MentorSlotRepository mentorSlotRepository;
private final ReservationRepository reservationRepository;

// ===== find 메서드 =====

public Mentoring findMentoring(Long mentoringId) {
return mentoringRepository.findById(mentoringId)
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING));
}

// TODO : 멘토:멘토링 1:N으로 변경 시 삭제 예정
public Mentoring findMentoringByMentor(Mentor mentor) {
return findMentoringsByMentorId(mentor.getId()).getFirst();
}

public List<Mentoring> findMentoringsByMentorId(Long mentorId) {
List<Mentoring> mentorings = mentoringRepository.findByMentorId(mentorId);
if (mentorings.isEmpty()) {
throw new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING);
}
return mentorings;
}

public MentorSlot findMentorSlot(Long slotId) {
return mentorSlotRepository.findById(slotId)
.orElseThrow(() -> new ServiceException(MentorSlotErrorCode.NOT_FOUND_MENTOR_SLOT));
}


// ==== exists 메서드 =====

public boolean hasReservationsForMentoring(Long mentoringId) {
return reservationRepository.existsByMentoringId(mentoringId);
}

public boolean hasMentorSlotsForMentor(Long mentorId) {
return mentorSlotRepository.existsByMentorId(mentorId);
}

public boolean hasReservationForMentorSlot(Long slotId) {
return reservationRepository.existsByMentorSlotId(slotId);
}


// ===== 데이터 조작 메서드 =====

public void deleteMentorSlotsData(Long mentorId) {
mentorSlotRepository.deleteAllByMentorId(mentorId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
@Tag(name = "ReservationController", description = "예약 API")
public class ReservationController {

private final Rq rq;
private final ReservationService reservationService;
private final MemberStorage memberStorage;
private final Rq rq;

@PostMapping
@PreAuthorize("hasRole('MENTEE')")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.back.domain.mentoring.slot.controller;

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

private final MentorSlotService mentorSlotService;
private final Rq rq;
private final MentorSlotService mentorSlotService;
private final MemberStorage memberStorage;

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

LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime = endDate.atStartOfDay();

List<MentorSlotDto> resDtoList = mentorSlotService.getMyMentorSlots(member, startDateTime, endDateTime);
List<MentorSlotDto> resDtoList = mentorSlotService.getMyMentorSlots(mentor, startDateTime, endDateTime);

return new RsData<>(
"200",
Expand All @@ -57,6 +61,10 @@ public RsData<List<MentorSlotDto>> getAvailableMentorSlots(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
) {
if (!memberStorage.existsMentorById(mentorId)) {
throw new ServiceException(MentoringErrorCode.NOT_FOUND_MENTOR);
}

LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime = endDate.atStartOfDay();

Expand Down Expand Up @@ -89,8 +97,8 @@ public RsData<MentorSlotResponse> getMentorSlot(
public RsData<MentorSlotResponse> createMentorSlot(
@RequestBody @Valid MentorSlotRequest reqDto
) {
Member member = rq.getActor();
MentorSlotResponse resDto = mentorSlotService.createMentorSlot(reqDto, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
MentorSlotResponse resDto = mentorSlotService.createMentorSlot(reqDto, mentor);

return new RsData<>(
"201",
Expand All @@ -105,8 +113,8 @@ public RsData<MentorSlotResponse> createMentorSlot(
public RsData<Void> createMentorSlotRepetition(
@RequestBody @Valid MentorSlotRepetitionRequest reqDto
) {
Member member = rq.getActor();
mentorSlotService.createMentorSlotRepetition(reqDto, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
mentorSlotService.createMentorSlotRepetition(reqDto, mentor);

return new RsData<>(
"201",
Expand All @@ -120,8 +128,8 @@ public RsData<MentorSlotResponse> updateMentorSlot(
@PathVariable Long slotId,
@RequestBody @Valid MentorSlotRequest reqDto
) {
Member member = rq.getActor();
MentorSlotResponse resDto = mentorSlotService.updateMentorSlot(slotId, reqDto, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
MentorSlotResponse resDto = mentorSlotService.updateMentorSlot(slotId, reqDto, mentor);

return new RsData<>(
"200",
Expand All @@ -135,8 +143,8 @@ public RsData<MentorSlotResponse> updateMentorSlot(
public RsData<Void> deleteMentorSlot(
@PathVariable Long slotId
) {
Member member = rq.getActor();
mentorSlotService.deleteMentorSlot(slotId, member);
Mentor mentor = memberStorage.findMentorByMember(rq.getActor());
mentorSlotService.deleteMentorSlot(slotId, mentor);

return new RsData<>(
"200",
Expand Down
Loading