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
Binary file not shown.
25 changes: 19 additions & 6 deletions src/main/java/app/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
* Entry point for the GiveHope application.
* Initializes the database, fetches organizations from Innsamlingskontrollen,
* and launches the JavaFX application.
*/
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}

/**
* Starts the JavaFX application by initializing the database,
* loading organizations from Innsamlingskontrollen, and displaying the main view.
*
* @param stage the primary stage for the application
* @throws Exception if the FXML file cannot be loaded
*/
@Override
public void start(Stage stage) throws Exception {
Database.initializeDatabase();
System.out.println("DB path: " + new java.io.File("givehope.db").getAbsolutePath());
OrganizationDetails details = InnsamlingskontrollenClient.fetchDetails(
"https://www.innsamlingskontrollen.no/organisasjoner/caritas-norge/"
);
System.out.println("Description: " + details.getDescription());
System.out.println("Logo: " + details.getLogoUrl());

OrganizationDao orgDao = new OrganizationDao();
Organization[] orgs = InnsamlingskontrollenClient.fetchOrganizations();
for (Organization org : orgs) {
Expand All @@ -50,6 +57,12 @@ public void start(Stage stage) throws Exception {
stage.show();
}

/**
* Inserts test users and donations into the database if they do not already exist.
* Used for development and demonstration purposes.
*
* @throws SQLException if a database error occurs
*/
private void addTestdata() throws SQLException {
UserDao userDao = new UserDao();
DonationDao donationDao = new DonationDao();
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/application/user/UserEditProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package application.user;

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

/**
* Use case class for editing a user's profile information.
* Handles validation, optional password hashing and persistence of updated user data.
*/
public class UserEditProfile {

private final PasswordHasher hasher;
private final UserRepository repo;

/**
* Constructs a new {@code UserEditProfile} with the given hasher and repository.
*
* @param hasher the password hasher used to hash new passwords
* @param repo the repository used to persist updated user data
*/
public UserEditProfile(PasswordHasher hasher, UserRepository repo) {
this.hasher = hasher;
this.repo = repo;
}

/**
* Updates the user's profile with the provided information.
* If the password field is non-blank, it will be hashed and updated.
*
* @param user the user to update
* @param username the new username
* @param email the new email address
* @param phoneNumber the new phone number
* @param password the new password, or blank/null to keep the current password
*/
public void execute(User user, String username, String email, String phoneNumber, String password) {
user.setUsername(username);
user.setEmail(email);
user.setPhoneNumber(phoneNumber);
if (password != null && !password.isBlank()) {
user.setPassword(hasher.hash(password));
}
repo.updateUser(user);
}
}
61 changes: 61 additions & 0 deletions src/main/java/application/user/UserFavorite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package application.user;

import domain.organization.Organization;
import persistence.dao.FavoriteDao;


import java.util.List;

public class UserFavorite {

private final FavoriteDao favoriteDao;

/**
* Use case class for managing a user's favourite organizations.
* Delegates all persistence operations to {@link FavoriteDao}.
*/
public UserFavorite(FavoriteDao favouriteDao) {
this.favoriteDao = favouriteDao;
}

/**
* Adds an organization to a user's favorites.
*
* @param userId the unique identifier of the user
* @param orgNumber the organization number to add as favourite
*/
public void addFavorite(long userId, String orgNumber) {
favoriteDao.addFavorite(userId, orgNumber);
}

/**
* Removes an organization from a user's favorites.
*
* @param userId the unique identifier of the user
* @param orgNumber the organization number to remove
*/
public void removeFavorite(long userId, String orgNumber) {
favoriteDao.removeFavorite(userId, orgNumber);
}

/**
* Retrieves all favorite organizations for a given user.
*
* @param userId the unique identifier of the user
* @return a list of {@link Organization} objects the user has saved as favourites
*/
public List<Organization> getFavorites(long userId) {
return favoriteDao.getFavorites(userId);
}

/**
* Checks whether an organization is saved as a favourite by a specific user.
*
* @param userId the unique identifier of the user
* @param orgNumber the organization number to check
* @return {@code true} if the organization is a favourite, {@code false} otherwise
*/
public boolean isFavorite(long userId, String orgNumber) {
return favoriteDao.isFavorite(userId, orgNumber);
}
}
36 changes: 0 additions & 36 deletions src/main/java/application/user/UserLogin.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/application/user/UserSignIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public UserSignIn(PasswordHasher hasher, UserRepository repo) {
* @return user object, if it exists.
*/
public User execute(String login, String password) {
login.trim().toLowerCase();
login = login.trim().toLowerCase();

User user = repo.findByLogin(login)
.orElseThrow(() -> new IllegalArgumentException("Invalid credentials"));
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/domain/donation/Donation.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public class Donation {
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
* Creates a new donation with the current date and time.
*
* @param amount the donation amount, must be greater than 0
* @param user the user making the donation, cannot be null
* @param organization the organization receiving the donation, cannot be null
* @throws IllegalArgumentException if amount is zero or negative
*/
public Donation(BigDecimal amount, User user, Organization organization) {
Objects.requireNonNull(amount, "Amount cannot be null");
Expand All @@ -37,6 +38,26 @@ public Donation(BigDecimal amount, User user, Organization organization) {
this.organization = Objects.requireNonNull(organization, "Organization cannot be null");
}

/**
* Creates a donation with an existing date and time, used when loading from the database.
*
* @param amount the donation amount, must be greater than 0
* @param user the user making the donation, cannot be null
* @param organization the organization receiving the donation, cannot be null
* @param dateTime the date and time of the donation, cannot be null
* @throws IllegalArgumentException if amount is zero or negative
*/
public Donation(BigDecimal amount, User user, Organization organization, LocalDateTime dateTime) {
Objects.requireNonNull(amount, "Amount cannot be null");
if (amount.compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("Amount must be greater than 0");
}
this.amount = amount;
this.dateTime = Objects.requireNonNull(dateTime, "DateTime cannot be null");
this.user = Objects.requireNonNull(user, "User cannot be null");
this.organization = Objects.requireNonNull(organization, "Organization cannot be null");
}


/**
* Sets the database-generated ID for this donation.
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/persistence/dao/DonationDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import persistence.db.Database;
Expand All @@ -28,10 +29,6 @@ public class DonationDao {
* @throws RuntimeException if a database error occurs during the insert
*/
public void insert(Donation donation) {
System.out.println("Inserting donation:");
System.out.println(" user_id = " + donation.getUser().getID());
System.out.println(" org_id = " + donation.getOrganization().getOrgNumber());

String sql = "INSERT INTO donation(amount, donation_date" +
", user_id, organization_id) VALUES(?, ?, ?, ?)";

Expand Down Expand Up @@ -80,10 +77,12 @@ private Donation mapRow(ResultSet rs) throws SQLException {
org.setPreApproved(rs.getInt("is_pre_approved") == 1);



Donation donation = new Donation(
new BigDecimal(rs.getString("amount")),
user,
org
org,
LocalDateTime.parse(rs.getString("donation_date"))
);
donation.setId(rs.getLong("id"));
return donation;
Expand Down
Loading
Loading