From 710b661b0db369a6436fe59ce1f5f62dd2582dbf Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Sat, 29 Jan 2022 09:55:06 +0100 Subject: [PATCH] Fixed user/del endpoint --- .../vpr/server/controller/UserController.java | 3 +++ .../implementation/UserDAOImplementation.java | 13 ++++++++++++ .../vpr/server/dao/interfaces/UserDAO.java | 2 ++ .../main/java/com/vpr/server/data/User.java | 20 ++++++++++++++----- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/vpr/server/controller/UserController.java b/server/src/main/java/com/vpr/server/controller/UserController.java index bc0857b..9c9e27b 100644 --- a/server/src/main/java/com/vpr/server/controller/UserController.java +++ b/server/src/main/java/com/vpr/server/controller/UserController.java @@ -144,6 +144,9 @@ public class UserController { if (user == null) { return new ResponseEntity<>("User nicht in der Datenbank vorhanden", HttpStatus.BAD_REQUEST); } + if(userDAO.deleteAllUserEvents(user.getId())){ + return new ResponseEntity<>("User konnte nicht gelöscht werden", HttpStatus.INTERNAL_SERVER_ERROR); + } userRepository.delete(user); return new ResponseEntity<>("", HttpStatus.OK); } diff --git a/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java b/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java index 3c2b857..c3c44da 100644 --- a/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java +++ b/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java @@ -21,4 +21,17 @@ public class UserDAOImplementation implements UserDAO { public List getAllUser() { return manager.createNamedQuery("getAllUser", User.class).getResultList(); } + + @Override + public boolean deleteAllUserEvents(long userId) { + try { + manager.createNamedQuery("deleteAllUserEvents", User.class) + .setParameter("userId", userId) + .executeUpdate(); + + return true; + }catch (Exception e){ + return false; + } + } } diff --git a/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java b/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java index b163e94..8cf8aa8 100644 --- a/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java +++ b/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java @@ -6,4 +6,6 @@ import java.util.List; public interface UserDAO { List getAllUser(); + + boolean deleteAllUserEvents(long userId); } diff --git a/server/src/main/java/com/vpr/server/data/User.java b/server/src/main/java/com/vpr/server/data/User.java index 01e0b84..b444d38 100644 --- a/server/src/main/java/com/vpr/server/data/User.java +++ b/server/src/main/java/com/vpr/server/data/User.java @@ -6,12 +6,22 @@ import java.util.List; // @Entity creates a table out of this class with Hibernate @Entity(name = "User") @Table(name = "user") + +@SqlResultSetMapping(name="deleteResult", columns = { + @ColumnResult(name = "count") +}) + @NamedNativeQueries({ - @NamedNativeQuery( - name = "getAllUser", - query = "SELECT * FROM user", - resultClass = User.class - ) + @NamedNativeQuery( + name = "getAllUser", + query = "SELECT * FROM user", + resultClass = User.class + ), + @NamedNativeQuery( + name = "deleteAllUserEvents", + query = "DELETE FROM user_event WHERE user_id = :userId", + resultSetMapping = "deleteResult" + ) }) public class User { // Generate the primary key