issue-6 #8

Merged
sebastian merged 12 commits from issue-6 into master 2023-09-25 13:18:06 +02:00
4 changed files with 85 additions and 2 deletions
Showing only changes of commit 99ae430d2b - Show all commits

View File

@ -4,6 +4,7 @@ import core.api.models.auth.SimpleStatusResponse;
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.timemanager.Taskgroup; import core.entities.timemanager.Taskgroup;
import core.services.PermissionResult;
import core.services.ServiceExitCode; import core.services.ServiceExitCode;
import core.services.ServiceResult; import core.services.ServiceResult;
import core.services.TaskgroupService; import core.services.TaskgroupService;
@ -35,4 +36,23 @@ public class TaskgroupController {
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed")); return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
} }
} }
@PostMapping("/taskgroups/{taskgroupID}")
public ResponseEntity<?> editTaskgroup(@PathVariable long taskgroupID, @Valid @RequestBody TaskgroupFieldInfo taskgroupFieldInfo) {
PermissionResult<Taskgroup> taskgroupPermissionResult = taskgroupService.getTaskgroupByIDAndUsername(taskgroupID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!taskgroupPermissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(taskgroupPermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
ServiceExitCode serviceExitCode = taskgroupService.editTaskgroup(taskgroupPermissionResult.getResult(), taskgroupFieldInfo);
if(serviceExitCode == ServiceExitCode.OK) {
return ResponseEntity.ok(new SimpleStatusResponse("success"));
} else {
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
}
}
} }

View File

@ -1,5 +1,6 @@
package core.repositories.timemanager; package core.repositories.timemanager;
import core.entities.User;
import core.entities.timemanager.Taskgroup; import core.entities.timemanager.Taskgroup;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -7,5 +8,5 @@ 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); boolean existsByTaskgroupNameAndUser(String name, User user);
} }

View File

@ -0,0 +1,40 @@
package core.services;
public class PermissionResult <T> extends ServiceResult<T> {
private boolean hasPermissions;
public PermissionResult(ServiceExitCode exitCode, T result) {
super(exitCode, result);
}
public PermissionResult(T result) {
super(result);
}
public PermissionResult(ServiceExitCode exitCode) {
super(exitCode);
}
public PermissionResult(ServiceExitCode exitCode, T result, boolean hasPermissions) {
super(exitCode, result);
this.hasPermissions = hasPermissions;
}
public PermissionResult(T result, boolean hasPermissions) {
super(result);
this.hasPermissions = hasPermissions;
}
public PermissionResult(ServiceExitCode exitCode, boolean hasPermissions) {
super(exitCode);
this.hasPermissions = hasPermissions;
}
public boolean isHasPermissions() {
return hasPermissions;
}
public void setHasPermissions(boolean hasPermissions) {
this.hasPermissions = hasPermissions;
}
}

View File

@ -9,6 +9,7 @@ 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.security.core.parameters.P;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -25,13 +26,19 @@ public class TaskgroupService {
this.userRepository = userRepository; this.userRepository = userRepository;
} }
public PermissionResult<Taskgroup> getTaskgroupByIDAndUsername(long taskgroupID, String username) {
Optional<Taskgroup> taskgroup = taskgroupRepository.findById(taskgroupID);
return taskgroup.map(value -> new PermissionResult<>(value, value.getUser().getUsername().equals(username))).orElseGet(() ->
new PermissionResult<>(ServiceExitCode.MISSING_ENTITY));
}
public ServiceResult<Taskgroup> addTaskgroup(TaskgroupFieldInfo taskData, String username) { public ServiceResult<Taskgroup> addTaskgroup(TaskgroupFieldInfo taskData, String username) {
Optional<User> user = userRepository.findByUsername(username); Optional<User> user = userRepository.findByUsername(username);
if(user.isEmpty()) { if(user.isEmpty()) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
if(!taskgroupRepository.existsByTaskgroupName(taskData.getName())) { if(!taskgroupRepository.existsByTaskgroupNameAndUser(taskData.getName(), user.get())) {
Taskgroup taskgroup = new Taskgroup(taskData.getName(), user.get()); Taskgroup taskgroup = new Taskgroup(taskData.getName(), user.get());
taskgroupRepository.save(taskgroup); taskgroupRepository.save(taskgroup);
@ -40,4 +47,19 @@ public class TaskgroupService {
return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST); return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST);
} }
} }
public ServiceExitCode editTaskgroup(Taskgroup taskgroup, TaskgroupFieldInfo taskgroupFieldInfo) {
if(!taskgroup.getTaskgroupName().equals(taskgroupFieldInfo.getName())) {
//Check if another taskgroup with the new name is already existent for the user of the taskgroup
if(!taskgroupRepository.existsByTaskgroupNameAndUser(taskgroupFieldInfo.getName(), taskgroup.getUser())) {
taskgroup.setTaskgroupName(taskgroupFieldInfo.getName());
taskgroupRepository.save(taskgroup);
return ServiceExitCode.OK;
} else {
return ServiceExitCode.ENTITY_ALREADY_EXIST;
}
} else {
return ServiceExitCode.OK;
}
}
} }