-
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.
- Loading branch information
Showing
6 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/letscareer/stage/controller/StageController.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,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()); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/letscareer/stage/dto/request/AddStageRequest.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,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 | ||
) { | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/letscareer/stage/dto/response/AddStageResponse.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,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 | ||
) { | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/letscareer/stage/service/StageService.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 |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |