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/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 +) { + +} 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 { +} 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); + } +}