diff --git a/backend/src/main/java/core/api/controller/TaskgroupController.java b/backend/src/main/java/core/api/controller/TaskgroupController.java new file mode 100644 index 0000000..3d415f1 --- /dev/null +++ b/backend/src/main/java/core/api/controller/TaskgroupController.java @@ -0,0 +1,36 @@ +package core.api.controller; + +import core.api.models.auth.SimpleStatusResponse; +import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo; +import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo; +import core.entities.timemanager.Taskgroup; +import core.services.ServiceExitCode; +import core.services.ServiceResult; +import core.services.TaskgroupService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@CrossOrigin(origins = "*", maxAge = 3600) +@RestController +@RequestMapping("/api/settings") +public class TaskgroupController { + + private final TaskgroupService taskgroupService; + + public TaskgroupController(@Autowired TaskgroupService taskgroupService) { + this.taskgroupService = taskgroupService; + } + + @PutMapping("/taskgroups") + public ResponseEntity createTaskgroup(@Valid @RequestBody TaskgroupFieldInfo fieldInfo) { + ServiceResult creationResult = taskgroupService.addTaskgroup(fieldInfo); + if(creationResult.getExitCode() == ServiceExitCode.OK) { + return ResponseEntity.ok(new TaskgroupEntityInfo(creationResult.getResult())); + } else { + return ResponseEntity.status(409).body(new SimpleStatusResponse("failed")); + } + } +} diff --git a/backend/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupEntityInfo.java b/backend/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupEntityInfo.java new file mode 100644 index 0000000..99fd360 --- /dev/null +++ b/backend/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupEntityInfo.java @@ -0,0 +1,30 @@ +package core.api.models.timemanager.taskgroup; + +import core.entities.timemanager.Taskgroup; + +public class TaskgroupEntityInfo { + + private long taskgroupID; + private String taskgroupName; + + public TaskgroupEntityInfo(Taskgroup taskgroup) { + this.taskgroupID = taskgroup.getTaskgroupID(); + this.taskgroupName = taskgroup.getTaskgroupName(); + } + + public long getTaskgroupID() { + return taskgroupID; + } + + public void setTaskgroupID(long taskgroupID) { + this.taskgroupID = taskgroupID; + } + + public String getTaskgroupName() { + return taskgroupName; + } + + public void setTaskgroupName(String taskgroupName) { + this.taskgroupName = taskgroupName; + } +} diff --git a/backend/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupFieldInfo.java b/backend/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupFieldInfo.java new file mode 100644 index 0000000..0c3116d --- /dev/null +++ b/backend/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupFieldInfo.java @@ -0,0 +1,23 @@ +package core.api.models.timemanager.taskgroup; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.Max; +import javax.validation.constraints.NotBlank; + +public class TaskgroupFieldInfo { + + @JsonProperty + @NotBlank + @Length(max = 255) + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/backend/src/main/java/core/entities/timemanager/Taskgroup.java b/backend/src/main/java/core/entities/timemanager/Taskgroup.java index 1738a2c..8be5426 100644 --- a/backend/src/main/java/core/entities/timemanager/Taskgroup.java +++ b/backend/src/main/java/core/entities/timemanager/Taskgroup.java @@ -37,4 +37,6 @@ public class Taskgroup { public void setTaskgroupName(String taskgroupName) { this.taskgroupName = taskgroupName; } + + } diff --git a/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java b/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java index 0199806..edb84a3 100644 --- a/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java +++ b/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java @@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository; @Repository public interface TaskgroupRepository extends CrudRepository { + + boolean existsByTaskgroupName(String name); } diff --git a/backend/src/main/java/core/services/ServiceExitCode.java b/backend/src/main/java/core/services/ServiceExitCode.java new file mode 100644 index 0000000..6105f94 --- /dev/null +++ b/backend/src/main/java/core/services/ServiceExitCode.java @@ -0,0 +1,8 @@ +package core.services; + +public enum ServiceExitCode { + + OK, + ENTITY_ALREADY_EXIST, + MISSING_ENTITY; +} diff --git a/backend/src/main/java/core/services/ServiceResult.java b/backend/src/main/java/core/services/ServiceResult.java new file mode 100644 index 0000000..da9e03d --- /dev/null +++ b/backend/src/main/java/core/services/ServiceResult.java @@ -0,0 +1,37 @@ +package core.services; + +public class ServiceResult { + + private ServiceExitCode exitCode; + private T result; + + public ServiceResult(ServiceExitCode exitCode, T result) { + this.exitCode = exitCode; + this.result = result; + } + + public ServiceResult(T result) { + this.result = result; + this.exitCode = ServiceExitCode.OK; + } + + public ServiceResult(ServiceExitCode exitCode) { + this.exitCode = exitCode; + } + + public ServiceExitCode getExitCode() { + return exitCode; + } + + public void setExitCode(ServiceExitCode exitCode) { + this.exitCode = exitCode; + } + + public T getResult() { + return result; + } + + public void setResult(T result) { + this.result = result; + } +} diff --git a/backend/src/main/java/core/services/TaskgroupService.java b/backend/src/main/java/core/services/TaskgroupService.java index aa3f180..516a4d1 100644 --- a/backend/src/main/java/core/services/TaskgroupService.java +++ b/backend/src/main/java/core/services/TaskgroupService.java @@ -1,15 +1,28 @@ package core.services; +import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo; +import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo; +import core.entities.timemanager.Taskgroup; import core.repositories.timemanager.TaskgroupRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.config.Task; import org.springframework.stereotype.Service; @Service public class TaskgroupService { private final TaskgroupRepository taskgroupRepository; - public TaskgroupService(@Autowired TaskgroupRepository taskgroupRepository) { this.taskgroupRepository = taskgroupRepository; } + + public ServiceResult addTaskgroup(TaskgroupFieldInfo taskData) { + if(!taskgroupRepository.existsByTaskgroupName(taskData.getName())) { + Taskgroup taskgroup = new Taskgroup(taskData.getName()); + taskgroupRepository.save(taskgroup); + return new ServiceResult<>(taskgroup); + } else { + return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST); + } + } }