#8: Setup Model and DAO
All checks were successful
Quality Check / Linting Check (push) Successful in 12s
Quality Check / Javadoc Check (push) Successful in 21s

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-19 23:15:43 +02:00 committed by Dominik Säume
parent ed6a19b6af
commit 0edcf7fb84
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
5 changed files with 157 additions and 5 deletions

View file

@ -0,0 +1,34 @@
package de.hitec.nhplus.login;
import de.hitec.nhplus.nurse.Nurse;
public class User {
private int id;
private String username;
private int permissions = 0;
private Nurse nurse;
public User(
int id,
String username,
int permissions,
Nurse nurse
) {
this.id = id;
this.username = username;
this.permissions = permissions;
this.nurse = nurse;
}
public User(
String username,
int permissions,
Nurse nurse,
boolean admin
) {
this.username = username;
this.permissions = permissions;
this.nurse = nurse;
}
}

View file

@ -0,0 +1,118 @@
package de.hitec.nhplus.login.database;
import de.hitec.nhplus.datastorage.Dao;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.login.User;
import de.hitec.nhplus.nurse.Nurse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDao implements Dao<User> {
protected final Connection connection;
public UserDao(Connection connection) {
this.connection = connection;
}
public int readUserId(String username) throws SQLException {
final String SQL = "SELECT id FROM user WHERE username = ?";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setString(1, username);
return statement.executeQuery().getInt(1);
}
public byte[] readPasswordSalt(int id) throws SQLException {
final String SQL = "SELECT passwordSalt FROM user WHERE id = ?";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setInt(1, id);
return statement.executeQuery().getBytes(1);
}
public byte[] readPasswordHash(int id) throws SQLException {
final String SQL = "SELECT passwordHash FROM user WHERE id = ?";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setInt(1, id);
return statement.executeQuery().getBytes(1);
}
// User
@Override
public User read(int id) throws SQLException {
final String SQL = """
SELECT user.username, user__permissions.permissions, user__nurse.nurseId
FROM user
LEFT JOIN user__permissions ON user.id = user__permissions.userId
LEFT JOIN user__nurse ON user.id = user__nurse.userId
WHERE user.id = ?;
""";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
int nurseId = result.getInt(3);
Nurse nurse = null;
if (!result.wasNull()) {
nurse = DaoFactory.getInstance().createNurseDAO().read(nurseId);
}
return new User(
id,
result.getString(1),
result.getInt(2),
nurse
);
}
// Administration
@Override
public void create(User user) throws SQLException {
}
@Override
public void update(User user) throws SQLException {
}
@Override
public void delete(int id) throws SQLException {
final String SQL = """
DELETE FROM user WHERE user.id = ?;
""";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
}
@Override
public List<User> readAll() throws SQLException {
final String SQL = """
SELECT user.id, user.username, user__permissions.permissions, user__nurse.nurseId
FROM user
LEFT JOIN user__permissions ON user.id = user__permissions.userId
LEFT JOIN user__nurse ON user.id = user__nurse.userId
""";
ResultSet result = connection.prepareStatement(SQL).executeQuery();
List<User> users = new ArrayList<>();
while (result.next()) {
int nurseId = result.getInt(4);
Nurse nurse = null;
if (!result.wasNull()) {
nurse = DaoFactory.getInstance().createNurseDAO().read(nurseId);
}
users.add(new User(
result.getInt(1),
result.getString(2),
result.getInt(3),
nurse
));
}
return users;
}
}

View file

@ -1,7 +1,7 @@
CREATE TABLE user CREATE TABLE user
( (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL, username TEXT NOT NULL UNIQUE,
passwordSalt BLOB NOT NULL, passwordSalt BLOB NOT NULL,
passwordHash BLOB NOT NULL passwordHash BLOB NOT NULL
) )

View file

@ -1,6 +1,6 @@
CREATE TABLE user__permissions CREATE TABLE user__permissions
( (
userId INTEGER NOT NULL, userId INTEGER NOT NULL UNIQUE,
permissions INTEGER, -- Binary Bitmask for Permissions permissions INTEGER NOT NULL, -- Binary Bitmask for Permissions
FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE
) )

View file

@ -1,7 +1,7 @@
CREATE TABLE user__nurse CREATE TABLE user__nurse
( (
userId INTEGER NOT NULL, userId INTEGER NOT NULL UNIQUE,
nurseId INTEGER NOT NULL, nurseId INTEGER NOT NULL UNIQUE,
FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE, FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE,
FOREIGN KEY (nurseId) REFERENCES nurse (id) ON DELETE CASCADE FOREIGN KEY (nurseId) REFERENCES nurse (id) ON DELETE CASCADE
) )