Skip to content

Commit

Permalink
Merge pull request #59 from Group-5/refactor/frontend-structure
Browse files Browse the repository at this point in the history
Refactor/frontend structure
  • Loading branch information
fredrjm authored Mar 26, 2026
2 parents 80f5044 + 6bab090 commit b031166
Show file tree
Hide file tree
Showing 30 changed files with 500 additions and 627 deletions.
24 changes: 14 additions & 10 deletions src/main/java/edu/group5/app/App.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package edu.group5.app;

import edu.group5.app.control.MainController;
import edu.group5.app.control.NavigationController;
import edu.group5.app.control.wrapper.DbWrapper;
import edu.group5.app.control.wrapper.OrgApiWrapper;
import edu.group5.app.model.donation.Donation;
import edu.group5.app.model.AppState;
import edu.group5.app.model.donation.DonationRepository;
import edu.group5.app.model.donation.DonationService;
import edu.group5.app.model.organization.OrganizationRepository;
import edu.group5.app.model.organization.OrganizationService;
import edu.group5.app.model.user.User;
import edu.group5.app.model.user.UserRepository;
import edu.group5.app.model.user.UserService;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.List;
Expand All @@ -28,9 +28,12 @@ public class App extends Application {
DbWrapper dbWrapper;
UserRepository userRepository;
DonationRepository donationRepository;

BorderPane root;
AppState appState;
NavigationController nav;

private Logger logger;
private MainController controller;
private Scene scene;

@Override
public void init() {
Expand Down Expand Up @@ -69,18 +72,19 @@ public void init() {
DonationService donationService = new DonationService(this.donationRepository, organizationRepository);
OrganizationService organizationService = new OrganizationService(organizationRepository);

this.controller = new MainController(userService, donationService, organizationService);

this.scene = controller.getMainView().getScene();
this.root = new BorderPane();
this.appState = new AppState();
this.nav = new NavigationController(root, appState, userService, donationService, organizationService);
}

@Override
public void start(Stage stage) {
this.controller.showLoginPage();
this.nav.showLoginPage();

Scene scene = new Scene(root, 1280, 720);
stage.getIcons().add(new Image(getClass().getResource("/header/images/hmh-logo.png").toExternalForm()));
stage.setTitle("Help-Me-Help");
stage.setScene(this.scene);
stage.setScene(scene);
stage.show();
}

Expand Down
16 changes: 0 additions & 16 deletions src/main/java/edu/group5/app/control/BrowseCardController.java

This file was deleted.

This file was deleted.

84 changes: 84 additions & 0 deletions src/main/java/edu/group5/app/control/DonationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package edu.group5.app.control;

import edu.group5.app.model.AppState;
import edu.group5.app.model.donation.Donation;
import edu.group5.app.model.donation.DonationService;
import edu.group5.app.model.organization.Organization;
import edu.group5.app.model.user.Customer;
import edu.group5.app.model.user.User;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class DonationController {
private final AppState appState;
private final NavigationController nav;
private final DonationService service;

public DonationController(AppState appState, NavigationController nav, DonationService service) {
this.appState = appState;
this.nav = nav;
this.service = service;
}

public Map<Integer, Donation> getUserDonations(int userId) {
return service.getDonationRepository().filterByUser(userId);
}

public Set<Integer> getUniqueOrgs() {
Map<Integer, Donation> userDonations = getUserDonations(appState.getCurrentUser().getUserId());

Set<Integer> uniqueOrgs = new HashSet<>();
for (Donation donation : userDonations.values()) {
uniqueOrgs.add(donation.organizationId());
}

return uniqueOrgs;
}

public void handleDonate() {
// Get session data from MainController
User currentUser = appState.getCurrentUser();
Organization currentOrg = appState.getCurrentOrganization();
BigDecimal amount = appState.getCurrentDonationAmount();

if (currentUser == null) {
System.err.println("Error: No user logged in");
return;
}
if (!(currentUser instanceof Customer customer)) {
System.err.println("Error: Only customers can donate");
return;
}
if (currentOrg == null) {
System.err.println("Error: No organization selected");
return;
}
if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
System.err.println("Error: Invalid donation amount");
return;
}

// Create donation via service
boolean success = service.donate(
customer,
currentOrg.orgNumber(),
amount,
"Online"
);

if (success) {
System.out.println("Donation created: " + amount + " kr to " + currentOrg.name());
} else {
System.err.println("Failed to create donation");
}

// Clear donation session state
appState.setCurrentDonationAmount(null);

// Navigate to payment complete
nav.showPaymentCompletePage();
}
}
28 changes: 0 additions & 28 deletions src/main/java/edu/group5/app/control/HeaderController.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/edu/group5/app/control/HomePageController.java

This file was deleted.

69 changes: 69 additions & 0 deletions src/main/java/edu/group5/app/control/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package edu.group5.app.control;

import edu.group5.app.model.AppState;
import edu.group5.app.model.user.User;
import edu.group5.app.model.user.UserService;
import edu.group5.app.view.loginpage.LoginPageView;
import edu.group5.app.view.loginpage.SignInPageView;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class LoginController {
private final AppState appState;
private final NavigationController nav;
private final UserService userService;

public LoginController(AppState appState, NavigationController nav, UserService userService) {
this.appState = appState;
this.nav = nav;
this.userService = userService;
}

public void handleSignIn(SignInPageView view, String firstName, String lastName, String email, char[] passwordChars) {
if (firstName == null || firstName.trim().isEmpty() ||
lastName == null || lastName.trim().isEmpty() ||
email == null || email.trim().isEmpty() ||
passwordChars == null || passwordChars.length == 0) {
view.showError("All fields are required");
return;
}

String password = new String(passwordChars);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String hashedPassword = encoder.encode(password);

boolean success = userService.registerUser(
"Customer", firstName, lastName, email, hashedPassword);

if (success) {
User user = userService.getUserByEmail(email);

appState.setCurrentUser(user);
nav.showHomePage();
} else {
view.showError("Registration failed. Email may already be in use.");
}
}

public void handleLogin(LoginPageView view, String email, char[] passwordChars) {
if (email == null || email.trim().isEmpty() || passwordChars == null || passwordChars.length == 0) {
view.showError("Email and password are required");
return;
}

User user = userService.login(email, passwordChars);

if (user != null) {
appState.setCurrentUser(user);
nav.showHomePage();
} else {
view.showError("Invalid email or password");
}
}

public void handleLogout() {
appState.setCurrentUser(null);
appState.setCurrentOrganization(null);
appState.setCurrentDonationAmount(null);
nav.showLoginPage();
}
}
44 changes: 0 additions & 44 deletions src/main/java/edu/group5/app/control/LoginPageController.java

This file was deleted.

Loading

0 comments on commit b031166

Please sign in to comment.