Skip to content

Commit

Permalink
Feat: Added UserSelect JavaDoc And Junit test
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 12, 2026
1 parent c1c1b7f commit 8d26537
Show file tree
Hide file tree
Showing 2 changed files with 490 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,49 @@
import ntnu.systemutvikling.team6.models.user.*;
import ntnu.systemutvikling.team6.security.PasswordHasher;

/**
* Data access class responsible for reading user-related data from the database.
*
* <p>Provides methods to retrieve individual users (by credentials or UUID),
* all users, a user's settings, and a user's inbox. Queries use LEFT JOINs
* across the {@code User}, {@code Settings}, and {@code Messages} tables to
* assemble fully populated {@link User} objects in a single round trip where
* possible.</p>
*/

public class UserSelect {
/** The database connection used for all queries in this class. */
private final DatabaseConnection connection;

/**
* Constructs a new {@code UserSelect} with the given database connection.
*
* @param connection the {@link DatabaseConnection} to use for executing queries;
* must not be {@code null}
*/
public UserSelect(DatabaseConnection connection) {
this.connection = connection;
}

/**
* Retrieves a single {@link User} from the database matching the given username
* and password.
*
* <p>The password is hashed via {@link PasswordHasher} before being compared
* against the stored value. If a matching user is found, their {@link Settings}
* (when present) and {@link Inbox} (including any {@link Message} objects) are
* also populated. Returns {@code null} if no matching user is found.</p>
*
* <p><strong>Note:</strong> the current SQL query compares both parameters against
* {@code user_password}; the {@code user_name} column is not yet included in the
* WHERE clause, which may be a bug.</p>
*
* @param username the plain-text username to look up
* @param password the plain-text password; hashed internally before the query runs
* @return the matching {@link User} with settings and inbox populated,
* or {@code null} if no match is found
* @throws RuntimeException if a {@link SQLException} occurs while executing the query
*/
public User getUserFromDBUsernameAndPassword(String username, String password) {
PasswordHasher hasher = new PasswordHasher();
String hashedpassword = hasher.getHashPassword(password);
Expand Down Expand Up @@ -83,6 +119,18 @@ public User getUserFromDBUsernameAndPassword(String username, String password) {
return user;
}

/**
* Retrieves a single {@link User} from the database by their UUID.
*
* <p>The returned user is fully populated with {@link Settings} (when present)
* and an {@link Inbox} containing any associated {@link Message} objects.
* Returns {@code null} if no user with the given UUID exists.</p>
*
* @param user_id the UUID string of the user to retrieve; must not be {@code null}
* @return the matching {@link User} with settings and inbox populated,
* or {@code null} if no user is found
* @throws RuntimeException if a {@link SQLException} occurs while executing the query
*/
public User getUserFromDBUuid(String user_id) {
User user = null;
Connection conn = null;
Expand Down Expand Up @@ -146,6 +194,18 @@ public User getUserFromDBUuid(String user_id) {
return user;
}

/**
* Retrieves all users from the database, each fully populated with their
* {@link Settings} and {@link Inbox}.
*
* <p>The query LEFT JOINs {@code User}, {@code Settings}, and {@code Messages}.
* Multiple rows for the same user UUID (due to multiple messages) are collapsed
* into a single {@link User} object with all messages appended to its inbox.</p>
*
* @return a {@link UserRegistry} containing all users found in the database;
* never {@code null}, but may be empty if no users exist
* @throws RuntimeException if a {@link SQLException} occurs while executing the query
*/
public UserRegistry getUsersFromDB() {
UserRegistry registry = new UserRegistry();
Connection conn = null;
Expand Down Expand Up @@ -211,6 +271,17 @@ public UserRegistry getUsersFromDB() {
return registry;
}

/**
* Retrieves the {@link Settings} for a specific user by their UUID.
*
* <p>At most one row is fetched (via {@code setMaxRows(1)}). Returns {@code null}
* if no settings row exists for the given user.</p>
*
* @param user_id the UUID string of the user whose settings should be retrieved;
* must not be {@code null}
* @return the user's {@link Settings}, or {@code null} if none are found
* @throws RuntimeException if a {@link SQLException} occurs while executing the query
*/
public Settings getSettingsForUser(String user_id) {
Settings settings = null;
Connection conn = null;
Expand Down Expand Up @@ -243,6 +314,19 @@ public Settings getSettingsForUser(String user_id) {
return settings;
}

/**
* Retrieves the {@link Inbox} for a specific user by their UUID, populated with
* all of their {@link Message} objects.
*
* <p>Returns an empty {@link Inbox} (never {@code null}) if no messages exist
* for the given user.</p>
*
* @param user_id the UUID string of the user whose inbox should be retrieved;
* must not be {@code null}
* @return an {@link Inbox} containing all messages for the user;
* empty if no messages are found
* @throws RuntimeException if a {@link SQLException} occurs while executing the query
*/
public Inbox getInboxForUser(String user_id) {
Inbox inbox = new Inbox();
Connection conn = null;
Expand Down
Loading

0 comments on commit 8d26537

Please sign in to comment.