Delete Taskgroups when deleting corresponding user

This commit is contained in:
Sebastian 2023-09-25 11:49:28 +02:00
parent e1b44cb92c
commit 9a67dfb875
3 changed files with 13 additions and 0 deletions

View File

@ -2,10 +2,12 @@ package core.repositories.timemanager;
import core.entities.User;
import core.entities.timemanager.Taskgroup;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.List;
@Repository
@ -15,4 +17,8 @@ public interface TaskgroupRepository extends CrudRepository<Taskgroup, Long> {
@Query("SELECT tg FROM Taskgroup tg WHERE tg.user.username = ?1")
List<Taskgroup> findAllByUser(String username);
@Modifying
@Transactional
void deleteAllByUser(User user);
}

View File

@ -71,4 +71,8 @@ public class TaskgroupService {
public List<Taskgroup> getTaskgroupsByUser(String username) {
return taskgroupRepository.findAllByUser(username);
}
public void deleteTaskgroupByUser(User user) {
taskgroupRepository.deleteAllByUser(user);
}
}

View File

@ -9,6 +9,7 @@ import core.api.models.users.UserUpdateInfo;
import core.entities.RoleEntity;
import core.entities.User;
import core.entities.UserRole;
import core.entities.timemanager.Taskgroup;
import core.repositories.RoleRepository;
import core.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,6 +30,7 @@ public class UserService {
@Autowired
private RoleRepository roleRepository;
@Autowired private TaskgroupService taskgroupService;
public List<UserInfo> getAllUserInfos() {
Iterable<User> users = userRepository.findAll();
@ -146,6 +148,7 @@ public class UserService {
return 2;
}
taskgroupService.deleteTaskgroupByUser(requestedUser.get());
userRepository.deleteByUsername(username);
return 0;
}