diff --git a/build.gradle b/build.gradle index 96a57085..1edc6fe7 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ repositories { } dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'com.squareup.okhttp3:okhttp:4.9.3' diff --git a/src/main/java/com/moddy/server/common/exception/enums/ErrorCode.java b/src/main/java/com/moddy/server/common/exception/enums/ErrorCode.java index 8a777ca2..91f26dde 100644 --- a/src/main/java/com/moddy/server/common/exception/enums/ErrorCode.java +++ b/src/main/java/com/moddy/server/common/exception/enums/ErrorCode.java @@ -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 diff --git a/src/main/java/com/moddy/server/controller/auth/AuthController.java b/src/main/java/com/moddy/server/controller/auth/AuthController.java index 111cfb75..f5a77e26 100644 --- a/src/main/java/com/moddy/server/controller/auth/AuthController.java +++ b/src/main/java/com/moddy/server/controller/auth/AuthController.java @@ -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; @@ -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 = { @@ -82,21 +80,6 @@ SuccessResponse 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 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 = "전화번호 인증 요청 성공입니다."), diff --git a/src/main/java/com/moddy/server/controller/model/ModelController.java b/src/main/java/com/moddy/server/controller/model/ModelController.java index a9b5ed92..a8337d72 100644 --- a/src/main/java/com/moddy/server/controller/model/ModelController.java +++ b/src/main/java/com/moddy/server/controller/model/ModelController.java @@ -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; @@ -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; @@ -42,6 +45,7 @@ public class ModelController { private final ModelService modelService; + private final ModelRegisterService modelRegisterService; private final HairServiceOfferRetrieveService hairServiceOfferRetrieveService; private final ModelRetrieveService modelRetrieveService; @@ -56,6 +60,22 @@ public SuccessResponse> 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 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({ diff --git a/src/main/java/com/moddy/server/controller/model/dto/request/ModelCreateRequest.java b/src/main/java/com/moddy/server/controller/model/dto/request/ModelCreateRequest.java index 427a3840..0338a9a4 100644 --- a/src/main/java/com/moddy/server/controller/model/dto/request/ModelCreateRequest.java +++ b/src/main/java/com/moddy/server/controller/model/dto/request/ModelCreateRequest.java @@ -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; @@ -35,4 +36,13 @@ public record ModelCreateRequest( @ValidPreferRegions List preferRegions ) { + public UserUpdateDto userInfoUpdate() { + return new UserUpdateDto( + this.name(), + this.gender(), + this.phoneNumber(), + this.isMarketingAgree() + ); + } + } diff --git a/src/main/java/com/moddy/server/controller/user/dto/UserUpdateDto.java b/src/main/java/com/moddy/server/controller/user/dto/UserUpdateDto.java new file mode 100644 index 00000000..eb325361 --- /dev/null +++ b/src/main/java/com/moddy/server/controller/user/dto/UserUpdateDto.java @@ -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 +) { +} diff --git a/src/main/java/com/moddy/server/domain/prefer_region/PreferRegion.java b/src/main/java/com/moddy/server/domain/prefer_region/PreferRegion.java index 338cb86a..dde72080 100644 --- a/src/main/java/com/moddy/server/domain/prefer_region/PreferRegion.java +++ b/src/main/java/com/moddy/server/domain/prefer_region/PreferRegion.java @@ -21,7 +21,6 @@ @Getter @NoArgsConstructor @AllArgsConstructor -@SuperBuilder public class PreferRegion extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -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; + } } diff --git a/src/main/java/com/moddy/server/service/model/ModelRegisterService.java b/src/main/java/com/moddy/server/service/model/ModelRegisterService.java index 3d98a0dc..1938a57e 100644 --- a/src/main/java/com/moddy/server/service/model/ModelRegisterService.java +++ b/src/main/java/com/moddy/server/service/model/ModelRegisterService.java @@ -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 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); diff --git a/src/main/java/com/moddy/server/service/model/ModelService.java b/src/main/java/com/moddy/server/service/model/ModelService.java index 7b40a7e1..ef51f595 100644 --- a/src/main/java/com/moddy/server/service/model/ModelService.java +++ b/src/main/java/com/moddy/server/service/model/ModelService.java @@ -62,7 +62,6 @@ public class ModelService { private final ModelJpaRepository modelJpaRepository; - private final UserRepository userRepository; private final DesignerJpaRepository designerJpaRepository; private final HairModelApplicationJpaRepository hairModelApplicationJpaRepository; private final HairServiceOfferJpaRepository hairServiceOfferJpaRepository; @@ -70,9 +69,7 @@ public class ModelService { 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; @@ -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) {