Skip to content

Commit

Permalink
[feat] 커리어 등록 controller, service, execption 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
oosedus committed Aug 30, 2024
1 parent fe116fa commit 6e7aa3f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, "해당 자원을 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6e7aa3f

Please sign in to comment.