Skip to content

Commit

Permalink
Fix: Fix OrganizationRepository constructor and update related classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Marjoni committed Mar 5, 2026
1 parent fb67556 commit 6c34cfc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 33 deletions.
4 changes: 0 additions & 4 deletions src/main/java/edu/group5/app/model/DBRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package edu.group5.app.model;


import java.util.HashMap;
import java.util.Map;

/**
* Abstract base class for repositories that store their data
* in a database-related structure.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/group5/app/model/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class Repository<K, V> {
*
* @param content the underlying data structure used to store entities
*/
public Repository(Map<K, V> content) {
protected Repository(Map<K, V> content) {
this.content = content;
}

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/edu/group5/app/model/donation/DonationService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package edu.group5.app.service;
package edu.group5.app.model.donation;
import java.time.Instant;

import java.math.BigDecimal;
import java.sql.Timestamp;
import edu.group5.app.model.donation.Donation;
import edu.group5.app.model.organization.Organization;
import edu.group5.app.model.organization.OrganizationRepository;
import edu.group5.app.model.user.Customer;
import edu.group5.app.model.donation.DonationRepository;

/**
* DonationService class provides functionality for handling donations in the system.
Expand Down Expand Up @@ -47,16 +45,16 @@ public DonationService(DonationRepository donationRepository,
* @param amount the amount of the donation
* @return true if the donation is successfully processed, false otherwise
*/
public boolean donate(Customer customer, int orgNumber, BigDecimal amount) {
if (customer == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
public boolean donate(Customer customer, int orgNumber, BigDecimal amount, String paymentMethod) {
if (customer == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0 || paymentMethod.isBlank()) {
return false;
}
Organization org = organizationRepository.findByOrgNumber(orgNumber);
if (org == null) {
return false;
}
Donation donation = new Donation(customer.getUserId(), org.orgNumber(),
amount, Timestamp.from(Instant.now()));
Donation donation = new Donation(1, customer.getUserId(), org.orgNumber(), /* TODO fix incrementation of donation */
amount, Timestamp.from(Instant.now()), paymentMethod);
donationRepository.addDonation(donation);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public class OrganizationRepository extends Repository<Integer, Organization> {
private final HashMap<Integer, Organization> grandMap;

public OrganizationRepository(Object[] input) {
super(new HashMap<>());
grandMap = new HashMap<>();
if (input == null) {
throw new IllegalArgumentException("The input cannot be null");
}
grandMap = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();

for (Object obj : input) {
Expand All @@ -33,12 +34,7 @@ public OrganizationRepository(Object[] input) {

String websiteURL = (String) contentMap.get("url");

boolean isPreApproved = true;
if (!contentMap.containsKey("is_pre_approved")) {
isPreApproved = false;
} else if ((boolean)contentMap.get("is_pre_approved") == false) {
isPreApproved = false;
}
boolean isPreApproved = Boolean.TRUE.equals(contentMap.get("is_pre_approved"));

String description = "Information about " + name;

Expand All @@ -63,4 +59,11 @@ public Map<Integer, Organization> getTrustedOrganizations() {
return trustedOrganizations;
}

public Organization findByOrgNumber(int orgNumber) {
if (orgNumber == 0 || orgNumber <= 0) {
throw new IllegalArgumentException("The Organization number must be a positive integer");
}
return grandMap.get(orgNumber);
}

}
2 changes: 0 additions & 2 deletions src/main/java/edu/group5/app/model/user/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.List;
import java.util.ArrayList;

import edu.group5.app.model.organization.Organization;

/**
* Customer class represents a customer in the system.
* It extends the User class and includes additional functionality specific to customers,
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/edu/group5/app/model/user/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.group5.app.model.user;

import java.util.HashMap;

import edu.group5.app.model.DBRepository;

public class UserRepository extends DBRepository<Integer, User>{

/**
* Constructs DonationRepository using Hashmap,
* and extends the content from DBRepository.
* @param content the underlying map used to store donations,
* where the key represents the donation ID
*/
public UserRepository(HashMap<Integer, User> content) {
super(content);
if (content == null) {
throw new IllegalArgumentException("Content cannot be null");
}
}

/**
* Adds a new donation to the repository
* <p>
* The donation is stored using its {@code donationId} as the key.
* If a donation with the same ID already exists, the donation
* will not be added.
* </p>
*
* @param donation the donation to add
* @return {@code true} if the donation was successfully added, and
* {@code false} if a donation with the same ID already exists
*/
public boolean addUser(User user) {
if (user == null) {
throw new IllegalArgumentException("Donation cannot be null");
}
if (content.containsKey(user.getUserId())){
return false;
}
content.put(user.getUserId(), user);
return true;
}

public User findUserByEmail(String email) {
return null;
}
}
16 changes: 6 additions & 10 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public boolean registerUser(String role, String firstName, String lastName,
User user;
if (role.equalsIgnoreCase("Customer")) {
user = new Customer(0, firstName, lastName, email, passwordHash);
}
} else {
return false;
}
userRepository.addUser(user);
return true;
}
Expand All @@ -32,13 +34,7 @@ public boolean login(String email, String password) {
if (email == null || email.trim().isEmpty() || password == null) {
return false;
}
User user = userRepository.findByEmail(email);
if (user == null) {
return false;
}
if (user.verifyPassword(password)) {
return true;
}
return false;
}
User user = userRepository.findUserByEmail(email);
return user != null && user.verifyPassword(password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.group5.app.model.user.Customer;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
Expand Down

0 comments on commit 6c34cfc

Please sign in to comment.