Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#49 [feat] 4.5 전형 단계 추가 #50

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -6,7 +6,7 @@
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.self_intro.dto.SaveSelfIntroRequest;
import com.example.letscareer.self_intro.dto.request.SaveSelfIntroRequest;
import com.example.letscareer.self_intro.service.SelfIntroService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand All @@ -17,7 +17,7 @@ public class SelfIntroController {

private final SelfIntroService selfIntroService;

@PostMapping("/schedules/{scheduleId}/stages/{stageId}/self-intro")
@PutMapping("/schedules/{scheduleId}/stages/{stageId}/self-intro")
public ApiResponse saveSelfIntro(
@RequestHeader("userId") Long userId,
@PathVariable Long scheduleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public class SelfIntro {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long selfIntroId;

private String title;

private int sequence;

@Lob
private String content;

@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "stageId")
@NotNull
private Stage stage;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.letscareer.self_intro.dto;

public record SelfIntroDTO(
String title,
int sequence,
String content) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.letscareer.self_intro.dto.request;

import com.example.letscareer.self_intro.dto.SelfIntroDTO;

import java.util.List;

public record SaveSelfIntroRequest(
List<SelfIntroDTO> selfIntros
) {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.letscareer.self_intro.repository;

import com.example.letscareer.self_intro.domain.SelfIntro;
import com.example.letscareer.stage.domain.Stage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SelfIntroRepository extends JpaRepository<SelfIntro, Long> {
void deleteByStage(Stage stage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.example.letscareer.schedule.domain.Schedule;
import com.example.letscareer.schedule.repository.ScheduleRepository;
import com.example.letscareer.self_intro.domain.SelfIntro;
import com.example.letscareer.self_intro.dto.SaveSelfIntroRequest;
import com.example.letscareer.self_intro.dto.SelfIntroDTO;
import com.example.letscareer.self_intro.dto.request.SaveSelfIntroRequest;
import com.example.letscareer.self_intro.repository.SelfIntroRepository;
import com.example.letscareer.stage.domain.Stage;
import com.example.letscareer.stage.repository.StageRepository;
Expand All @@ -14,7 +15,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
Expand All @@ -36,11 +36,18 @@ public void saveSelfIntro(Long userId, Long scheduleId, Long stageId, SaveSelfIn
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(USER_NOT_FOUND_EXCEPTION));

SelfIntro selfIntro = SelfIntro.builder()
.stage(stage)
.content(request.content())
.build();
// 현재 stage에 있는 모든 SelfIntro를 삭제한다.
selfIntroRepository.deleteByStage(stage);

selfIntroRepository.save(selfIntro);
// 새로 들어온 자기소개서 항목을 저장한다.
for (SelfIntroDTO selfIntroDTO : request.selfIntros()) {
SelfIntro selfIntro = SelfIntro.builder()
.title(selfIntroDTO.title())
.sequence(selfIntroDTO.sequence())
.content(selfIntroDTO.content())
.stage(stage)
.build();
selfIntroRepository.save(selfIntro);
}
}
}
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;
}

}
Loading