diff --git a/src/main/java/com/example/solidconnection/common/exception/ErrorCode.java b/src/main/java/com/example/solidconnection/common/exception/ErrorCode.java index 17661fe2d..b8eea25eb 100644 --- a/src/main/java/com/example/solidconnection/common/exception/ErrorCode.java +++ b/src/main/java/com/example/solidconnection/common/exception/ErrorCode.java @@ -50,6 +50,7 @@ public enum ErrorCode { BLOCK_USER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "차단 대상 사용자를 찾을 수 없습니다."), TERM_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "존재하지 않는 학기입니다."), CURRENT_TERM_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "현재 학기를 찾을 수 없습니다."), + MENTOR_APPLICATION_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "멘토 지원서가 존재하지 않습니다."), // auth USER_ALREADY_SIGN_OUT(HttpStatus.UNAUTHORIZED.value(), "로그아웃 되었습니다."), @@ -127,6 +128,7 @@ public enum ErrorCode { UNIVERSITY_ID_REQUIRED_FOR_CATALOG(HttpStatus.BAD_REQUEST.value(), "목록에서 학교를 선택한 경우 학교 정보가 필요합니다."), UNIVERSITY_ID_MUST_BE_NULL_FOR_OTHER(HttpStatus.BAD_REQUEST.value(), "기타 학교를 선택한 경우 학교 정보를 입력할 수 없습니다."), INVALID_UNIVERSITY_SELECT_TYPE(HttpStatus.BAD_REQUEST.value(), "지원하지 않는 학교 선택 방식입니다."), + MENTOR_ALREADY_EXISTS(HttpStatus.BAD_REQUEST.value(), "이미 존재하는 멘토입니다."), // socket UNAUTHORIZED_SUBSCRIBE(HttpStatus.FORBIDDEN.value(), "구독 권한이 없습니다."), diff --git a/src/main/java/com/example/solidconnection/mentor/controller/MentorMyPageController.java b/src/main/java/com/example/solidconnection/mentor/controller/MentorMyPageController.java index dd9289a3e..76c951a74 100644 --- a/src/main/java/com/example/solidconnection/mentor/controller/MentorMyPageController.java +++ b/src/main/java/com/example/solidconnection/mentor/controller/MentorMyPageController.java @@ -1,6 +1,7 @@ package com.example.solidconnection.mentor.controller; import com.example.solidconnection.common.resolver.AuthorizedUser; +import com.example.solidconnection.mentor.dto.MentorMyPageCreateRequest; import com.example.solidconnection.mentor.dto.MentorMyPageResponse; import com.example.solidconnection.mentor.dto.MentorMyPageUpdateRequest; import com.example.solidconnection.mentor.service.MentorMyPageService; @@ -10,6 +11,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -40,4 +42,14 @@ public ResponseEntity updateMentorMyPage( mentorMyPageService.updateMentorMyPage(siteUserId, mentorMyPageUpdateRequest); return ResponseEntity.ok().build(); } + + @RequireRoleAccess(roles = Role.MENTOR) + @PostMapping + public ResponseEntity createMentorMyPage( + @AuthorizedUser long siteUserId, + @Valid @RequestBody MentorMyPageCreateRequest request + ) { + mentorMyPageService.createMentorMyPage(siteUserId, request); + return ResponseEntity.ok().build(); + } } diff --git a/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java b/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java index 420170236..30dbfec20 100644 --- a/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java +++ b/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java @@ -53,6 +53,20 @@ public class Mentor extends BaseEntity { @OneToMany(mappedBy = "mentor", cascade = CascadeType.ALL, orphanRemoval = true) private List channels = new ArrayList<>(); + public Mentor( + String introduction, + String passTip, + long siteUserId, + Long universityId, + long termId + ) { + this.introduction = introduction; + this.passTip = passTip; + this.siteUserId = siteUserId; + this.universityId = universityId; + this.termId = termId; + } + public void increaseMenteeCount() { this.menteeCount++; } @@ -82,4 +96,11 @@ public void updateChannels(List channels) { } } } + + public void createChannels(List channels) { + for(Channel channel : channels) { + channel.updateMentor(this); + this.channels.add(channel); + } + } } diff --git a/src/main/java/com/example/solidconnection/mentor/domain/MentorApplication.java b/src/main/java/com/example/solidconnection/mentor/domain/MentorApplication.java index 8f800dcff..29e1960c6 100644 --- a/src/main/java/com/example/solidconnection/mentor/domain/MentorApplication.java +++ b/src/main/java/com/example/solidconnection/mentor/domain/MentorApplication.java @@ -53,6 +53,9 @@ public class MentorApplication extends BaseEntity { @Column(nullable = false, name = "mentor_proof_url", length = 500) private String mentorProofUrl; + @Column(nullable = false, name = "term_id") + private long termId; + private String rejectedReason; @Column(nullable = false) @@ -61,7 +64,7 @@ public class MentorApplication extends BaseEntity { @Column(nullable = false) @Enumerated(EnumType.STRING) - private MentorApplicationStatus mentorApplicationStatus = MentorApplicationStatus.PENDING; + private MentorApplicationStatus mentorApplicationStatus; private static final Set ALLOWED = Collections.unmodifiableSet(EnumSet.of(ExchangeStatus.STUDYING_ABROAD, ExchangeStatus.AFTER_EXCHANGE)); @@ -72,6 +75,7 @@ public MentorApplication( Long universityId, UniversitySelectType universitySelectType, String mentorProofUrl, + long termId, ExchangeStatus exchangeStatus ) { validateExchangeStatus(exchangeStatus); @@ -82,7 +86,9 @@ public MentorApplication( this.universityId = universityId; this.universitySelectType = universitySelectType; this.mentorProofUrl = mentorProofUrl; + this.termId = termId; this.exchangeStatus = exchangeStatus; + this.mentorApplicationStatus = MentorApplicationStatus.PENDING; } private void validateUniversitySelection(UniversitySelectType universitySelectType, Long universityId) { diff --git a/src/main/java/com/example/solidconnection/mentor/dto/MentorApplicationRequest.java b/src/main/java/com/example/solidconnection/mentor/dto/MentorApplicationRequest.java index da6d206ab..c4c09977b 100644 --- a/src/main/java/com/example/solidconnection/mentor/dto/MentorApplicationRequest.java +++ b/src/main/java/com/example/solidconnection/mentor/dto/MentorApplicationRequest.java @@ -3,12 +3,23 @@ import com.example.solidconnection.mentor.domain.UniversitySelectType; import com.example.solidconnection.siteuser.domain.ExchangeStatus; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; public record MentorApplicationRequest( + @NotNull(message = "교환 상태를 입력해주세요.") @JsonProperty("preparationStatus") ExchangeStatus exchangeStatus, + + @NotNull(message = "대학교 선택 유형을 입력해주세요.") UniversitySelectType universitySelectType, + + @NotNull(message = "국가를 입력해주세요") String country, - Long universityId + + Long universityId, + + @NotBlank(message = "학기를 입력해주세요.") + String term ) { } diff --git a/src/main/java/com/example/solidconnection/mentor/dto/MentorMyPageCreateRequest.java b/src/main/java/com/example/solidconnection/mentor/dto/MentorMyPageCreateRequest.java new file mode 100644 index 000000000..be0269b29 --- /dev/null +++ b/src/main/java/com/example/solidconnection/mentor/dto/MentorMyPageCreateRequest.java @@ -0,0 +1,20 @@ +package com.example.solidconnection.mentor.dto; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import java.util.List; + +public record MentorMyPageCreateRequest( + @NotBlank(message = "자기소개를 입력해주세요.") + String introduction, + + @NotBlank(message = "합격 레시피를 입력해주세요.") + String passTip, + + @NotNull + @Valid + List channels +) { + +} diff --git a/src/main/java/com/example/solidconnection/mentor/repository/MentorApplicationRepository.java b/src/main/java/com/example/solidconnection/mentor/repository/MentorApplicationRepository.java index d03b53892..43b38ef4b 100644 --- a/src/main/java/com/example/solidconnection/mentor/repository/MentorApplicationRepository.java +++ b/src/main/java/com/example/solidconnection/mentor/repository/MentorApplicationRepository.java @@ -3,9 +3,12 @@ import com.example.solidconnection.mentor.domain.MentorApplication; import com.example.solidconnection.mentor.domain.MentorApplicationStatus; import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; public interface MentorApplicationRepository extends JpaRepository { boolean existsBySiteUserIdAndMentorApplicationStatusIn(long siteUserId, List mentorApplicationStatuses); + + Optional findBySiteUserIdAndMentorApplicationStatus(long siteUserId, MentorApplicationStatus mentorApplicationStatus); } diff --git a/src/main/java/com/example/solidconnection/mentor/service/MentorApplicationService.java b/src/main/java/com/example/solidconnection/mentor/service/MentorApplicationService.java index d0716a156..e4e187808 100644 --- a/src/main/java/com/example/solidconnection/mentor/service/MentorApplicationService.java +++ b/src/main/java/com/example/solidconnection/mentor/service/MentorApplicationService.java @@ -10,6 +10,8 @@ import com.example.solidconnection.s3.service.S3Service; import com.example.solidconnection.siteuser.domain.SiteUser; import com.example.solidconnection.siteuser.repository.SiteUserRepository; +import com.example.solidconnection.term.domain.Term; +import com.example.solidconnection.term.repository.TermRepository; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -18,6 +20,7 @@ import org.springframework.web.multipart.MultipartFile; import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_APPLICATION_ALREADY_EXISTED; +import static com.example.solidconnection.common.exception.ErrorCode.TERM_NOT_FOUND; import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND; @Service @@ -28,6 +31,7 @@ public class MentorApplicationService { private final MentorApplicationRepository mentorApplicationRepository; private final SiteUserRepository siteUserRepository; private final S3Service s3Service; + private final TermRepository termRepository; @Transactional public void submitMentorApplication( @@ -35,15 +39,12 @@ public void submitMentorApplication( MentorApplicationRequest mentorApplicationRequest, MultipartFile file ) { - if (mentorApplicationRepository.existsBySiteUserIdAndMentorApplicationStatusIn( - siteUserId, - List.of(MentorApplicationStatus.PENDING, MentorApplicationStatus.APPROVED)) - ) { - throw new CustomException(MENTOR_APPLICATION_ALREADY_EXISTED); - } + ensureNoPendingOrApprovedMentorApplication(siteUserId); SiteUser siteUser = siteUserRepository.findById(siteUserId) .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); + Term term = termRepository.findByName(mentorApplicationRequest.term()) + .orElseThrow(() -> new CustomException(TERM_NOT_FOUND)); UploadedFileUrlResponse uploadedFile = s3Service.uploadFile(file, ImgType.MENTOR_PROOF); MentorApplication mentorApplication = new MentorApplication( siteUser.getId(), @@ -51,8 +52,18 @@ public void submitMentorApplication( mentorApplicationRequest.universityId(), mentorApplicationRequest.universitySelectType(), uploadedFile.fileUrl(), + term.getId(), mentorApplicationRequest.exchangeStatus() ); mentorApplicationRepository.save(mentorApplication); } + + private void ensureNoPendingOrApprovedMentorApplication(long siteUserId) { + if (mentorApplicationRepository.existsBySiteUserIdAndMentorApplicationStatusIn( + siteUserId, + List.of(MentorApplicationStatus.PENDING, MentorApplicationStatus.APPROVED)) + ) { + throw new CustomException(MENTOR_APPLICATION_ALREADY_EXISTED); + } + } } diff --git a/src/main/java/com/example/solidconnection/mentor/service/MentorMyPageService.java b/src/main/java/com/example/solidconnection/mentor/service/MentorMyPageService.java index 810891b2b..7f226f380 100644 --- a/src/main/java/com/example/solidconnection/mentor/service/MentorMyPageService.java +++ b/src/main/java/com/example/solidconnection/mentor/service/MentorMyPageService.java @@ -1,6 +1,8 @@ package com.example.solidconnection.mentor.service; import static com.example.solidconnection.common.exception.ErrorCode.CHANNEL_REGISTRATION_LIMIT_EXCEEDED; +import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_ALREADY_EXISTS; +import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_APPLICATION_NOT_FOUND; import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_NOT_FOUND; import static com.example.solidconnection.common.exception.ErrorCode.TERM_NOT_FOUND; import static com.example.solidconnection.common.exception.ErrorCode.UNIVERSITY_NOT_FOUND; @@ -9,9 +11,13 @@ import com.example.solidconnection.common.exception.CustomException; import com.example.solidconnection.mentor.domain.Channel; import com.example.solidconnection.mentor.domain.Mentor; +import com.example.solidconnection.mentor.domain.MentorApplication; +import com.example.solidconnection.mentor.domain.MentorApplicationStatus; import com.example.solidconnection.mentor.dto.ChannelRequest; +import com.example.solidconnection.mentor.dto.MentorMyPageCreateRequest; import com.example.solidconnection.mentor.dto.MentorMyPageResponse; import com.example.solidconnection.mentor.dto.MentorMyPageUpdateRequest; +import com.example.solidconnection.mentor.repository.MentorApplicationRepository; import com.example.solidconnection.mentor.repository.MentorRepository; import com.example.solidconnection.siteuser.domain.SiteUser; import com.example.solidconnection.siteuser.repository.SiteUserRepository; @@ -36,6 +42,7 @@ public class MentorMyPageService { private final SiteUserRepository siteUserRepository; private final UniversityRepository universityRepository; private final TermRepository termRepository; + private final MentorApplicationRepository mentorApplicationRepository; @Transactional(readOnly = true) public MentorMyPageResponse getMentorMyPage(long siteUserId) { @@ -61,18 +68,53 @@ public void updateMentorMyPage(long siteUserId, MentorMyPageUpdateRequest reques updateChannel(request.channels(), mentor); } + private void updateChannel(List channelRequests, Mentor mentor) { + List newChannels = buildChannels(channelRequests); + mentor.updateChannels(newChannels); + } + + @Transactional + public void createMentorMyPage(long siteUserId, MentorMyPageCreateRequest request) { + validateUserCanCreateMentor(siteUserId); + validateChannelRegistrationLimit(request.channels()); + MentorApplication mentorApplication = mentorApplicationRepository.findBySiteUserIdAndMentorApplicationStatus(siteUserId, MentorApplicationStatus.APPROVED) + .orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND)); + + Mentor mentor = new Mentor( + request.introduction(), + request.passTip(), + siteUserId, + mentorApplication.getUniversityId(), + mentorApplication.getTermId() + ); + + createChannels(request.channels(), mentor); + mentorRepository.save(mentor); + } + + private void validateUserCanCreateMentor(long siteUserId) { + if (mentorRepository.existsBySiteUserId(siteUserId)) { + throw new CustomException(MENTOR_ALREADY_EXISTS); + } + } + private void validateChannelRegistrationLimit(List channelRequests) { if (channelRequests.size() > CHANNEL_REGISTRATION_LIMIT) { throw new CustomException(CHANNEL_REGISTRATION_LIMIT_EXCEEDED); } } - private void updateChannel(List channelRequests, Mentor mentor) { + private void createChannels(List channelRequests, Mentor mentor) { + List newChannels = buildChannels(channelRequests); + mentor.createChannels(newChannels); + } + + private List buildChannels(List channelRequests) { int sequence = CHANNEL_SEQUENCE_START_NUMBER; List newChannels = new ArrayList<>(); for (ChannelRequest request : channelRequests) { newChannels.add(new Channel(sequence++, request.type(), request.url())); } - mentor.updateChannels(newChannels); + return newChannels; } } diff --git a/src/main/java/com/example/solidconnection/term/repository/TermRepository.java b/src/main/java/com/example/solidconnection/term/repository/TermRepository.java index 7badf27cb..763137dc9 100644 --- a/src/main/java/com/example/solidconnection/term/repository/TermRepository.java +++ b/src/main/java/com/example/solidconnection/term/repository/TermRepository.java @@ -7,4 +7,6 @@ public interface TermRepository extends JpaRepository { Optional findByIsCurrentTrue(); + + Optional findByName(String name); } diff --git a/src/main/resources/db/migration/V38__add_term_id_to_mentor_application.sql b/src/main/resources/db/migration/V38__add_term_id_to_mentor_application.sql new file mode 100644 index 000000000..87867f44e --- /dev/null +++ b/src/main/resources/db/migration/V38__add_term_id_to_mentor_application.sql @@ -0,0 +1,6 @@ +ALTER TABLE mentor_application + ADD COLUMN term_id BIGINT NOT NULL; + +ALTER TABLE mentor_application + ADD CONSTRAINT fk_mentor_application_term_id + FOREIGN KEY (term_id) REFERENCES term(id); \ No newline at end of file diff --git a/src/main/resources/secret b/src/main/resources/secret index 2f4a16822..8300cdeca 160000 --- a/src/main/resources/secret +++ b/src/main/resources/secret @@ -1 +1 @@ -Subproject commit 2f4a168223ea81cfe3447d6d95441b1a020fdbfe +Subproject commit 8300cdecaebfc28fd657064a00a44815a7bb2eee diff --git a/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixture.java b/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixture.java index 918db7ef4..0baf62e2f 100644 --- a/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixture.java +++ b/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixture.java @@ -4,6 +4,7 @@ import com.example.solidconnection.mentor.domain.MentorApplicationStatus; import com.example.solidconnection.mentor.domain.UniversitySelectType; import com.example.solidconnection.siteuser.domain.ExchangeStatus; +import com.example.solidconnection.term.fixture.TermFixture; import lombok.RequiredArgsConstructor; import org.springframework.boot.test.context.TestComponent; @@ -12,6 +13,7 @@ public class MentorApplicationFixture { private final MentorApplicationFixtureBuilder mentorApplicationFixtureBuilder; + private final TermFixture termFixture; private static final String DEFAULT_COUNTRY_CODE = "US"; private static final String DEFAULT_PROOF_URL = "/mentor-proof.pdf"; @@ -28,6 +30,7 @@ public class MentorApplicationFixture { .universityId(universityId) .universitySelectType(selectType) .mentorProofUrl(DEFAULT_PROOF_URL) + .termId(termFixture.현재_학기("2025-1").getId()) .exchangeStatus(DEFAULT_EXCHANGE_STATUS) .create(); } @@ -43,6 +46,7 @@ public class MentorApplicationFixture { .universityId(universityId) .universitySelectType(selectType) .mentorProofUrl(DEFAULT_PROOF_URL) + .termId(termFixture.현재_학기("2025-1").getId()) .exchangeStatus(DEFAULT_EXCHANGE_STATUS) .mentorApplicationStatus(MentorApplicationStatus.APPROVED) .create(); @@ -59,6 +63,7 @@ public class MentorApplicationFixture { .universityId(universityId) .universitySelectType(selectType) .mentorProofUrl(DEFAULT_PROOF_URL) + .termId(termFixture.현재_학기("2025-1").getId()) .exchangeStatus(DEFAULT_EXCHANGE_STATUS) .mentorApplicationStatus(MentorApplicationStatus.REJECTED) .create(); diff --git a/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixtureBuilder.java b/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixtureBuilder.java index fc6fe547d..fd2a74ff6 100644 --- a/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixtureBuilder.java +++ b/src/test/java/com/example/solidconnection/mentor/fixture/MentorApplicationFixtureBuilder.java @@ -20,6 +20,7 @@ public class MentorApplicationFixtureBuilder { private Long universityId = null; private UniversitySelectType universitySelectType = UniversitySelectType.OTHER; private String mentorProofUrl = "/mentor-proof.pdf"; + private long termId; private ExchangeStatus exchangeStatus = ExchangeStatus.AFTER_EXCHANGE; private MentorApplicationStatus mentorApplicationStatus = MentorApplicationStatus.PENDING; @@ -52,6 +53,11 @@ public MentorApplicationFixtureBuilder mentorProofUrl(String mentorProofUrl) { return this; } + public MentorApplicationFixtureBuilder termId(long termId) { + this.termId = termId; + return this; + } + public MentorApplicationFixtureBuilder exchangeStatus(ExchangeStatus exchangeStatus) { this.exchangeStatus = exchangeStatus; return this; @@ -69,6 +75,7 @@ public MentorApplication create() { universityId, universitySelectType, mentorProofUrl, + termId, exchangeStatus ); ReflectionTestUtils.setField(mentorApplication, "mentorApplicationStatus", mentorApplicationStatus); diff --git a/src/test/java/com/example/solidconnection/mentor/service/MentorApplicationServiceTest.java b/src/test/java/com/example/solidconnection/mentor/service/MentorApplicationServiceTest.java index aaf63b600..daa429fc3 100644 --- a/src/test/java/com/example/solidconnection/mentor/service/MentorApplicationServiceTest.java +++ b/src/test/java/com/example/solidconnection/mentor/service/MentorApplicationServiceTest.java @@ -20,6 +20,8 @@ import com.example.solidconnection.siteuser.domain.SiteUser; import com.example.solidconnection.siteuser.fixture.SiteUserFixture; import com.example.solidconnection.support.TestContainerSpringBootTest; +import com.example.solidconnection.term.domain.Term; +import com.example.solidconnection.term.fixture.TermFixture; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -41,6 +43,9 @@ public class MentorApplicationServiceTest { @Autowired private SiteUserFixture siteUserFixture; + @Autowired + private TermFixture termFixture; + @Autowired private MentorApplicationFixture mentorApplicationFixture; @@ -48,10 +53,12 @@ public class MentorApplicationServiceTest { private S3Service s3Service; private SiteUser user; + private Term term; @BeforeEach void setUp() { user = siteUserFixture.사용자(); + term = termFixture.현재_학기("2025-1"); } @Test @@ -190,7 +197,8 @@ private MentorApplicationRequest createMentorApplicationRequest(UniversitySelect ExchangeStatus.AFTER_EXCHANGE, universitySelectType, "US", - universityId + universityId, + term.getName() ); } } diff --git a/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java b/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java index 7beb2e4f0..cecae13be 100644 --- a/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java +++ b/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java @@ -1,24 +1,35 @@ package com.example.solidconnection.mentor.service; +import static com.example.solidconnection.common.exception.ErrorCode.CHANNEL_REGISTRATION_LIMIT_EXCEEDED; +import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_ALREADY_EXISTS; +import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_APPLICATION_NOT_FOUND; import static com.example.solidconnection.mentor.domain.ChannelType.BLOG; +import static com.example.solidconnection.mentor.domain.ChannelType.BRUNCH; import static com.example.solidconnection.mentor.domain.ChannelType.INSTAGRAM; +import static com.example.solidconnection.mentor.domain.ChannelType.YOUTUBE; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.tuple; import static org.junit.jupiter.api.Assertions.assertAll; +import com.example.solidconnection.common.exception.CustomException; import com.example.solidconnection.mentor.domain.Channel; import com.example.solidconnection.mentor.domain.Mentor; +import com.example.solidconnection.mentor.domain.UniversitySelectType; import com.example.solidconnection.mentor.dto.ChannelRequest; import com.example.solidconnection.mentor.dto.ChannelResponse; +import com.example.solidconnection.mentor.dto.MentorMyPageCreateRequest; import com.example.solidconnection.mentor.dto.MentorMyPageResponse; import com.example.solidconnection.mentor.dto.MentorMyPageUpdateRequest; import com.example.solidconnection.mentor.fixture.ChannelFixture; +import com.example.solidconnection.mentor.fixture.MentorApplicationFixture; import com.example.solidconnection.mentor.fixture.MentorFixture; import com.example.solidconnection.mentor.repository.ChannelRepositoryForTest; import com.example.solidconnection.mentor.repository.MentorRepository; import com.example.solidconnection.siteuser.domain.SiteUser; import com.example.solidconnection.siteuser.fixture.SiteUserFixture; import com.example.solidconnection.support.TestContainerSpringBootTest; +import com.example.solidconnection.term.domain.Term; import com.example.solidconnection.term.fixture.TermFixture; import com.example.solidconnection.university.domain.University; import com.example.solidconnection.university.fixture.UniversityFixture; @@ -57,16 +68,22 @@ class MentorMyPageServiceTest { @Autowired private ChannelRepositoryForTest channelRepositoryForTest; + @Autowired + private MentorApplicationFixture mentorApplicationFixture; + private SiteUser mentorUser; private Mentor mentor; private University university; + private SiteUser siteUser; + private Term term; @BeforeEach void setUp() { - termFixture.현재_학기("2025-2"); + term = termFixture.현재_학기("2025-1"); university = universityFixture.메이지_대학(); mentorUser = siteUserFixture.멘토(1, "멘토"); mentor = mentorFixture.멘토(mentorUser.getId(), university.getId()); + siteUser = siteUserFixture.사용자(); } @Nested @@ -155,4 +172,111 @@ class 멘토의_마이_페이지를_수정한다 { ); } } + + @Nested + class 멘토의_마이페이지를_생성한다 { + + @Test + void 멘토_정보를_생성한다() { + // given + String introduction = "멘토 자기소개"; + String passTip = "멘토의 합격 팁"; + List channels = List.of( + new ChannelRequest(BLOG, "https://blog.com"), + new ChannelRequest(INSTAGRAM, "https://instagram.com"), + new ChannelRequest(YOUTUBE, "https://youtubr.com"), + new ChannelRequest(BRUNCH, "https://brunch.com") + ); + MentorMyPageCreateRequest request = new MentorMyPageCreateRequest(introduction, passTip, channels); + mentorApplicationFixture.승인된_멘토신청(siteUser.getId(), UniversitySelectType.CATALOG, university.getId()); + + // when + mentorMyPageService.createMentorMyPage(siteUser.getId(), request); + + // then + Mentor createMentor = mentorRepository.findBySiteUserId(siteUser.getId()).get(); + List createChannels = channelRepositoryForTest.findAllByMentorId(createMentor.getId()); + assertAll( + () -> assertThat(createMentor.getIntroduction()).isEqualTo(introduction), + () -> assertThat(createMentor.getPassTip()).isEqualTo(passTip), + () -> assertThat(createMentor.getTermId()).isEqualTo(term.getId()), + () -> assertThat(createMentor.getUniversityId()).isEqualTo(university.getId()), + () -> assertThat(createMentor.getSiteUserId()).isEqualTo(siteUser.getId()), + () -> assertThat(createMentor.getMenteeCount()).isEqualTo(0), + () -> assertThat(createMentor.isHasBadge()).isFalse(), + () -> assertThat(createChannels).extracting(Channel::getSequence, Channel::getType, Channel::getUrl) + .containsExactlyInAnyOrder( + tuple(1, BLOG, "https://blog.com"), + tuple(2, INSTAGRAM, "https://instagram.com"), + tuple(3, YOUTUBE, "https://youtubr.com"), + tuple(4, BRUNCH, "https://brunch.com") + ) + ); + } + + @Test + void 이미_멘토_정보가_존재하는데_생성_요청_시_예외가_발생한다() { + // given + MentorMyPageCreateRequest request = new MentorMyPageCreateRequest("introduction", "passTip", List.of()); + mentorFixture.멘토(siteUser.getId(), university.getId()); + + // when & then + assertThatCode(() -> mentorMyPageService.createMentorMyPage(siteUser.getId(), request)) + .isInstanceOf(CustomException.class) + .hasMessage(MENTOR_ALREADY_EXISTS.getMessage()); + } + + @Test + void 채널을_제한_이상_생성하면_예외가_발생한다() { + // given + List newChannels = List.of( + new ChannelRequest(BLOG, "https://blog.com"), + new ChannelRequest(INSTAGRAM, "https://instagram.com"), + new ChannelRequest(YOUTUBE, "https://youtubr.com"), + new ChannelRequest(BRUNCH, "https://brunch.com"), + new ChannelRequest(BLOG, "https://blog.com") + ); + MentorMyPageCreateRequest request = new MentorMyPageCreateRequest("introduction", "passTip", newChannels); + + // when & then + assertThatCode(() -> mentorMyPageService.createMentorMyPage(siteUser.getId(), request)) + .isInstanceOf(CustomException.class) + .hasMessage(CHANNEL_REGISTRATION_LIMIT_EXCEEDED.getMessage()); + } + + @Test + void 멘토_승격_요청_없이_멘토_정보_생성_시_예외가_발생한다() { + // given + MentorMyPageCreateRequest request = new MentorMyPageCreateRequest("introduction", "passTip", List.of()); + + // when & then + assertThatCode(() -> mentorMyPageService.createMentorMyPage(siteUser.getId(), request)) + .isInstanceOf(CustomException.class) + .hasMessage(MENTOR_APPLICATION_NOT_FOUND.getMessage()); + } + + @Test + void 멘토_승격_요청_상태가_REJECTED_면_예외가_발생한다() { + // given + MentorMyPageCreateRequest request = new MentorMyPageCreateRequest("introduction", "passTip", List.of()); + mentorApplicationFixture.거절된_멘토신청(siteUser.getId(), UniversitySelectType.CATALOG, university.getId()); + + // when & then + assertThatCode(() -> mentorMyPageService.createMentorMyPage(siteUser.getId(), request)) + .isInstanceOf(CustomException.class) + .hasMessage(MENTOR_APPLICATION_NOT_FOUND.getMessage()); + } + + @Test + void 멘토_승격_요청_상태가_PENDING_면_예외가_발생한다() { + // given + MentorMyPageCreateRequest request = new MentorMyPageCreateRequest("introduction", "passTip", List.of()); + mentorApplicationFixture.대기중_멘토신청(siteUser.getId(), UniversitySelectType.CATALOG, university.getId()); + + // when & then + assertThatCode(() -> mentorMyPageService.createMentorMyPage(siteUser.getId(), request)) + .isInstanceOf(CustomException.class) + .hasMessage(MENTOR_APPLICATION_NOT_FOUND.getMessage()); + } + } }