Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/application/security/PasswordHasher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package application.security;

/**
* Interface that creates a "contract" for which methods the class that implements it, needs.
* Interface is created for SHA256PasswordHasher.
*/
public interface PasswordHasher {

String hash(String password);
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/application/user/UserRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,40 @@
import domain.user.User;
import persistence.UserRepository;

/**
* The UserRegister class is responsible for handling the user registration process.
* It ensures that the provided user details meet the necessary criteria and that
* the username and email are not already in use. It also hashes the user's password
* before saving the user information into the repository.
*/
public class UserRegister {

private final PasswordHasher hasher;
private final UserRepository repo;

public UserRegister(PasswordHasher hasher, UserRepository repo) {
/**
* Constructs a {@code UserRegister} instance responsible for handling the user registration process.
* It ensures user details meet validation criteria, such as uniqueness of username and email,
* and hashes passwords before saving user information into the repository.
*
* @param hasher a {@code PasswordHasher} implementation used to hash user passwords.
* @param repo a {@code UserRepository} implementation responsible for storing user data.
*/
public UserRegister(PasswordHasher hasher, UserRepository repo) {
this.hasher = hasher;
this.repo = repo;
}

/**
* Executes a user registration.
* Validation checks, checks if username or e-mail already exists.
* Hashes inputted password using PasswordHasher, that uses SHA256PasswordHasher.
* Creates user object, and inserts it into UserRepository using insert method.
* @param username
* @param phoneNumber
* @param password
* @param email
*/
public void execute(String username, String phoneNumber, String password, String email) {
username = username.trim();
email = email.trim().toLowerCase();
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/application/user/UserSignIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,37 @@
import domain.user.User;
import persistence.UserRepository;

/**
* The UserSignIn class handles the process of user authentication.
* It verifies the input credentials (login and password) against the stored user data.
* The password is validated through hashing and comparison with the stored hash.
*/
public class UserSignIn {

private final PasswordHasher hasher;
private final UserRepository repo;

public UserSignIn(PasswordHasher hasher, UserRepository repo) {
/**
* Constructs a {@code UserSignIn} instance responsible for user authentication.
* It handles the process of verifying user credentials by comparing the provided
* input with stored data in the repository and validating the password through hashing.
*
* @param hasher a {@code PasswordHasher} implementation used to hash and validate user passwords.
* @param repo a {@code UserRepository} implementation that stores user authentication data.
*/
public UserSignIn(PasswordHasher hasher, UserRepository repo) {
this.hasher = hasher;
this.repo = repo;
}

/**
* Execute methods, that takes in inputted login and password.
* Checks via UserRepository if login exists in database.
* Compares hashed input password with hashed password in database.
* @param login inputted login by user
* @param password inputted password by user
* @return user object, if it exists.
*/
public User execute(String login, String password) {
// normalize (trim etc.)
login.trim().toLowerCase();
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/application/user/UserStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
/**
* Provides statistical information about a user's donation activity.
* Acts as a service layer between the application and {@link DonationDao},
* exposing user-friendly methods for retrieving donation history,
* total amounts, and favorite organizations.
* showing relevant userstatistics to the user.
*/
public class UserStatistics {
private DonationDao donationDao;
Expand All @@ -24,9 +23,9 @@ public UserStatistics() {
/**
* Returns the top three organizations that the user has donated to most frequently.
*
* @param user the user whose favorite organizations are to be retrieved; must not be null
* @param user the user whose favorite organizations are going to be returned.
* @return a list of up to three formatted strings, each representing an organization
* and its donation count; empty list if the user has no donations
* and its donation count. Returns empty list if the user has no donations.
*/
public List<String> userFavoriteOrganization(User user) {
List<String> favoriteOrganization = donationDao.getFavoriteOrganization(user.getID());
Expand All @@ -37,8 +36,8 @@ public List<String> userFavoriteOrganization(User user) {
* Returns a formatted list of all donations made by the user.
* Each entry contains the donation amount, date, and organization.
*
* @param user the user whose donation history is to be retrieved; must not be null
* @return a list of formatted donation strings; empty list if the user has no donations
* @param user the user whose donation history is returned.
* @return a list of formatted donation strings. Returns empty list if the user has no donations
*/
public List<String> userDonations(User user) {
List<String> userDonationList = donationDao.getUserDonations(user.getID());
Expand All @@ -48,8 +47,8 @@ public List<String> userDonations(User user) {
/**
* Returns the total amount donated by the user across all donations.
*
* @param user the user whose total donation amount is to be calculated; must not be null
* @return a string representation of the total donated amount; {@code "0"} if no donations exist
* @param user the user whose total donation amount is returned.
* @return a string representation of the total donated amount. Returns "0" if no donations exist
*/
public String userTotalDonationAmount(User user) {
String totalDonations = donationDao.getTotalDonationAmount(user.getID());
Expand All @@ -59,8 +58,8 @@ public String userTotalDonationAmount(User user) {
/**
* Returns the total number of donations made by the user.
*
* @param user the user whose donation count is to be retrieved; must not be null
* @return a string representation of the total number of donations; {@code "0"} if none exist
* @param user the user whose donation count is returned.
* @return a string representation of the total number of donations. Returns "0" if none exist.
*/
public String getTotalDonationsMade(User user) {
String totalDonationsMade = donationDao.getTotalDonationsMade(user.getID());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/domain/donation/Cause.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package domain.donation;

/**
* Enum class that contains 5 different cause types
*/
public enum Cause { HEALTH, EMERGENCY_RELIEF, CHILDREN, ENVIRONMENT, CONFLICTS; }
45 changes: 45 additions & 0 deletions src/main/java/domain/donation/Donation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import java.time.LocalDateTime;
import java.util.Objects;

/**
* Donation object that defines what a Donation is.
* Provides set and get methods.
*/
public class Donation {
private Long id; //get set to null by default,
// Gets set to a value by database when registered?
Expand All @@ -15,6 +19,13 @@ public class Donation {
private final User user;
private final Organization organization;

/**
* Donation takes in parameters that is needed to define what a donation is.
* Requires both user and organization to be not null.
* @param amount
* @param user
* @param organization
*/
public Donation(BigDecimal amount, User user, Organization organization) {
Objects.requireNonNull(amount, "Amount cannot be null");
if (amount.compareTo(BigDecimal.ZERO) <= 0) {
Expand All @@ -27,6 +38,15 @@ public Donation(BigDecimal amount, User user, Organization organization) {
this.organization = Objects.requireNonNull(organization, "Organization cannot be null");
}


/**
* Sets the database-generated ID for this donation.
* Can only be set once — throws if the ID is already assigned.
*
* @param id the ID to assign; must be a positive non-null value
* @throws IllegalStateException if the ID has already been set
* @throws IllegalArgumentException if {@code id} is null or not positive
*/
public void setId(Long id) {
if (this.id != null) {
throw new IllegalStateException("ID is already set");
Expand All @@ -37,22 +57,47 @@ public void setId(Long id) {
this.id = id;
}

/**
* Returns the unique database ID of this donation.
*
* @return the donation ID
*/
public long getId() {
return id;
}

/**
* Returns the amount of this donation.
*
* @return the donation amount as a {@link BigDecimal}
*/
public BigDecimal getAmount() {
return amount;
}

/**
* Returns the date and time when this donation was created.
*
* @return the donation timestamp as a {@link LocalDateTime}
*/
public LocalDateTime getDateTime() {
return dateTime;
}

/**
* Returns the user who made this donation.
*
* @return the {@link User} associated with this donation
*/
public User getUser() {
return user;
}

/**
* Returns the organization this donation was made to.
*
* @return the {@link Organization} associated with this donation
*/
public Organization getOrganization() {
return organization;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/domain/organization/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import java.util.Objects;

/**
* Class
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Organization {

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/domain/user/Role.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package domain.user;

/**
* Enum class that defines if a user has the role Admin or user.
*/
public enum Role {ADMIN, USER
}
63 changes: 63 additions & 0 deletions src/main/java/domain/user/User.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package domain.user;

/**
* Class that defines what a User is.
* Contains get and set methods for each parameter, and validating methods.
*/
public class User {
private String userName;
private String phoneNumber;
private final String eMail;
private String password;
private Long id;

/**
* Constructor that takes in the parameters that define a user.
* It contains input validation on all parameters.
* @param userName
* @param phoneNumber
* @param password
* @param eMail
*/
public User(String userName, String phoneNumber, String password, String eMail) {

if (userName == null || userName.isBlank()) {
Expand All @@ -31,40 +43,91 @@ public User(String userName, String phoneNumber, String password, String eMail)
this.password = password;
}

/**
* Returns the username of this user.
*
* @return the username
*/
public String getUsername() {
return userName;
}

/**
* Returns the phone number of this user.
*
* @return the phone number
*/
public String getPhoneNumber() {
return phoneNumber;
}


/**
* Returns the email address of this user.
*
* @return the email address
*/
public String getEmail() {
return eMail;
}

/**
* Returns the password of this user.
*
* @return the password hash
*/
public String getPassword() {
return password;
}

/**
* Returns the ID of this user.
*
* @return the user ID
*/
public long getID() { return id; }

/**
* Sets a new password hash for this user.
*
* @param password the new password hash; must not be null
*/
public void setPassword(String password) {
this.password = password;
}

/**
* Sets a new username for this user.
*
* @param userName the new username; must not be null or blank
*/
public void setUsername(String userName) {
this.userName = userName;
}

/**
* Sets a new phone number for this user.
*
* @param phoneNumber the new phone number; must be exactly 8 digits
*/
public void setPhonenumber (String phoneNumber) {
this.phoneNumber = phoneNumber;
}

/**
* Sets the ID for this user.
*
* @param id the ID to assign
*/
public void setId(Long id) {
this.id = id;
}

/**
* Method that validates that a e-mail is in the right format.
* @param email
* @return
*/
private static boolean isValidEmail(String email) {
if (email == null || email.isBlank()) return false;
return email.matches("^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/integration/InnsamlingskontrollenClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import org.jsoup.nodes.Document;


/**
* The InnsamlingskontrollenClient class provides functionality to fetch a list
* of organizations from the Innsamlingskontrollen API. The fetched data is
* represented as an array of Organization objects.
*/
public class InnsamlingskontrollenClient {

public static Organization[] fetchOrganizations() throws Exception {
Expand Down
Loading