Add new Taskgroup (Backend)

This commit is contained in:
Sebastian 2023-09-25 11:15:56 +02:00
parent 2d32625d2c
commit 3adebd1670
8 changed files with 152 additions and 1 deletions

View File

@ -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<Taskgroup> 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"));
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -37,4 +37,6 @@ public class Taskgroup {
public void setTaskgroupName(String taskgroupName) { public void setTaskgroupName(String taskgroupName) {
this.taskgroupName = taskgroupName; this.taskgroupName = taskgroupName;
} }
} }

View File

@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface TaskgroupRepository extends CrudRepository<Taskgroup, Long> { public interface TaskgroupRepository extends CrudRepository<Taskgroup, Long> {
boolean existsByTaskgroupName(String name);
} }

View File

@ -0,0 +1,8 @@
package core.services;
public enum ServiceExitCode {
OK,
ENTITY_ALREADY_EXIST,
MISSING_ENTITY;
}

View File

@ -0,0 +1,37 @@
package core.services;
public class ServiceResult <T> {
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;
}
}

View File

@ -1,15 +1,28 @@
package core.services; 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 core.repositories.timemanager.TaskgroupRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.config.Task;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class TaskgroupService { public class TaskgroupService {
private final TaskgroupRepository taskgroupRepository; private final TaskgroupRepository taskgroupRepository;
public TaskgroupService(@Autowired TaskgroupRepository taskgroupRepository) { public TaskgroupService(@Autowired TaskgroupRepository taskgroupRepository) {
this.taskgroupRepository = taskgroupRepository; this.taskgroupRepository = taskgroupRepository;
} }
public ServiceResult<Taskgroup> 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);
}
}
} }