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
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
22 changes: 20 additions & 2 deletions src/main/java/application/user/UserEditProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,36 @@
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);
Expand Down
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
5 changes: 5 additions & 0 deletions src/main/java/persistence/dao/FavoriteDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import java.util.ArrayList;
import java.util.List;

/**
* Data Access Object for favorite organization-related database operations.
* Provides methods for adding, removing, retrieving and checking
* a user's saved favorite organizations.
*/
public class FavoriteDao {


Expand Down
6 changes: 6 additions & 0 deletions src/main/java/persistence/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ private User mapUser(ResultSet rs) throws SQLException {
return user;
}

/**
* Updates an existing user's profile information in the database.
*
* @param user the {@link User} containing the updated information to persist
* @throws RuntimeException if a database error occurs during the update
*/
public void updateUser(User user) {
String sql = "UPDATE user SET user_name = ?, phone_number = ?, e_mail = ?, password_hash = ? WHERE id = ?";

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/ui/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*/
public enum Page {
HOME("Home.fxml"),
PROFILE("MyProfile.fxml"),
SIGN_IN("SignIn.fxml"),
REGISTER("Register.fxml"),
ORGANIZATIONS("OrganizationsView.fxml"),
MY_PROFILE("MyProfile.fxml"),
DONATION_NOT_LOGGED_IN("DonationNotLoggedIn.fxml"),
DONATION_AMOUNT("DonationAmount.fxml"),
DONATION_PAYMENT("DonationPayment.fxml"),
DONATION_CONFIRMATION("DonationConfirmation.fxml");
PROFILE("profileView/MyProfile.fxml"),
SIGN_IN("auth/SignIn.fxml"),
REGISTER("auth/Register.fxml"),
ORGANIZATIONS("organizationView/OrganizationsView.fxml"),
MY_PROFILE("profileView/MyProfile.fxml"),
DONATION_NOT_LOGGED_IN("donationView/DonationNotLoggedIn.fxml"),
DONATION_AMOUNT("donationView/DonationAmount.fxml"),
DONATION_PAYMENT("donationView/DonationPayment.fxml"),
DONATION_CONFIRMATION("donationView/DonationConfirmation.fxml");

private final String fileName;

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/ui/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
import ui.DonationSession;
import ui.Page;
import ui.DonationSessionAware;
import ui.controller.auth.RegisterController;
import ui.controller.auth.SignInController;
import ui.controller.donation.DonationFlowController;
import ui.controller.organization.OrganizationController;
import ui.controller.organization.OrganizationDetailController;
import ui.controller.organization.SignInRequiredController;
import ui.controller.profile.MyProfileController;

import java.io.IOException;

/**
Expand All @@ -37,6 +44,10 @@ public class MainController {
private DonationSession donationSession;
private DonationFlowController donationFlowController;

/**
* Initializes the main controller by setting up the donation flow
* and navbar navigation callbacks, then loads the home page.
*/
public void initialize() {
donationFlowController = new DonationFlowController(mainPane, navbarController);
donationFlowController.setOnNavigate(page -> {
Expand Down Expand Up @@ -127,7 +138,7 @@ public void loadDonationPage(Organization org) throws IOException {
public void showOrganizationDetail(Organization org, Button btn) {
try {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("/view/OrganizationDetail.fxml"));
getClass().getResource("/view/organizationView/OrganizationDetail.fxml"));
Parent root = loader.load();

OrganizationDetails details =
Expand Down Expand Up @@ -182,7 +193,7 @@ public void showOrganizationDetail(Organization org, Button btn) {
public void showSignInRequired() {
try {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("/view/SignInRequired.fxml"));
getClass().getResource("/view/organizationView/SignInRequired.fxml"));
Parent root = loader.load();

SignInRequiredController controller = loader.getController();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ui/controller/NavbarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void goHome() {
}

/**
* Navigates to the sign in page or profile page depending on authentication state.
* Navigates to the sign-in page or profile page depending on authentication state.
*/
@FXML
public void goToSignIn() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ui.controller;
package ui.controller.auth;

import application.user.UserRegister;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import ui.Page;
import ui.controller.NavigationAware;

import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ui.controller;
package ui.controller.auth;

import application.user.UserSignIn;
import domain.user.User;
Expand All @@ -7,6 +7,7 @@
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import ui.Page;
import ui.controller.NavigationAware;
import util.SessionManager;
import java.io.IOException;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void start(Organization org) throws IOException {

// Loads the "not logged in" page
private void loadStep1() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DonationNotLoggedIn.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/donationView/DonationNotLoggedIn.fxml"));
Parent root = loader.load();

DonationNotLoggedInController controller = loader.getController();
Expand All @@ -67,7 +67,7 @@ private void loadStep1() throws IOException {

// Loads the amount selection page
private void loadStep2() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DonationAmount.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/donationView/DonationAmount.fxml"));
Parent root = loader.load();

DonationAmountController controller = loader.getController();
Expand All @@ -92,7 +92,7 @@ private void loadStep2() throws IOException {

// Loads the payment information page
private void loadStep3() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DonationPayment.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/donationView/DonationPayment.fxml"));
Parent root = loader.load();

DonationPaymentController controller = loader.getController();
Expand Down Expand Up @@ -123,7 +123,7 @@ private void loadStep3() throws IOException {

// Loads the confirmation page
private void loadStep4() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DonationConfirmation.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/donationView/DonationConfirmation.fxml"));
Parent root = loader.load();

DonationConfirmationController controller = loader.getController();
Expand Down
Loading