-
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.
Merge pull request #59 from Group-5/refactor/frontend-structure
Refactor/frontend structure
- Loading branch information
Showing
30 changed files
with
500 additions
and
627 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
16 changes: 0 additions & 16 deletions
16
src/main/java/edu/group5/app/control/BrowseCardController.java
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/java/edu/group5/app/control/BrowsePageController.java
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
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 |
|---|---|---|
| @@ -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
28
src/main/java/edu/group5/app/control/HeaderController.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/main/java/edu/group5/app/control/HomePageController.java
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -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
44
src/main/java/edu/group5/app/control/LoginPageController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.