issue-6 #8
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -37,4 +37,6 @@ public class Taskgroup {
|
||||
public void setTaskgroupName(String taskgroupName) {
|
||||
this.taskgroupName = taskgroupName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface TaskgroupRepository extends CrudRepository<Taskgroup, Long> {
|
||||
|
||||
boolean existsByTaskgroupName(String name);
|
||||
}
|
||||
|
8
backend/src/main/java/core/services/ServiceExitCode.java
Normal file
8
backend/src/main/java/core/services/ServiceExitCode.java
Normal file
@ -0,0 +1,8 @@
|
||||
package core.services;
|
||||
|
||||
public enum ServiceExitCode {
|
||||
|
||||
OK,
|
||||
ENTITY_ALREADY_EXIST,
|
||||
MISSING_ENTITY;
|
||||
}
|
37
backend/src/main/java/core/services/ServiceResult.java
Normal file
37
backend/src/main/java/core/services/ServiceResult.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user