Skip to content

Commit

Permalink
[feat] 전형 단게 추가 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
oosedus committed Sep 2, 2024
1 parent f3ab993 commit 7256a52
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public enum SuccessCode {
MID_REVIEW_SAVE_SUCCESS(HttpStatus.CREATED, "중간 전형 회고 추가 성공"),
INT_REVIEW_SAVE_SUCCESS(HttpStatus.CREATED, "면접 회고 추가 성공"),
SELF_INTRO_SAVE_SUCCESS(HttpStatus.CREATED, "자기소개 추가 성공"),
APPEAL_CAREERS_ADD_SUCCESS(HttpStatus.CREATED, "어필할 커리어 추가 성공");
APPEAL_CAREERS_ADD_SUCCESS(HttpStatus.CREATED, "어필할 커리어 추가 성공"),
STAGE_ADD_SUCCESS(HttpStatus.CREATED, "전형 단계 추가 성공");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.letscareer.schedule.domain;

import com.example.letscareer.stage.domain.Stage;
import com.example.letscareer.user.domain.User;
import jakarta.persistence.*;
import lombok.*;
import org.antlr.v4.runtime.misc.NotNull;

import java.util.Date;
import java.util.List;

@Entity
@Getter
Expand All @@ -31,4 +33,7 @@ public class Schedule {
private Progress progress;

private String url;

@OneToMany(mappedBy = "schedule", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Stage> stages;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.letscareer.stage.controller;

import com.example.letscareer.common.dto.ApiResponse;
import com.example.letscareer.common.dto.ErrorResponse;
import com.example.letscareer.common.dto.SuccessResponse;
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.stage.dto.request.AddStageRequest;
import com.example.letscareer.stage.dto.response.AddStageResponse;
import com.example.letscareer.stage.service.StageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class StageController {

private final StageService stageService;

@PostMapping("/schedules/{scheduleId}/stages")
public ApiResponse addStage(
@RequestHeader("userId") Long userId,
@PathVariable Long scheduleId,
@RequestBody AddStageRequest request
) {

try {
AddStageResponse response = stageService.addStage(userId, scheduleId, request);
return SuccessResponse.success(SuccessCode.STAGE_ADD_SUCCESS, response);
} catch (NotFoundException | BadRequestException e) {
return ErrorResponse.error(e.getErrorCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.letscareer.stage.dto.request;

import com.example.letscareer.stage.domain.Type;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;

import java.time.LocalDate;

public record AddStageRequest(
@Enumerated(EnumType.STRING)
Type type,
String mid_name,
LocalDate date
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.letscareer.stage.dto.response;

import java.time.LocalDate;

public record AddStageResponse(
Long stageId,
String type,
String mid_name,
String status,
LocalDate date,
int dday
) {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@
package com.example.letscareer.stage.service;

import com.example.letscareer.common.exception.model.NotFoundException;
import com.example.letscareer.schedule.domain.Schedule;
import com.example.letscareer.schedule.repository.ScheduleRepository;
import com.example.letscareer.stage.domain.Stage;
import com.example.letscareer.stage.domain.Status;
import com.example.letscareer.stage.dto.request.AddStageRequest;
import com.example.letscareer.stage.dto.response.AddStageResponse;
import com.example.letscareer.stage.repository.StageRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;

import static com.example.letscareer.common.exception.enums.ErrorCode.SCHEDULE_NOT_FOUND_EXCEPTION;

@Service
@RequiredArgsConstructor
public class StageService {

@Autowired
private final StageRepository stageRepository;
private final ScheduleRepository scheduleRepository;

@Transactional
public AddStageResponse addStage(Long userId, Long scheduleId, AddStageRequest request) {
Schedule schedule = scheduleRepository.findById(scheduleId)
.orElseThrow(() -> new NotFoundException(SCHEDULE_NOT_FOUND_EXCEPTION));

Stage stage = Stage.builder()
.schedule(schedule)
.type(request.type())
.midName(request.mid_name())
.date(request.date())
.status(Status.DO)
.order(schedule.getStages().size() + 1)
.build();

stageRepository.save(stage);

// D-day 계산
Integer dday = (request.date() != null) ? calculateDday(request.date()) : null;

return new AddStageResponse(
stage.getStageId(),
stage.getType().getValue(),
stage.getMidName(),
stage.getStatus().getValue(),
stage.getDate(),
dday);
}

private int calculateDday(LocalDate deadline) {
int dday = Period.between(LocalDate.now(), deadline).getDays();
return dday;
}

}

0 comments on commit 7256a52

Please sign in to comment.