Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#198 [Refactor] 모델 회원가입 API Service 분리 #217

Merged
merged 8 commits into from
Jan 31, 2024
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ repositories {
}

dependencies {

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

implementation 'com.squareup.okhttp3:okhttp:4.9.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public enum ErrorCode {

// 409 Conflict
ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 유저입니다."),
ALREADY_EXIST_MODEL_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 모델입니다."),
ALREADY_EXIST_OFFER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 제안서입니다"),

// 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import com.moddy.server.controller.auth.dto.response.LoginResponseDto;
import com.moddy.server.controller.designer.dto.request.DesignerCreateRequest;
import com.moddy.server.controller.designer.dto.response.UserCreateResponse;
import com.moddy.server.controller.model.dto.request.ModelCreateRequest;
import com.moddy.server.service.auth.AuthService;
import com.moddy.server.service.designer.DesignerService;
import com.moddy.server.service.model.ModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down Expand Up @@ -51,7 +49,7 @@ public class AuthController {
private static final String ORIGIN = "origin";
private final AuthService authService;
private final DesignerService designerService;
private final ModelService modelService;


@Operation(summary = "[KAKAO CODE] 로그인 API")
@ApiResponses(value = {
Expand Down Expand Up @@ -82,21 +80,6 @@ SuccessResponse<UserCreateResponse> createDesigner(
return SuccessResponse.success(SuccessCode.DESIGNER_CREATE_SUCCESS, designerService.createDesigner(userId, designerInfo, profileImg));
}

@Operation(summary = "[JWT] 모델 회원가입 API", description = "모델 회원가입 API입니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "모델 회원가입 성공"),
@ApiResponse(responseCode = "401", description = "인증오류 입니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "유효하지 않은 값을 입력했습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", description = "서버 내부 오류", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping(value = "/signup/model")
@SecurityRequirement(name = "JWT Auth")
public SuccessResponse<UserCreateResponse> createModel(
@Parameter(hidden = true) @UserId Long userId,
@Valid @RequestBody ModelCreateRequest modelCreateRequest) {
return SuccessResponse.success(SuccessCode.MODEL_CREATE_SUCCESS, modelService.createModel(userId, modelCreateRequest));
}

@Operation(summary = "인증번호 요청 API", description = "인증번호 요청 API입니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "전화번호 인증 요청 성공입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import com.moddy.server.common.exception.enums.SuccessCode;
import com.moddy.server.config.resolver.user.UserId;
import com.moddy.server.controller.auth.dto.response.RegionResponse;
import com.moddy.server.controller.designer.dto.response.UserCreateResponse;
import com.moddy.server.controller.model.dto.request.ModelApplicationRequest;
import com.moddy.server.controller.model.dto.request.ModelCreateRequest;
import com.moddy.server.controller.model.dto.response.ApplicationUserDetailResponse;
import com.moddy.server.controller.model.dto.response.DetailOfferResponse;
import com.moddy.server.controller.model.dto.response.ModelMainResponse;
import com.moddy.server.controller.model.dto.response.OpenChatResponse;
import com.moddy.server.service.model.ModelRegisterService;
import com.moddy.server.service.model.ModelRetrieveService;
import com.moddy.server.service.model.ModelService;
import com.moddy.server.service.offer.HairServiceOfferRetrieveService;
Expand All @@ -29,7 +32,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -42,6 +45,7 @@
public class ModelController {

private final ModelService modelService;
private final ModelRegisterService modelRegisterService;
private final HairServiceOfferRetrieveService hairServiceOfferRetrieveService;
private final ModelRetrieveService modelRetrieveService;

Expand All @@ -56,6 +60,22 @@ public SuccessResponse<List<RegionResponse>> getRegionList() {
return SuccessResponse.success(SuccessCode.FIND_REGION_LIST_SUCCESS, modelRetrieveService.getRegionList());
}

@Tag(name = "Auth Controller", description = "로그인 및 회원 가입 관련 API 입니다.")
Copy link
Member

Choose a reason for hiding this comment

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

p4
혹시 이런 식으로 메서드 별로 태그 걸면 스웨거 상으로 평소처럼 보이는 것일까요?

Copy link
Member Author

Choose a reason for hiding this comment

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

@KWY0218 넵! 예를들어 상위 Controller 단에 'AuthController'태그를 넣고, 하위 메소드 단에 추가로 'ModelController'를 달아두면 두 컨트롤러 단에 다 올라가더라고요 😓

@Operation(summary = "[JWT] 모델 회원가입 API", description = "모델 회원가입 API입니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "모델 회원가입 성공"),
@ApiResponse(responseCode = "401", description = "인증오류 입니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "유효하지 않은 값을 입력했습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", description = "서버 내부 오류", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping(value = "/auth/signup/model")
@SecurityRequirement(name = "JWT Auth")
public SuccessResponse<UserCreateResponse> createModel(
@Parameter(hidden = true) @UserId Long userId,
@Valid @RequestBody ModelCreateRequest modelCreateRequest) {
return SuccessResponse.success(SuccessCode.MODEL_CREATE_SUCCESS, modelRegisterService.createModel(userId, modelCreateRequest));
}

@Tag(name = "ModelController")
@Operation(summary = "[JWT] 모델 메인 뷰 조회", description = "모델 메인 뷰 조회 API입니다.")
@ApiResponses({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.moddy.server.common.validation.year.ValidYear;
import com.moddy.server.common.validation.prefer_regions.ValidPreferRegions;
import com.moddy.server.controller.user.dto.UserUpdateDto;
import com.moddy.server.domain.user.Gender;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.EnumType;
Expand Down Expand Up @@ -35,4 +36,13 @@ public record ModelCreateRequest(
@ValidPreferRegions
List<Long> preferRegions
) {
public UserUpdateDto userInfoUpdate() {
return new UserUpdateDto(
this.name(),
this.gender(),
this.phoneNumber(),
this.isMarketingAgree()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moddy.server.controller.user.dto;

import com.moddy.server.domain.user.Gender;
import jakarta.validation.constraints.NotNull;

@NotNull
public record UserUpdateDto(
String name,
Gender gender,
String phoneNumber,
boolean isMarketingAgree
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class PreferRegion extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -37,4 +36,8 @@ public class PreferRegion extends BaseTimeEntity {
@NotNull
private Region region;

public PreferRegion(Model model, Region region) {
this.model = model;
this.region = region;
}
Comment on lines +39 to +42
Copy link
Member

Choose a reason for hiding this comment

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

p3
👍
이제 빌더를 사용하지 않는다면 위에 @SuperBuilder 어노테이션도 삭제해주시면 좋을 것 같습니다!
만약 다른 곳에서 빌더 사용 중이면 다음에 없애면 될 것 같습니다!

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,64 @@
package com.moddy.server.service.model;

import com.moddy.server.common.exception.enums.ErrorCode;
import com.moddy.server.common.exception.model.ConflictException;
import com.moddy.server.common.exception.model.NotFoundException;
import com.moddy.server.controller.designer.dto.response.UserCreateResponse;
import com.moddy.server.controller.model.dto.request.ModelCreateRequest;
import com.moddy.server.controller.user.dto.UserUpdateDto;
import com.moddy.server.domain.model.Model;
import com.moddy.server.domain.model.repository.ModelJpaRepository;
import com.moddy.server.domain.prefer_region.PreferRegion;
import com.moddy.server.domain.prefer_region.repository.PreferRegionJpaRepository;
import com.moddy.server.domain.region.Region;
import com.moddy.server.domain.region.repository.RegionJpaRepository;
import com.moddy.server.domain.user.Role;
import com.moddy.server.domain.user.User;
import com.moddy.server.domain.user.repository.UserRepository;
import com.moddy.server.external.s3.S3Service;
import com.moddy.server.service.auth.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.moddy.server.common.exception.enums.ErrorCode.USER_NOT_FOUND_EXCEPTION;

@Service
@RequiredArgsConstructor
public class ModelRegisterService {
private final PreferRegionJpaRepository preferRegionJpaRepository;
private final ModelJpaRepository modelJpaRepository;
private final RegionJpaRepository regionJpaRepository;
private final UserRepository userRepository;
private final S3Service s3Service;
private final AuthService authService;

@Transactional
public UserCreateResponse createModel(final Long userId, ModelCreateRequest request) {
if (modelJpaRepository.existsById(userId)) throw new ConflictException(ErrorCode.ALREADY_EXIST_MODEL_EXCEPTION);
updateUserInfos(userId, request.userInfoUpdate());
modelJpaRepository.modelRegister(userId, request.year());
createModelPreferRegions(userId, request.preferRegions());

return authService.createUserToken(userId.toString());
}

private void updateUserInfos(final Long userId, final UserUpdateDto userUpdateDto) {
final User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(USER_NOT_FOUND_EXCEPTION));
user.update(userUpdateDto.name(), userUpdateDto.gender(), userUpdateDto.phoneNumber(), userUpdateDto.isMarketingAgree(), s3Service.getDefaultProfileImageUrl(), Role.MODEL);
}

private void createModelPreferRegions(final Long modelId, final List<Long> preferRegions) {
Model model = modelJpaRepository.findById(modelId).orElseThrow(() -> new NotFoundException(ErrorCode.MODEL_NOT_FOUND_EXCEPTION));

preferRegions.forEach(preferRegionId -> {
Region region = regionJpaRepository.findById(preferRegionId).orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_REGION_EXCEPTION));
PreferRegion preferRegion = new PreferRegion(model, region);
preferRegionJpaRepository.save(preferRegion);
});
}

public void deleteModelInfo(final Long modelId) {
deleteModelPreferRegions(modelId);
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/com/moddy/server/service/model/ModelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,14 @@
public class ModelService {

private final ModelJpaRepository modelJpaRepository;
private final UserRepository userRepository;
private final DesignerJpaRepository designerJpaRepository;
private final HairModelApplicationJpaRepository hairModelApplicationJpaRepository;
private final HairServiceOfferJpaRepository hairServiceOfferJpaRepository;
private final PreferOfferConditionJpaRepository preferOfferConditionJpaRepository;
private final DayOffJpaRepository dayOffJpaRepository;
private final PreferHairStyleJpaRepository preferHairStyleJpaRepository;
private final PreferRegionJpaRepository preferRegionJpaRepository;
private final RegionJpaRepository regionJpaRepository;
private final HairServiceRecordJpaRepository hairServiceRecordJpaRepository;
private final AuthService authService;
private final S3Service s3Service;
private final DesignerRetrieveService designerRetrieveService;

Expand Down Expand Up @@ -106,27 +103,6 @@ public ModelMainResponse getModelMainInfo(Long userId, int page, int size) {
return new ModelMainResponse(page, size, totalElements, modelApplyStatus, user.getName(), offerResponseList);
}

@Transactional
public UserCreateResponse createModel(long userId, ModelCreateRequest request) {

User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));

if (modelJpaRepository.existsById(userId)) throw new ConflictException(ErrorCode.ALREADY_EXIST_USER_EXCEPTION);

user.update(request.name(), request.gender(), request.phoneNumber(), request.isMarketingAgree(), s3Service.getDefaultProfileImageUrl(), Role.MODEL);

modelJpaRepository.modelRegister(userId, request.year());
Model model = modelJpaRepository.findById(userId).orElseThrow(() -> new NotFoundException(ErrorCode.MODEL_NOT_FOUND_EXCEPTION));

request.preferRegions().stream().forEach(preferRegionId -> {
Region region = regionJpaRepository.findById(preferRegionId).orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_REGION_EXCEPTION));
PreferRegion preferRegion = PreferRegion.builder().model(model).region(region).build();
preferRegionJpaRepository.save(preferRegion);
});

return authService.createUserToken(model.getId().toString());
}

@Transactional
public void postApplication(Long userId, MultipartFile modelImgUrl, MultipartFile applicationCaptureImgUrl, ModelApplicationRequest applicationInfo) {

Expand Down
Loading