Skip to content

Commit 4b440ea

Browse files
authored
Refactor: 멘토-멘토링 1:N 으로 변경 (#157)
* Refactor: 멘토링 여러개 등록 가능하게 변경 * Refactor: 슬롯 상세 조회 시 멘토링 목록 함께 반환 * Refactor: 멘토링 평점 필드 추가 * Refactor: 평균 리뷰 평점 소수점 1자리까지 * Refactor: 요청값 swagger 샘플데이터 세팅
1 parent 01da591 commit 4b440ea

File tree

21 files changed

+111
-158
lines changed

21 files changed

+111
-158
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ReviewController {
2525
private final ReviewService reviewService;
2626

2727
@GetMapping("/mentorings/{mentoringId}/reviews")
28-
@Operation(summary = "멘토링 리뷰 조회", description = "멘토링 리뷰 목록을 조회합니다.")
28+
@Operation(summary = "멘토링 리뷰 목록 조회", description = "멘토링 리뷰 목록을 조회합니다.")
2929
public RsData<ReviewPagingResponse> getReviews(
3030
@PathVariable Long mentoringId,
3131
@RequestParam(defaultValue = "0") int page,

back/src/main/java/com/back/domain/mentoring/mentoring/dto/MentoringDetailDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public record MentoringDetailDto(
1717
String bio,
1818
@Schema(description = "멘토링 썸네일")
1919
String thumb,
20+
@Schema(description = "멘토링 평점")
21+
Double rating,
2022
@Schema(description = "생성일")
2123
LocalDateTime createDate,
2224
@Schema(description = "수정일")
@@ -29,6 +31,7 @@ public static MentoringDetailDto from(Mentoring mentoring) {
2931
mentoring.getTagNames(),
3032
mentoring.getBio(),
3133
mentoring.getThumb(),
34+
mentoring.getRating(),
3235
mentoring.getCreateDate(),
3336
mentoring.getModifyDate()
3437
);

back/src/main/java/com/back/domain/mentoring/mentoring/dto/request/MentoringRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
import java.util.List;
88

99
public record MentoringRequest(
10-
@Schema(description = "멘토링 제목")
10+
@Schema(description = "멘토링 제목", example = "title")
1111
@NotNull @Size(max = 100)
1212
String title,
1313

1414
@Schema(description = "멘토링 태그", example = "[\"Java\", \"Spring\"]")
1515
List<String> tags,
1616

17-
@Schema(description = "멘토링 소개")
17+
@Schema(description = "멘토링 소개", example = "bio")
1818
@NotNull
1919
String bio,
2020

21-
@Schema(description = "멘토링 썸네일")
21+
@Schema(description = "멘토링 썸네일", example = "test.png")
2222
String thumb
2323
) {
2424
}

back/src/main/java/com/back/domain/mentoring/mentoring/dto/request/ReviewRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public record ReviewRequest(
1111
Double rating,
1212

1313
@Size(max = 1000)
14-
@Schema(description = "리뷰 내용")
14+
@Schema(description = "리뷰 내용", example = "review content")
1515
String content
1616
) {
1717
}

back/src/main/java/com/back/domain/mentoring/mentoring/entity/Mentoring.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public class Mentoring extends BaseEntity {
3030
@Column(length = 255)
3131
private String thumb;
3232

33+
@Column
34+
private double rating = 0.0;
35+
3336
@Builder
3437
public Mentoring(Mentor mentor, String title, String bio, String thumb) {
3538
this.mentor = mentor;
@@ -56,6 +59,10 @@ public void updateTags(List<Tag> tags) {
5659
}
5760
}
5861

62+
public void updateRating(double averageRating) {
63+
this.rating = averageRating;
64+
}
65+
5966
public boolean isOwner(Mentor mentor) {
6067
return this.mentor.equals(mentor);
6168
}

back/src/main/java/com/back/domain/mentoring/mentoring/error/MentoringErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum MentoringErrorCode implements ErrorCode {
2020
NOT_FOUND_MENTEE("404-3", "멘티를 찾을 수 없습니다."),
2121

2222
// 409
23-
ALREADY_EXISTS_MENTORING("409-1", "이미 멘토링 정보가 존재합니다.");
23+
ALREADY_EXISTS_MENTORING("409-1", "이미 동일한 이름의 멘토링이 존재합니다.");
2424

2525
private final String code;
2626
private final String message;

back/src/main/java/com/back/domain/mentoring/mentoring/repository/MentoringRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
public interface MentoringRepository extends JpaRepository<Mentoring, Long>, MentoringRepositoryCustom {
1010
List<Mentoring> findByMentorId(Long mentorId);
1111
Optional<Mentoring> findTopByOrderByIdDesc();
12-
boolean existsByMentorId(Long mentorId);
12+
boolean existsByMentorIdAndTitle(Long mentorId, String title);
13+
boolean existsByMentorIdAndTitleAndIdNot(Long mentorId, String title, Long MentoringId);
1314
}

back/src/main/java/com/back/domain/mentoring/mentoring/repository/ReviewRepository.java

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

3-
import com.back.domain.member.mentor.entity.Mentor;
43
import com.back.domain.mentoring.mentoring.entity.Review;
54
import org.springframework.data.domain.Page;
65
import org.springframework.data.domain.Pageable;
@@ -9,16 +8,26 @@
98
import org.springframework.data.repository.query.Param;
109

1110
public interface ReviewRepository extends JpaRepository<Review, Long> {
12-
boolean existsByReservationId(Long id);
11+
boolean existsByReservationId(Long reservationId);
1312

1413
@Query("""
15-
SELECT AVG(r.rating)
14+
SELECT ROUND(AVG(r.rating), 1)
1615
FROM Review r
1716
INNER JOIN r.reservation res
18-
WHERE res.mentor = :mentor
17+
WHERE res.mentor.id = :mentorId
1918
""")
20-
Double findAverageRating(
21-
@Param("mentor") Mentor mentor
19+
Double calculateMentorAverageRating(
20+
@Param("mentorId") Long mentorId
21+
);
22+
23+
@Query("""
24+
SELECT ROUND(AVG(r.rating), 1)
25+
FROM Review r
26+
INNER JOIN r.reservation res
27+
WHERE res.mentoring.id = :mentoringId
28+
""")
29+
Double calculateMentoringAverageRating(
30+
@Param("mentoringId") Long mentoringId
2231
);
2332

2433
@Query("""

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public MentoringResponse getMentoring(Long mentoringId) {
5151

5252
@Transactional
5353
public MentoringResponse createMentoring(MentoringRequest reqDto, Mentor mentor) {
54-
// 멘토당 멘토링 1개 제한 체크 (추후 1:N 변경 시 제거 필요)
55-
if (mentoringRepository.existsByMentorId(mentor.getId())) {
56-
throw new ServiceException(MentoringErrorCode.ALREADY_EXISTS_MENTORING);
57-
}
54+
validateMentoringTitle(mentor.getId(), reqDto.title());
5855

5956
Mentoring mentoring = Mentoring.builder()
6057
.mentor(mentor)
@@ -79,6 +76,7 @@ public MentoringResponse updateMentoring(Long mentoringId, MentoringRequest reqD
7976
Mentoring mentoring = mentoringStorage.findMentoring(mentoringId);
8077

8178
validateOwner(mentoring, mentor);
79+
validateMentoringTitleForUpdate(mentor.getId(), reqDto.title(), mentoringId);
8280

8381
List<Tag> tags = getOrCreateTags(reqDto.tags());
8482

@@ -100,11 +98,6 @@ public void deleteMentoring(Long mentoringId, Mentor mentor) {
10098
if (mentoringStorage.hasReservationsForMentoring(mentoring.getId())) {
10199
throw new ServiceException(MentoringErrorCode.CANNOT_DELETE_MENTORING);
102100
}
103-
104-
// 멘토 슬롯 있을 시 일괄 삭제 (추후 1:N 변경 시 제거 필요)
105-
if (mentoringStorage.hasMentorSlotsForMentor(mentor.getId())) {
106-
mentoringStorage.deleteMentorSlotsData(mentor.getId());
107-
}
108101
mentoringRepository.delete(mentoring);
109102
}
110103

@@ -153,4 +146,16 @@ private void validateOwner(Mentoring mentoring, Mentor mentor) {
153146
throw new ServiceException(MentoringErrorCode.FORBIDDEN_NOT_OWNER);
154147
}
155148
}
149+
150+
private void validateMentoringTitle(Long mentorId, String title) {
151+
if (mentoringRepository.existsByMentorIdAndTitle(mentorId, title)) {
152+
throw new ServiceException(MentoringErrorCode.ALREADY_EXISTS_MENTORING);
153+
}
154+
}
155+
156+
private void validateMentoringTitleForUpdate(Long mentorId, String title, Long mentoringId) {
157+
if (mentoringRepository.existsByMentorIdAndTitleAndIdNot(mentorId, title, mentoringId)) {
158+
throw new ServiceException(MentoringErrorCode.ALREADY_EXISTS_MENTORING);
159+
}
160+
}
156161
}

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

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

3-
import com.back.domain.member.mentor.entity.Mentor;
43
import com.back.domain.mentoring.mentoring.entity.Mentoring;
54
import com.back.domain.mentoring.mentoring.error.MentoringErrorCode;
65
import com.back.domain.mentoring.mentoring.repository.MentoringRepository;
@@ -36,11 +35,6 @@ public Mentoring findMentoring(Long mentoringId) {
3635
.orElseThrow(() -> new ServiceException(MentoringErrorCode.NOT_FOUND_MENTORING));
3736
}
3837

39-
// TODO : 멘토:멘토링 1:N으로 변경 시 삭제 예정
40-
public Mentoring findMentoringByMentor(Mentor mentor) {
41-
return findMentoringsByMentorId(mentor.getId()).getFirst();
42-
}
43-
4438
public List<Mentoring> findMentoringsByMentorId(Long mentorId) {
4539
List<Mentoring> mentorings = mentoringRepository.findByMentorId(mentorId);
4640
if (mentorings.isEmpty()) {
@@ -73,11 +67,4 @@ public boolean hasMentorSlotsForMentor(Long mentorId) {
7367
public boolean hasReservationForMentorSlot(Long slotId) {
7468
return reservationRepository.existsByMentorSlotId(slotId);
7569
}
76-
77-
78-
// ===== 데이터 조작 메서드 =====
79-
80-
public void deleteMentorSlotsData(Long mentorId) {
81-
mentorSlotRepository.deleteAllByMentorId(mentorId);
82-
}
8370
}

0 commit comments

Comments
 (0)