#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

This commit is contained in:
Dominik Säume 2024-05-19 23:15:43 +02:00
parent ed6a19b6af
commit 1f309f2ac4
Signed by: SZUT-Dominik
GPG key ID: DACB4B96EB59ABA8
4 changed files with 126 additions and 2 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,90 @@
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.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 {
}
@Override
public List<User> readAll() throws SQLException {
return null;
}
}

View file

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

View file

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