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

[Feat] #30 - Task POST API 개발 #31

Merged
merged 6 commits into from
Jan 6, 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 @@ -5,8 +5,8 @@
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;
import org.moonshot.server.domain.task.dto.request.TaskCreateRequestDto;
import org.hibernate.validator.constraints.Range;
import org.moonshot.server.domain.objective.dto.request.TaskCreateRequestDto;
import org.moonshot.server.global.common.model.validator.ValidTargetNumber;
import org.springframework.format.annotation.DateTimeFormat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.moonshot.server.global.common.response.ErrorType;

public class KeyResultInvalidPositionException extends MoonshotException {

public KeyResultInvalidPositionException() {
super(ErrorType.INVALID_KEY_RESULT_ORDER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.moonshot.server.global.common.response.ErrorType;

public class KeyResultNumberExceededException extends MoonshotException {

public KeyResultNumberExceededException() {
super(ErrorType.KEY_RESULT_NUMBER_EXCEEDED);
super(ErrorType.ACTIVE_KEY_RESULT_NUMBER_EXCEEDED);
}
Comment on lines 8 to 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4;
캬캬~ 위아래 한칸이요!


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public interface KeyResultRepository extends JpaRepository<KeyResult, Long> {
@Query("select kr from KeyResult kr join fetch kr.objective where kr.id = :keyResultId")
Optional<KeyResult> findKeyResultAndObjective(@Param("keyResultId") Long keyResultId);

@Query("select kr from KeyResult kr join fetch kr.objective where kr.id = :keyResultId")
Optional<KeyResult> findKeyResultAndObjective(@Param("keyResultId") Long keyResultId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;
import org.moonshot.server.domain.task.dto.request.TaskCreateRequestDto;
import org.moonshot.server.global.common.model.validator.ValidTargetNumber;
import org.springframework.format.annotation.DateTimeFormat;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.moonshot.server.domain.task.controller;

import static org.moonshot.server.global.common.response.SuccessType.*;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.moonshot.server.domain.task.dto.request.TaskSingleCreateRequestDto;
import org.moonshot.server.domain.task.service.TaskService;
import org.moonshot.server.global.common.response.ApiResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/task")
public class TaskController {

private final TaskService taskService;

@PostMapping
public ApiResponse<?> createTask(@RequestBody @Valid TaskSingleCreateRequestDto request) {
taskService.createTask(request);
return ApiResponse.success(POST_TASK_SUCCESS);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.moonshot.server.domain.objective.dto.request;
package org.moonshot.server.domain.task.dto.request;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.moonshot.server.domain.task.dto.request;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.hibernate.validator.constraints.Range;

public record TaskSingleCreateRequestDto(
@NotNull(message = "KR이 생성될 KeyResult Id를 입력해주세요.")
Long keyResultId,
@Size(min = 1, max = 30, message = "Task는 30자 이하여야 합니다.")
String title,
@NotNull(message = "KR 순서를 입력해주세요")
@Range(min = 0, max = 2, message = "KeyResult의 순서는 0부터 2까지로 설정할 수 있습니다.")
Short idx
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.moonshot.server.domain.task.exception;

import org.moonshot.server.global.common.exception.MoonshotException;
import org.moonshot.server.global.common.response.ErrorType;

public class TaskInvalidPositionException extends MoonshotException {

public TaskInvalidPositionException() {
super(ErrorType.INVALID_TASK_ORDER);
}
Comment on lines +8 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4;
여기두요 !


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.moonshot.server.domain.task.exception;

import org.moonshot.server.global.common.exception.MoonshotException;
import org.moonshot.server.global.common.response.ErrorType;

public class TaskNumberExceededException extends MoonshotException {

public TaskNumberExceededException() {
super(ErrorType.ACTIVE_TASK_NUMBER_EXCEEDED);
}
Comment on lines +8 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4;
또 찾아버렸네요..


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
public interface TaskRepository extends JpaRepository<Task, Long> {

List<Task> findAllByKeyResult(KeyResult keyResult);
List<Task> findAllByKeyResultOrderByIdx(KeyResult keyResult);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.moonshot.server.domain.task.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.moonshot.server.domain.keyresult.exception.KeyResultInvalidPositionException;
import org.moonshot.server.domain.keyresult.model.KeyResult;
import org.moonshot.server.domain.keyresult.repository.KeyResultRepository;
import org.moonshot.server.domain.task.dto.request.TaskSingleCreateRequestDto;
import org.moonshot.server.domain.task.exception.TaskNumberExceededException;
import org.moonshot.server.domain.task.model.Task;
import org.moonshot.server.domain.task.repository.TaskRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class TaskService {

private static final int ACTIVE_TASK_NUMBER = 3;

private final KeyResultRepository keyResultRepository;
private final TaskRepository taskRepository;

@Transactional
public void createTask(TaskSingleCreateRequestDto request) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(request.keyResultId())
.orElseThrow();
List<Task> taskList = taskRepository.findAllByKeyResultOrderByIdx(keyResult);

if (taskList.size() >= ACTIVE_TASK_NUMBER) {
throw new TaskNumberExceededException();
}
if (request.idx() > taskList.size()) {
throw new KeyResultInvalidPositionException();
}

for (short i = request.idx(); i < taskList.size(); i++) {
taskList.get(i).incrementIdx();
}
taskRepository.save(Task.builder()
.title(request.title())
.idx(request.idx())
.keyResult(keyResult)
.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public enum ErrorType {
INVALID_HTTP_METHOD(HttpStatus.BAD_REQUEST, "지원되지 않는 HTTP method 요청입니다."),
INVALID_IMAGE_EXTENSION(HttpStatus.BAD_REQUEST, "지원하지 않는 이미지 확장자입니다."),
ACTIVE_OBJECTIVE_NUMBER_EXCEEDED(HttpStatus.BAD_REQUEST, "허용된 Objective 개수를 초과하였습니다"),
KEY_RESULT_NUMBER_EXCEEDED(HttpStatus.BAD_REQUEST, "허용된 Objective 개수를 초과하였습니다"),
ACTIVE_KEY_RESULT_NUMBER_EXCEEDED(HttpStatus.BAD_REQUEST, "허용된 Key Result 개수를 초과하였습니다"),
ACTIVE_TASK_NUMBER_EXCEEDED(HttpStatus.BAD_REQUEST, "허용된 Task 개수를 초과하였습니다."),
INVALID_KEY_RESULT_ORDER(HttpStatus.BAD_REQUEST, "정상적이지 않은 KeyResult 위치입니다."),
INVALID_TASK_ORDER(HttpStatus.BAD_REQUEST, "정상적이지 않은 Task 위치입니다."),

/**
* 404 NOT FOUND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum SuccessType {
POST_NOTIFY_IMAGE_SAVE_SUCCESS(HttpStatus.CREATED, "Presigned Url을 통해 이미지 생성을 성공하였습니다."),
POST_OKR_SUCCESS(HttpStatus.CREATED, "O-KR을 생성을 성공하였습니다."),
POST_KEY_RESULT_SUCCESS(HttpStatus.CREATED, "KeyResult 생성을 성공하였습니다."),
POST_TASK_SUCCESS(HttpStatus.CREATED, "Task 생성을 성공하였습니다."),

/**
* 204 NO CONTENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SecurityConfig {
"/actuator/health",
"/v1/image",
"/v1/objective",
"/v1/task",
"/v1/key-result",
"/error",
"/swagger-ui/**",
Expand Down
Loading