-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#3 3.2 커리어 등록 기능 개발
- Loading branch information
Showing
8 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/letscareer/career/controller/CareerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/letscareer/career/dto/request/SaveCareerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/letscareer/career/repository/CareerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Career, Long> { | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/example/letscareer/career/service/CareerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/letscareer/common/dto/SuccessNonDataResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/letscareer/common/exception/model/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |