Skip to content

Commit

Permalink
Merge pull request #217 from TEAM-MODDY/refactor/#198
Browse files Browse the repository at this point in the history
#198 [Refactor] 모델 회원가입 API Service 분리
  • Loading branch information
hellozo0 authored Jan 31, 2024
2 parents ccc9cfd + 1dedd6f commit d085aa0
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 44 deletions.
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 입니다.")
@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;
}
}
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

0 comments on commit d085aa0

Please sign in to comment.