Skip to content

Commit

Permalink
Feat : Career 활동 수정 API, 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonda02 committed Jul 23, 2024
1 parent 00f3093 commit 7970a5d
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 54 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'mysql:mysql-connector-java:8.0.32'
implementation 'org.springframework.boot:spring-boot-starter-validation'

implementation 'org.openapitools:jackson-databind-nullable:0.2.1'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public CareerResponse<Object> update(@RequestBody @Valid CareerRequestDto.Update
@PathVariable Long careerId) {
LoginUser loginUser = LoginUser.get();
Career updateCareer = careerService.updateCareer(careerId, request);
return null;
return CareerResponse.success(CareerStatusCode.OK,
CareerResponseMessage.CAREER_UPDATE_SUCCESS,
CareerConverter.toUpdateCareerResultDto(updateCareer));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.coyote.Response;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
Expand All @@ -26,7 +25,12 @@ public ResponseEntity<CareerResponse<?>> handleInvalidFormatExceptions(InvalidFo
}

@ExceptionHandler(CareerNotFoundException.class)
public ResponseEntity<CareerResponse<?>> handleCareerNotFoundException(CareerNotFoundException exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CareerResponse.createError(exception.getMessage()));
public ResponseEntity<CareerResponse<?>> handleCareerNotFoundException(CareerNotFoundException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CareerResponse.createError(e.getMessage()));
}

