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
6 changes: 3 additions & 3 deletions src/main/java/app/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import java.math.BigDecimal;
import java.sql.SQLException;

import persistence.DonationDao;
import persistence.OrganizationDao;
import persistence.UserDao;
import persistence.dao.DonationDao;
import persistence.dao.OrganizationDao;
import persistence.dao.UserDao;
import persistence.db.Database;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/application/user/UserDonate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
import domain.donation.Donation;
import domain.organization.Organization;
import domain.user.User;
import persistence.DonationDao;

import persistence.dao.DonationDao;
import java.math.BigDecimal;

/**
* Use case for handling donations made by a user to an organization.
* Creates a {@link Donation} and persists it to the database.
*/
public class UserDonate {
private final DonationDao donationDao;

/**
* Creates a new {@code UserDonate} with a default {@link DonationDao}.
*/
public UserDonate() {
this.donationDao = new DonationDao();
}

/**
* Executes the donation use case by creating and saving a donation.
* @param user the user making the donation
* @param org the organization receiving the donation
* @param amount the donation amount in NOK
*/
public void execute(User user, Organization org, BigDecimal amount) {
Donation donation = new Donation(amount, user, org);
donationDao.insert(donation);
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/application/user/UserRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import application.security.PasswordHasher;
import domain.user.User;
import persistence.UserRepository;
import persistence.dao.UserRepository;

/**
* The UserRegister class is responsible for handling the user registration process.
Expand Down Expand Up @@ -43,21 +43,16 @@ public void execute(String username, String phoneNumber, String password, String
email = email.trim().toLowerCase();
phoneNumber = phoneNumber.trim();

// duplicate check (repo.existsByEmail)
if (repo.existsByEmail(email)) {
throw new IllegalArgumentException("Email already in use");
}
if (repo.existsByUsername(username)) {
throw new IllegalArgumentException("This username is taken");
}

// hash password (optional)
String hashedPassword = hasher.hash(password);

// new User(...) (domain validates)
User user = new User(username, phoneNumber, hashedPassword, email);

// saves user
repo.insert(user);
}
}
7 changes: 1 addition & 6 deletions src/main/java/application/user/UserSignIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import application.security.PasswordHasher;
import domain.user.User;
import persistence.UserRepository;
import persistence.dao.UserRepository;

/**
* The UserSignIn class handles the process of user authentication.
Expand Down Expand Up @@ -36,22 +36,17 @@ public UserSignIn(PasswordHasher hasher, UserRepository repo) {
* @return user object, if it exists.
*/
public User execute(String login, String password) {
// normalize (trim etc.)
login.trim().toLowerCase();

User user = repo.findByLogin(login)
.orElseThrow(() -> new IllegalArgumentException("Invalid credentials"));

// hash password (optional)
String hashedInput = hasher.hash(password);

// compare hashed input with stored hash
if (!hashedInput.equals(user.getPassword())) {
throw new IllegalArgumentException("Invalid password");
}

System.out.println("Loggin in");
// return user (if match)
return user;
}
}
16 changes: 6 additions & 10 deletions src/main/java/application/user/UserStatistics.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package application.user;

import domain.user.User;
import java.util.ArrayList;

import java.util.List;
import persistence.DonationDao;
import persistence.dao.DonationDao;

/**
* Provides statistical information about a user's donation activity.
Expand All @@ -28,8 +28,7 @@ public UserStatistics() {
* 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());
return favoriteOrganization;
return donationDao.getFavoriteOrganization(user.getID());
}

/**
Expand All @@ -40,8 +39,7 @@ public List<String> userFavoriteOrganization(User user) {
* @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());
return userDonationList;
return donationDao.getUserDonations(user.getID());
}

/**
Expand All @@ -51,8 +49,7 @@ public List<String> userDonations(User user) {
* @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());
return totalDonations;
return donationDao.getTotalDonationAmount(user.getID());
}

/**
Expand All @@ -62,7 +59,6 @@ public String userTotalDonationAmount(User user) {
* @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());
return totalDonationsMade;
return donationDao.getTotalDonationsMade(user.getID());
}
}
6 changes: 0 additions & 6 deletions src/main/java/domain/donation/Cause.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/java/domain/donation/Donation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
* 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?
private Long id;
private final BigDecimal amount;
private final LocalDateTime dateTime;
private final User user;
Expand Down
59 changes: 53 additions & 6 deletions src/main/java/domain/organization/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import domain.donation.Cause;

import java.util.Objects;

/**
* Class
* Represents a charitable organization retrieved from the Innsamlingskontrollen API.
* Contains basic information such as name, organization number, status and URL.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Organization {
Expand All @@ -20,27 +19,75 @@ public class Organization {

@JsonProperty("status")
private String status;

@JsonProperty("url")
private String url;

@JsonProperty("is_pre_approved")
private boolean isPreApproved;
public boolean isPreApproved;

public Organization() {}

/**
* Returns the organization number.
* @return the organization number
*/
public String getOrgNumber() { return orgNumber; }

/**
* Sets the organization number.
* @param orgNumber the organization number to set
*/
public void setOrgNumber(String orgNumber) { this.orgNumber = orgNumber; }

/**
* Returns the name of the organization.
* @return the organization name
*/
public String getName() { return name; }

/**
* Sets the name of the organization.
* @param name the name to set
*/
public void setName(String name) { this.name = name; }

/**
* Returns the approval status of the organization.
* @return the status, either "approved" or "obs"
*/
public String getStatus() { return status; }

/**
* Sets the approval status of the organization.
* @param status the status to set
*/
public void setStatus(String status) { this.status = status; }

/**
* Returns the URL to the organization's page on Innsamlingskontrollen.
* @return the URL
*/
public String getUrl() { return url; }

/**
* Sets the URL to the organization's page on Innsamlingskontrollen.
* @param url the URL to set
*/
public void setUrl(String url) { this.url = url; }

public boolean isPreApproved() { return isPreApproved; }
/**
* Returns whether the organization is pre-approved.
* @return true if pre-approved, false otherwise
*/
public boolean isPreApproved() {
return isPreApproved;
}

/**
* Sets whether the organization is pre-approved.
* @param preApproved true if pre-approved, false otherwise
*/
public void setPreApproved(boolean preApproved) { isPreApproved = preApproved; }

}
21 changes: 19 additions & 2 deletions src/main/java/domain/organization/OrganizationDetails.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package domain.organization;

