Skip to content
Open
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
@@ -1,5 +1,6 @@
package com.example.solidconnection.admin.service;

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.USER_NOT_FOUND;

Expand All @@ -9,9 +10,11 @@
import com.example.solidconnection.admin.dto.MentorApplicationSearchCondition;
import com.example.solidconnection.admin.dto.MentorApplicationSearchResponse;
import com.example.solidconnection.common.exception.CustomException;
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.repository.MentorApplicationRepository;
import com.example.solidconnection.mentor.repository.MentorRepository;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import com.example.solidconnection.university.domain.HostUniversity;
Expand All @@ -31,6 +34,7 @@ public class AdminMentorApplicationService {
private final MentorApplicationRepository mentorApplicationRepository;
private final HostUniversityRepository hostUniversityRepository;
private final SiteUserRepository siteUserRepository;
private final MentorRepository mentorRepository;

@Transactional(readOnly = true)
public Page<MentorApplicationSearchResponse> searchMentorApplications(
Expand All @@ -44,8 +48,28 @@ public Page<MentorApplicationSearchResponse> searchMentorApplications(
public void approveMentorApplication(Long mentorApplicationId) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));

mentorApplication.approve();

SiteUser siteUser = siteUserRepository.findById(mentorApplication.getSiteUserId())
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
validateUserCanCreateMentor(siteUser.getId());

siteUser.becomeMentor();
Comment on lines 49 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어차피 한 트랜잭션 내에서 작동하니 크게 상관은 없을 거 같긴한데 검증이 먼저되는 게 좋을 거 같아서

siteUser = siteUserRepository.findById(...) // 1. 유저 조회
validateUserCanCreateMentor(siteUser.getId()); // 2. 검증

mentorApplication.approve(); // 3. 상태 변경
siteUser.becomeMentor(); // 4. Role 승격
mentorRepository.save(mentor); // 5. 멘토 생성

이런식으로 순서를 바꾸는 건 어떤가요?
사소하긴 합니다

Mentor mentor = new Mentor(
null,
null,
siteUser.getId(),
mentorApplication.getUniversityId(),
mentorApplication.getTermId()
);
Comment on lines +58 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 null넘기는 것보단 밑에 3개만 받는 생성자나 정적팩토리 메서드 만드는 건 어떤가요?
예전에 형준님이 언급했었던 컨벤션인 거 같긴한데 저희가 잘 지켜지진 않고 있는 거로 압니다..
근데 지금 보니 좀 신경쓰이는 거 같아서 이번거부터 조금씩 고쳐보는 거 어떤가요?


mentorRepository.save(mentor);
}

private void validateUserCanCreateMentor(long siteUserId) {
if (mentorRepository.existsBySiteUserId(siteUserId)) {
throw new CustomException(MENTOR_ALREADY_EXISTS);
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;
Expand All @@ -11,7 +10,6 @@
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;
Expand Down Expand Up @@ -42,14 +40,4 @@ public ResponseEntity<Void> updateMentorMyPage(
mentorMyPageService.updateMentorMyPage(siteUserId, mentorMyPageUpdateRequest);
return ResponseEntity.ok().build();
}

@RequireRoleAccess(roles = Role.MENTOR)
@PostMapping
public ResponseEntity<Void> createMentorMyPage(
@AuthorizedUser long siteUserId,
@Valid @RequestBody MentorMyPageCreateRequest request
) {
mentorMyPageService.createMentorMyPage(siteUserId, request);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class Mentor extends BaseEntity {
@Column
private boolean hasBadge = false;

@Column(length = 1000, nullable = false)
@Column(length = 1000)
private String introduction;

@Column(length = 1000, nullable = false)
@Column(length = 1000)
private String passTip;

@Column
Expand Down Expand Up @@ -96,11 +96,4 @@ public void updateChannels(List<Channel> channels) {
}
}
}

public void createChannels(List<Channel> channels) {
for(Channel channel : channels) {
channel.updateMentor(this);
this.channels.add(channel);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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;
Expand All @@ -11,10 +9,7 @@
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;
Expand Down Expand Up @@ -65,50 +60,17 @@ public void updateMentorMyPage(long siteUserId, MentorMyPageUpdateRequest reques

mentor.updateIntroduction(request.introduction());
mentor.updatePassTip(request.passTip());
updateChannel(request.channels(), mentor);
}

private void updateChannel(List<ChannelRequest> channelRequests, Mentor mentor) {
List<Channel> newChannels = buildChannels(channelRequests);
List<Channel> newChannels = buildChannels(request.channels());
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<ChannelRequest> channelRequests) {
if (channelRequests.size() > CHANNEL_REGISTRATION_LIMIT) {
throw new CustomException(CHANNEL_REGISTRATION_LIMIT_EXCEEDED);
}
}

private void createChannels(List<ChannelRequest> channelRequests, Mentor mentor) {
List<Channel> newChannels = buildChannels(channelRequests);
mentor.createChannels(newChannels);
}

private List<Channel> buildChannels(List<ChannelRequest> channelRequests) {
int sequence = CHANNEL_SEQUENCE_START_NUMBER;
List<Channel> newChannels = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,8 @@ public void updatePassword(String newEncodedPassword) {
public void updateUserStatus(UserStatus status) {
this.userStatus = status;
}

public void becomeMentor() {
this.role = Role.MENTOR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE mentor
MODIFY introduction VARCHAR(1000) NULL;

ALTER TABLE mentor
MODIFY pass_tip VARCHAR(1000) NULL;
Loading
Loading