-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: divided controllers into Navigation-, User-, Donation- and …
…OrganizationController
- Loading branch information
Showing
5 changed files
with
233 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/edu/group5/app/control/DonationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +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(); | ||
| } | ||
| } |
100 changes: 46 additions & 54 deletions
100
src/main/java/edu/group5/app/control/NavigationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,99 +1,91 @@ | ||
| package edu.group5.app.control; | ||
|
|
||
| import edu.group5.app.model.AppState; | ||
| import edu.group5.app.model.donation.DonationService; | ||
| import edu.group5.app.model.organization.Organization; | ||
| import edu.group5.app.model.organization.OrganizationService; | ||
| import edu.group5.app.model.user.User; | ||
| import edu.group5.app.model.user.UserService; | ||
| import edu.group5.app.view.ViewManager; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class MainController { | ||
| private final ViewManager view; | ||
|
|
||
| import edu.group5.app.view.Header; | ||
| import edu.group5.app.view.causespage.CausesPageView; | ||
| import edu.group5.app.view.donationpage.DonationPageView; | ||
| import edu.group5.app.view.donationpage.PaymentCompletePageView; | ||
| import edu.group5.app.view.homepage.HomePageView; | ||
| import edu.group5.app.view.loginpage.LoginHeader; | ||
| import edu.group5.app.view.loginpage.LoginPageView; | ||
| import edu.group5.app.view.loginpage.SignInPageView; | ||
| import edu.group5.app.view.organizationpage.OrganizationPageView; | ||
| import edu.group5.app.view.userpage.UserPageView; | ||
| import javafx.scene.layout.BorderPane; | ||
|
|
||
| public class NavigationController { | ||
| private final BorderPane root; | ||
| private final Header header; | ||
| private final LoginHeader loginHeader; | ||
|
|
||
| private final AppState appState; | ||
| private final UserService userService; | ||
| private final DonationService donationService; | ||
| private final OrganizationService organizationService; | ||
|
|
||
| private final UserController userController; | ||
| private final DonationController donationController; | ||
| private final OrganizationController organizationController; | ||
|
|
||
| private User currentUser; | ||
| private Organization currentOrganization; | ||
| private BigDecimal currentDonationAmount; | ||
| public NavigationController(BorderPane root, AppState appState, UserService userService, DonationService donationService, OrganizationService organizationService) { | ||
| this.root = root; | ||
| this.header = new Header(this); | ||
| this.loginHeader = new LoginHeader(); | ||
|
|
||
| public MainController(UserService userService, DonationService donationService, | ||
| OrganizationService organizationService) { | ||
| this.appState = appState; | ||
| this.userService = userService; | ||
| this.donationService = donationService; | ||
| this.organizationService = organizationService; | ||
|
|
||
| this.userController = new UserController(this, userService, donationService, organizationService); | ||
|
|
||
| this.view = new ViewManager(this); | ||
| } | ||
|
|
||
| public ViewManager getMainView() { | ||
| return this.view; | ||
| } | ||
|
|
||
| public User getCurrentUser() { | ||
| return this.currentUser; | ||
| } | ||
|
|
||
| public void setCurrentUser(User user) { | ||
| this.currentUser = user; | ||
| } | ||
|
|
||
| public Organization getCurrentOrganization() { | ||
| return this.currentOrganization; | ||
| } | ||
|
|
||
| public void setCurrentOrganization(Organization organization) { | ||
| this.currentOrganization = organization; | ||
| } | ||
|
|
||
| public BigDecimal getCurrentDonationAmount() { | ||
| return this.currentDonationAmount; | ||
| } | ||
|
|
||
| public void setCurrentDonationAmount(BigDecimal amount) { | ||
| this.currentDonationAmount = amount; | ||
| this.userController = new UserController(appState, this, userService); | ||
| this.donationController = new DonationController(appState, this, donationService); | ||
| this.organizationController = new OrganizationController(appState, this, organizationService); | ||
| } | ||
|
|
||
| public void showHomePage() { | ||
| view.showHomePage(); | ||
| root.setTop(header); | ||
| root.setCenter(new HomePageView(appState, this)); | ||
| } | ||
|
|
||
| public void showLoginPage() { | ||
| view.showLoginPage(); | ||
| root.setTop(loginHeader); | ||
| root.setCenter(new LoginPageView(appState, this, userController)); | ||
| } | ||
|
|
||
| public void showSignInPage() { | ||
| view.showSignInPage(); | ||
| root.setTop(loginHeader); | ||
| root.setCenter(new SignInPageView(appState, this, userController)); | ||
| } | ||
|
|
||
| public void showPaymentCompletePage() { | ||
| view.showPaymentCompletePage(); | ||
| root.setTop(header); | ||
| root.setCenter(new PaymentCompletePageView(this)); | ||
| } | ||
|
|
||
| public void showCausesPage() { | ||
| view.showCausesPage(); | ||
| root.setTop(header); | ||
| root.setCenter(new CausesPageView(appState, this, organizationController)); | ||
| } | ||
|
|
||
| public void showOrganizationPage() { | ||
| view.showOrganizationPage(); | ||
| root.setTop(header); | ||
| root.setCenter(new OrganizationPageView(appState, this, donationController)); | ||
| } | ||
|
|
||
| public void showDonationPage() { | ||
| view.showDonationPage(); | ||
| root.setTop(header); | ||
| root.setCenter(new DonationPageView(appState, this, donationController)); | ||
| } | ||
|
|
||
| public void showAboutUsPage() { | ||
| view.showAboutUsPage(); | ||
| root.setTop(header); | ||
| } | ||
|
|
||
| public void showUserPage() { | ||
| view.showUserPage(); | ||
| root.setTop(header); | ||
| root.setCenter(new UserPageView(appState, this, userController, donationController, organizationController)); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/edu/group5/app/control/OrganizationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,27 @@ | ||
| package edu.group5.app.control; | ||
|
|
||
| import edu.group5.app.model.AppState; | ||
| import edu.group5.app.model.organization.Organization; | ||
| import edu.group5.app.model.organization.OrganizationService; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class OrganizationController { | ||
| private final AppState appState; | ||
| private final NavigationController nav; | ||
| private final OrganizationService service; | ||
|
|
||
| public OrganizationController(AppState appState, NavigationController nav, OrganizationService service) { | ||
| this.appState = appState; | ||
| this.nav = nav; | ||
| this.service = service; | ||
| } | ||
|
|
||
| public Organization getOrgById(int orgId) { | ||
| return service.findByOrgNumber(orgId); | ||
| } | ||
|
|
||
| public Map<Integer, Organization> getTrustedOrgs() { | ||
| return service.getTrustedOrganizations(); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,80 @@ | ||
| 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.organization.OrganizationService; | ||
| import edu.group5.app.model.user.Customer; | ||
| 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; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class UserController { | ||
| private final AppState appState; | ||
| private final NavigationController nav; | ||
| private final UserService userService; | ||
|
|
||
| public UserController(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(); | ||
| } | ||
| } |