/**
* Holds additional details about an organization fetched from Innsamlingskontrollen's website.
* Contains a short description and a URL to the organization's logo.
*/
public class OrganizationDetails {
private String description;
private String logoUrl;
private final String description;
private final String logoUrl;

/**
* Creates a new OrganizationDetails with the given description and logo URL.
* @param description a short description of the organization
* @param logoUrl the URL to the organization's logo
*/
public OrganizationDetails(String description, String logoUrl) {
this.description = description;
this.logoUrl = logoUrl;
}

/**
* Returns the organization's description.
* @return the description
*/
public String getDescription() {
return description;
}

/**
* Returns the URL to the organization's logo.
* @return the logo URL
*/
public String getLogoUrl() {
return logoUrl;
}
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/domain/user/Role.java

This file was deleted.

45 changes: 9 additions & 36 deletions src/main/java/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class User {
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
* Creates a new User with the given parameters.
* Validates all input before assigning.
* @param userName the username; must not be null or blank
* @param phoneNumber the phone number; must be exactly 8 digits
* @param password the hashed password
* @param eMail the email address; must be a valid email format
*/
public User(String userName, String phoneNumber, String password, String eMail) {

Expand Down Expand Up @@ -87,33 +87,6 @@ public String getPassword() {
*/
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.
*
Expand All @@ -124,9 +97,9 @@ public void setId(Long id) {
}

/**
* Method that validates that a e-mail is in the right format.
* @param email
* @return
* Validates that an email address is in the correct format.
* @param email the email address to validate
* @return true if the email is valid, false otherwise
*/
private static boolean isValidEmail(String email) {
if (email == null || email.isBlank()) return false;
Expand Down
Loading