From 7b6b648707ce4c283f588555011d6194f3d2d4cc Mon Sep 17 00:00:00 2001 From: oosedus Date: Fri, 30 Aug 2024 19:21:59 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[feat]=20dto=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../career/dto/request/SaveCareerRequest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/com/example/letscareer/career/dto/request/SaveCareerRequest.java diff --git a/src/main/java/com/example/letscareer/career/dto/request/SaveCareerRequest.java b/src/main/java/com/example/letscareer/career/dto/request/SaveCareerRequest.java new file mode 100644 index 0000000..29b508d --- /dev/null +++ b/src/main/java/com/example/letscareer/career/dto/request/SaveCareerRequest.java @@ -0,0 +1,17 @@ +package com.example.letscareer.career.dto.request; + +import com.example.letscareer.career.domain.Category; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; + +public record SaveCareerRequest( + @Enumerated(EnumType.STRING) + Category category, + String title, + String situation, + String task, + String action, + String result +) { + +} From fe116fa673ec3cc5cdad91cb2599d254a0c26465 Mon Sep 17 00:00:00 2001 From: oosedus Date: Fri, 30 Aug 2024 19:23:13 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[feat]=20repository=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../letscareer/career/repository/CareerRepository.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/com/example/letscareer/career/repository/CareerRepository.java diff --git a/src/main/java/com/example/letscareer/career/repository/CareerRepository.java b/src/main/java/com/example/letscareer/career/repository/CareerRepository.java new file mode 100644 index 0000000..6eec0cf --- /dev/null +++ b/src/main/java/com/example/letscareer/career/repository/CareerRepository.java @@ -0,0 +1,9 @@ +package com.example.letscareer.career.repository; + +import com.example.letscareer.career.domain.Career; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CareerRepository extends JpaRepository { +} From 6e7aa3f33c83a6945e7090137995a2895b8428b1 Mon Sep 17 00:00:00 2001 From: oosedus Date: Fri, 30 Aug 2024 19:25:42 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[feat]=20=EC=BB=A4=EB=A6=AC=EC=96=B4=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20controller,=20service,=20execption=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../career/controller/CareerController.java | 37 ++++++++++++++ .../career/service/CareerService.java | 49 +++++++++++++++++++ .../common/dto/SuccessNonDataResponse.java | 19 +++++++ .../common/exception/enums/ErrorCode.java | 2 +- .../common/exception/enums/SuccessCode.java | 3 +- .../exception/model/ValidationException.java | 9 ++++ 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/letscareer/career/controller/CareerController.java create mode 100644 src/main/java/com/example/letscareer/career/service/CareerService.java create mode 100644 src/main/java/com/example/letscareer/common/dto/SuccessNonDataResponse.java create mode 100644 src/main/java/com/example/letscareer/common/exception/model/ValidationException.java diff --git a/src/main/java/com/example/letscareer/career/controller/CareerController.java b/src/main/java/com/example/letscareer/career/controller/CareerController.java new file mode 100644 index 0000000..fd37977 --- /dev/null +++ b/src/main/java/com/example/letscareer/career/controller/CareerController.java @@ -0,0 +1,37 @@ +package com.example.letscareer.career.controller; + +import com.example.letscareer.career.dto.request.SaveCareerRequest; +import com.example.letscareer.career.service.CareerService; +import com.example.letscareer.common.dto.ApiResponse; +import com.example.letscareer.common.dto.ErrorResponse; +import com.example.letscareer.common.dto.SuccessNonDataResponse; +import com.example.letscareer.common.exception.enums.ErrorCode; +import com.example.letscareer.common.exception.enums.SuccessCode; +import com.example.letscareer.common.exception.model.BadRequestException; +import com.example.letscareer.common.exception.model.NotFoundException; +import com.example.letscareer.common.exception.model.ValidationException; +import com.example.letscareer.user.domain.User; +import com.example.letscareer.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/careers") +public class CareerController { + + private final UserRepository userRepository; + private final CareerService careerService; + + @PostMapping + public ApiResponse saveCareer(@RequestHeader("userId") Long userId, + @RequestBody SaveCareerRequest request) { + + try { + careerService.saveCareer(userId, request); + return SuccessNonDataResponse.success(SuccessCode.SAVE_CAREER_SUCCESS); + } catch (NotFoundException | BadRequestException | ValidationException e) { + return ErrorResponse.error(e.getErrorCode()); + } + } +} diff --git a/src/main/java/com/example/letscareer/career/service/CareerService.java b/src/main/java/com/example/letscareer/career/service/CareerService.java new file mode 100644 index 0000000..6b754a5 --- /dev/null +++ b/src/main/java/com/example/letscareer/career/service/CareerService.java @@ -0,0 +1,49 @@ +package com.example.letscareer.career.service; + +import com.example.letscareer.career.domain.Career; +import com.example.letscareer.career.dto.request.SaveCareerRequest; +import com.example.letscareer.career.repository.CareerRepository; +import com.example.letscareer.common.exception.enums.ErrorCode; +import com.example.letscareer.common.exception.model.BadRequestException; +import com.example.letscareer.common.exception.model.NotFoundException; +import com.example.letscareer.common.exception.model.ValidationException; +import com.example.letscareer.user.domain.User; +import com.example.letscareer.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CareerService { + + @Autowired + private final CareerRepository careerRepository; + private final UserRepository userRepository; + + public void saveCareer(Long userId, SaveCareerRequest request) { + + User user = userRepository.findByUserId(userId) + .orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION)); + + if(request.title() == null || request.title().isEmpty()) { + throw new ValidationException(ErrorCode.CAREER_TITLE_IS_EMPTY); + } + + try { + Career career = Career.builder() + .user(user) + .category(request.category()) + .title(request.title()) + .situation(request.situation()) + .task(request.task()) + .action(request.action()) + .result(request.result()) + .build(); + + careerRepository.save(career); + } catch (Exception e) { + throw new BadRequestException(ErrorCode.INTERNAL_SERVER_EXCEPTION); + } + } +} diff --git a/src/main/java/com/example/letscareer/common/dto/SuccessNonDataResponse.java b/src/main/java/com/example/letscareer/common/dto/SuccessNonDataResponse.java new file mode 100644 index 0000000..d761367 --- /dev/null +++ b/src/main/java/com/example/letscareer/common/dto/SuccessNonDataResponse.java @@ -0,0 +1,19 @@ +package com.example.letscareer.common.dto; + +import com.example.letscareer.common.exception.enums.SuccessCode; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class SuccessNonDataResponse extends ApiResponse{ + private final int code; + private final String message; + + public static SuccessNonDataResponse success(SuccessCode successCode) { + return new SuccessNonDataResponse(successCode.getHttpStatus().value(), successCode.getMessage()); + } +} diff --git a/src/main/java/com/example/letscareer/common/exception/enums/ErrorCode.java b/src/main/java/com/example/letscareer/common/exception/enums/ErrorCode.java index bab6d2d..b0aa3b6 100644 --- a/src/main/java/com/example/letscareer/common/exception/enums/ErrorCode.java +++ b/src/main/java/com/example/letscareer/common/exception/enums/ErrorCode.java @@ -13,7 +13,7 @@ public enum ErrorCode { INVALID_ENUM_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "요청한 상수 값이 유효하지 않습니다."), INVALID_EMPTY_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "해당 값은 null 또 상수 값이 유효하지 않습니다."), VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "요청값이 유효하지 않습니다."), - + CAREER_TITLE_IS_EMPTY(HttpStatus.BAD_REQUEST, "커리어 제목이 비어있습니다."), //404 NOT_FOUND_RESOURCE_EXCEPTION(HttpStatus.NOT_FOUND, "해당 자원을 찾을 수 없습니다."), diff --git a/src/main/java/com/example/letscareer/common/exception/enums/SuccessCode.java b/src/main/java/com/example/letscareer/common/exception/enums/SuccessCode.java index 083197f..ca6cce2 100644 --- a/src/main/java/com/example/letscareer/common/exception/enums/SuccessCode.java +++ b/src/main/java/com/example/letscareer/common/exception/enums/SuccessCode.java @@ -9,7 +9,8 @@ public enum SuccessCode { - SCHEDULE_SUCCESS(HttpStatus.OK, "일정 찾기 성공입니다."); + SCHEDULE_SUCCESS(HttpStatus.OK, "일정 찾기 성공입니다."), + SAVE_CAREER_SUCCESS(HttpStatus.CREATED, "커리어 등록 성공"); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/com/example/letscareer/common/exception/model/ValidationException.java b/src/main/java/com/example/letscareer/common/exception/model/ValidationException.java new file mode 100644 index 0000000..9deeb7f --- /dev/null +++ b/src/main/java/com/example/letscareer/common/exception/model/ValidationException.java @@ -0,0 +1,9 @@ +package com.example.letscareer.common.exception.model; + +import com.example.letscareer.common.exception.enums.ErrorCode; + +public class ValidationException extends LetsException { + public ValidationException(ErrorCode errorCode) { + super(errorCode); + } +}