@ExceptionHandler(CareerValidationException.class)
public ResponseEntity<CareerResponse<?>> handleCareerValidationException(CareerValidationException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CareerResponse.createError(e.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package umc.kkijuk.server.career.controller.exception;

public class CareerValidationException extends RuntimeException{
private String field;

public CareerValidationException(String message, String field) {
super(message);
this.field = field;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class CareerResponseMessage {
public static final String CAREER_CREATE_SUCCESS = "활동 추가가 정상적으로 이루어졌습니다.";
public static final String CAREER_UPDATE_SUCCESS = "활동 수정이 정상적으로 이루어졌습니다.";
public static final String CAREER_NOT_FOUND ="존재하는 활동 Id가 아닙니다.";

public static final String CAREER_CREATE_FAIL = "활동 추가를 실패했습니다.";
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/umc/kkijuk/server/career/domain/Career.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public class Career extends BaseEntity {
@JoinColumn(name = "category_id")
private Category category;

public void setName(String name) {
this.name = name;
}

public void setAlias(String alias) {
this.alias = alias;
}

public void setCurrent(Boolean current) {
this.current = current;
}

public void setSummary(String summary) {
this.summary = summary;
}

public void setStartdate(LocalDate startdate) {
this.startdate = startdate;
}

public void setEnddate(LocalDate enddate) {
this.enddate = enddate;
}

public void setCategory(Category category) {
this.category = category;
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/umc/kkijuk/server/career/dto/CareerRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,27 @@ public static class CareerDto{
@Builder
public static class UpdateCareerDto{
@Size(max = 20)
@Schema(description = "활동명", example = "IT 서비스 개발 동아리", type="string")
@Schema(description = "수정된 활동명", example = "IT 서비스 개발 동아리", type="string")
String careerName;


@Size(max = 20)
@Schema(description = "활동 별칭", example = "동아리", type="string")
@Schema(description = "수정된 활동 별칭", example = "동아리", type="string")
String alias;
@Size(max = 50)
@Schema(description = "활동 내역", example = "활동 내역 최대 50자까지 입력 가능(선택사항입니다.)",type = "string")
@Schema(description = "수정된 활동 내역", example = "활동 내역 최대 50자까지 입력 가능(선택사항입니다.)",type = "string")
String summary;
@Schema(description = "활동 여부", example = "false", type = "boolean")
@Schema(description = "수정된 활동 여부", example = "false", type = "boolean")
Boolean isCurrent;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
@Schema(description = "활동 시작 날짜", example = "2024-04-14", type="string")
@Schema(description = "수정된 활동 시작 날짜", example = "2024-04-14", type="string")
LocalDate startDate;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
@Schema(description = "활동 종료 날짜", example = "2024-07-20", type = "string")
@Schema(description = "수정된 활동 종료 날짜", example = "2024-07-20", type = "string")
LocalDate endDate;
@Schema(description = "활동 카테고리", example = "1", type = "int")
int category;
@ValidCategory
@Schema(description = "수정된 활동 카테고리", example = "1", type = "int")
Integer category;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static Career toCareer(CareerRequestDto.CareerDto request){
.alias(request.getAlias())
.summary(request.getSummary())
.startdate(request.getStartDate())
.enddate(request.getEndDate())
.current(request.getIsCurrent())
.enddate(request.getEndDate())
.build();
Expand All @@ -21,4 +22,16 @@ public static CareerResponseDto.CareerResultDto tocareerResultDto(Career career)
.careerId(career.getId())
.build();
}
public static CareerResponseDto.UpdateCareerResultDto toUpdateCareerResultDto(Career career) {
return CareerResponseDto.UpdateCareerResultDto.builder()
.careerName(career.getName())
.alias(career.getAlias())
.summary(career.getSummary())
.isCurrent(career.getCurrent())
.startDate(career.getStartdate())
.endDate(career.getEnddate())
.category(Math.toIntExact(career.getCategory().getId()))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package umc.kkijuk.server.career.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.career.controller.exception.CareerNotFoundException;
import umc.kkijuk.server.career.controller.exception.CareerValidationException;
import umc.kkijuk.server.career.controller.response.CareerResponseMessage;
import umc.kkijuk.server.career.domain.Career;
import umc.kkijuk.server.career.dto.CareerRequestDto;
import umc.kkijuk.server.career.dto.converter.CareerConverter;
import umc.kkijuk.server.career.repository.CareerRepository;
import umc.kkijuk.server.career.repository.CategoryRepository;

import java.beans.PropertyDescriptor;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

@Service
@RequiredArgsConstructor
Expand All @@ -31,6 +25,10 @@ public class CareerServiceImpl implements CareerService {
@Transactional
public Career createCareer(CareerRequestDto.CareerDto request) {
Career career = CareerConverter.toCareer(request);
if(career.getCurrent()){
career.setEnddate(LocalDate.now());
career.setYear(LocalDate.now().getYear());
}
career.setCategory(categoryRepository.findById(Long.valueOf(request.getCategory())).get());
career.setYear(parsingYear(request));
return careerRepository.save(career);
Expand All @@ -42,21 +40,79 @@ public void deleteCareer(Long careerId) {
careerRepository.delete(career.get());
}
@Override
@Transactional
public Career updateCareer(Long careerId, CareerRequestDto.UpdateCareerDto request) {
Career career = findCareer(careerId).get();
if(request.getCategory()!=0){

if (request.getCareerName()!=null) {
career.setName(request.getCareerName());
}
if (request.getAlias()!=null) {
career.setAlias(request.getAlias());
}
if (request.getSummary()!=null) {
career.setSummary(request.getSummary());
}
if (request.getIsCurrent()!=null || request.getEndDate()!=null ) {
updateEndDateAndCurrentStatus(career,request.getIsCurrent(),request.getEndDate());
validatedPeriod(career);
}
if (request.getStartDate()!=null) {
career.setStartdate(request.getStartDate());
}
if(request.getCategory()!=null){
career.setCategory(categoryRepository.findById(Long.valueOf(request.getCategory())).get());
}
return careerRepository.save(career);
}


@Override
public Optional<Career> findCareer(Long careerId) {
return Optional.ofNullable(careerRepository.findById(careerId).orElseThrow(
() -> new CareerNotFoundException(CareerResponseMessage.CAREER_NOT_FOUND.toString())));
}








private void updateEndDateAndCurrentStatus(Career career, Boolean isCurrent, LocalDate endDate) {
if (isCurrent != null) {
career.setCurrent(isCurrent);
if (isCurrent) {
career.setEnddate(LocalDate.now());
career.setYear(LocalDate.now().getYear());
} else {
setEndDate(career, endDate);
}
}else{
setEndDate(career, endDate);
}
}

private void setEndDate(Career career, LocalDate endDate) {
if (endDate != null) {
career.setEnddate(endDate);
career.setYear(endDate.getYear());
}else{
LocalDate existingEndDate = career.getEnddate();
if (existingEndDate != null) {
career.setEnddate(existingEndDate);
}else{
throw new CareerValidationException(CareerResponseMessage.CAREER_ENDDATE, "endDate");
}
}
}

private void validatedPeriod(Career career) {
if(career.getEnddate().isBefore(career.getStartdate())){
throw new CareerValidationException(CareerResponseMessage.CAREER_PERIOD_FAIL, "endDate");
}
}

private int parsingYear(CareerRequestDto.CareerDto request){
if(!request.getIsCurrent()){
return request.getEndDate().getYear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import umc.kkijuk.server.career.controller.response.CareerResponseMessage;
Expand Down
Loading

0 comments on commit 7970a5d

Please sign in to comment.