From 712f7937365c2563a7904777f3ae55afaeafabd9 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 25 Sep 2023 11:21:09 +0200 Subject: [PATCH] Include Owner of Taskgroup in Datamodel --- .../api/controller/TaskgroupController.java | 4 +++- .../core/entities/timemanager/Taskgroup.java | 15 ++++++++++++- .../java/core/services/TaskgroupService.java | 21 ++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/core/api/controller/TaskgroupController.java b/backend/src/main/java/core/api/controller/TaskgroupController.java index 3d415f1..856edda 100644 --- a/backend/src/main/java/core/api/controller/TaskgroupController.java +++ b/backend/src/main/java/core/api/controller/TaskgroupController.java @@ -9,6 +9,7 @@ import core.services.ServiceResult; import core.services.TaskgroupService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @@ -26,7 +27,8 @@ public class TaskgroupController { @PutMapping("/taskgroups") public ResponseEntity createTaskgroup(@Valid @RequestBody TaskgroupFieldInfo fieldInfo) { - ServiceResult creationResult = taskgroupService.addTaskgroup(fieldInfo); + String username = SecurityContextHolder.getContext().getAuthentication().getName(); + ServiceResult creationResult = taskgroupService.addTaskgroup(fieldInfo, username); if(creationResult.getExitCode() == ServiceExitCode.OK) { return ResponseEntity.ok(new TaskgroupEntityInfo(creationResult.getResult())); } else { diff --git a/backend/src/main/java/core/entities/timemanager/Taskgroup.java b/backend/src/main/java/core/entities/timemanager/Taskgroup.java index 8be5426..4043f4d 100644 --- a/backend/src/main/java/core/entities/timemanager/Taskgroup.java +++ b/backend/src/main/java/core/entities/timemanager/Taskgroup.java @@ -1,5 +1,7 @@ package core.entities.timemanager; +import core.entities.User; + import javax.persistence.*; import javax.validation.constraints.NotBlank; @@ -15,8 +17,13 @@ public class Taskgroup { @Column(name = "name", length = 255) private String taskgroupName; - public Taskgroup(String taskgroupName) { + @OneToOne + @JoinColumn(name = "taskgroupuser", nullable = false) + private User user; + + public Taskgroup(String taskgroupName, User user) { this.taskgroupName = taskgroupName; + this.user = user; } public Taskgroup() { @@ -38,5 +45,11 @@ public class Taskgroup { this.taskgroupName = taskgroupName; } + public User getUser() { + return user; + } + public void setUser(User user) { + this.user = user; + } } diff --git a/backend/src/main/java/core/services/TaskgroupService.java b/backend/src/main/java/core/services/TaskgroupService.java index 516a4d1..2eae025 100644 --- a/backend/src/main/java/core/services/TaskgroupService.java +++ b/backend/src/main/java/core/services/TaskgroupService.java @@ -2,23 +2,38 @@ package core.services; import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo; import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo; +import core.entities.User; +import core.entities.UserRole; import core.entities.timemanager.Taskgroup; +import core.repositories.UserRepository; import core.repositories.timemanager.TaskgroupRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.config.Task; import org.springframework.stereotype.Service; +import java.util.NoSuchElementException; +import java.util.Optional; + @Service public class TaskgroupService { private final TaskgroupRepository taskgroupRepository; - public TaskgroupService(@Autowired TaskgroupRepository taskgroupRepository) { + private final UserRepository userRepository; + public TaskgroupService(@Autowired TaskgroupRepository taskgroupRepository, + @Autowired UserRepository userRepository) { this.taskgroupRepository = taskgroupRepository; + this.userRepository = userRepository; } - public ServiceResult addTaskgroup(TaskgroupFieldInfo taskData) { + public ServiceResult addTaskgroup(TaskgroupFieldInfo taskData, String username) { + Optional user = userRepository.findByUsername(username); + if(user.isEmpty()) { + throw new NoSuchElementException(); + } + if(!taskgroupRepository.existsByTaskgroupName(taskData.getName())) { - Taskgroup taskgroup = new Taskgroup(taskData.getName()); + + Taskgroup taskgroup = new Taskgroup(taskData.getName(), user.get()); taskgroupRepository.save(taskgroup); return new ServiceResult<>(taskgroup); } else {