Fixed user/del endpoint

This commit is contained in:
Marc Beyer 2022-01-29 09:55:06 +01:00
parent f22bdf3040
commit 710b661b0d
4 changed files with 33 additions and 5 deletions

View File

@ -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);
}

View File

@ -21,4 +21,17 @@ public class UserDAOImplementation implements UserDAO {
public List<User> 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;
}
}
}

View File

@ -6,4 +6,6 @@ import java.util.List;
public interface UserDAO {
List<User> getAllUser();
boolean deleteAllUserEvents(long userId);
}

View File

@ -6,11 +6,21 @@ 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 = "deleteAllUserEvents",
query = "DELETE FROM user_event WHERE user_id = :userId",
resultSetMapping = "deleteResult"
)
})
public class User {