issue-6 #8

Merged
sebastian merged 12 commits from issue-6 into master 2023-09-25 13:18:06 +02:00
3 changed files with 35 additions and 5 deletions
Showing only changes of commit 712f793736 - Show all commits

View File

@ -9,6 +9,7 @@ import core.services.ServiceResult;
import core.services.TaskgroupService; import core.services.TaskgroupService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
@ -26,7 +27,8 @@ public class TaskgroupController {
@PutMapping("/taskgroups") @PutMapping("/taskgroups")
public ResponseEntity<?> createTaskgroup(@Valid @RequestBody TaskgroupFieldInfo fieldInfo) { public ResponseEntity<?> createTaskgroup(@Valid @RequestBody TaskgroupFieldInfo fieldInfo) {
ServiceResult<Taskgroup> creationResult = taskgroupService.addTaskgroup(fieldInfo); String username = SecurityContextHolder.getContext().getAuthentication().getName();
ServiceResult<Taskgroup> creationResult = taskgroupService.addTaskgroup(fieldInfo, username);
if(creationResult.getExitCode() == ServiceExitCode.OK) { if(creationResult.getExitCode() == ServiceExitCode.OK) {
return ResponseEntity.ok(new TaskgroupEntityInfo(creationResult.getResult())); return ResponseEntity.ok(new TaskgroupEntityInfo(creationResult.getResult()));
} else { } else {

View File

@ -1,5 +1,7 @@
package core.entities.timemanager; package core.entities.timemanager;
import core.entities.User;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
@ -15,8 +17,13 @@ public class Taskgroup {
@Column(name = "name", length = 255) @Column(name = "name", length = 255)
private String taskgroupName; 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.taskgroupName = taskgroupName;
this.user = user;
} }
public Taskgroup() { public Taskgroup() {
@ -38,5 +45,11 @@ public class Taskgroup {
this.taskgroupName = taskgroupName; this.taskgroupName = taskgroupName;
} }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
} }

View File

@ -2,23 +2,38 @@ package core.services;
import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo; import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo;
import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo; import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo;
import core.entities.User;
import core.entities.UserRole;
import core.entities.timemanager.Taskgroup; import core.entities.timemanager.Taskgroup;
import core.repositories.UserRepository;
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.scheduling.config.Task;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.NoSuchElementException;
import java.util.Optional;
@Service @Service
public class TaskgroupService { public class TaskgroupService {
private final TaskgroupRepository taskgroupRepository; 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.taskgroupRepository = taskgroupRepository;
this.userRepository = userRepository;
}
public ServiceResult<Taskgroup> addTaskgroup(TaskgroupFieldInfo taskData, String username) {
Optional<User> user = userRepository.findByUsername(username);
if(user.isEmpty()) {
throw new NoSuchElementException();
} }
public ServiceResult<Taskgroup> addTaskgroup(TaskgroupFieldInfo taskData) {
if(!taskgroupRepository.existsByTaskgroupName(taskData.getName())) { if(!taskgroupRepository.existsByTaskgroupName(taskData.getName())) {
Taskgroup taskgroup = new Taskgroup(taskData.getName());
Taskgroup taskgroup = new Taskgroup(taskData.getName(), user.get());
taskgroupRepository.save(taskgroup); taskgroupRepository.save(taskgroup);
return new ServiceResult<>(taskgroup); return new ServiceResult<>(taskgroup);
} else { } else {