Skip to content

Commit

Permalink
merge: merge feat/wrapper into feat/user
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Marjoni committed Mar 3, 2026
2 parents 2e3bee8 + a3752a2 commit c334f4e
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ out/
## VSCode
##############################
.vscode/

##############################
## Misc
##############################
.settings/*
.project
.classpath
31 changes: 30 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@
<artifactId>commons-logging</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<!-- Source: https://mvnrepository.com/artifact/tools.jackson.core/jackson-databind -->
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down Expand Up @@ -91,6 +98,28 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.12.0</version>
</plugin>

<!-- Fat JAR plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>edu.group5.app.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
11 changes: 8 additions & 3 deletions src/main/java/edu/group5/app/App.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package edu.group5.app;

import edu.group5.app.control.OrgAPIWrapper;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
public static void main(String[] args) {
OrgAPIWrapper orgWrap = new OrgAPIWrapper("https://app.innsamlingskontrollen.no/api/public/v1/all");
System.out.println();
System.out.println();
orgWrap.importData();
}
}
42 changes: 42 additions & 0 deletions src/main/java/edu/group5/app/control/OrgAPIWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.group5.app.control;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import tools.jackson.databind.ObjectMapper;

public class OrgAPIWrapper extends Wrapper {
private String urlString;
private Object[] data;

public OrgAPIWrapper(String urlString) {
this.urlString = urlString;
}

@Override
public boolean importData() {
URI uri = URI.create(this.urlString);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();

try {
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
this.data = new ObjectMapper().readValue(response.body(), Object[].class);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

@Override
public Object[] getData() {
return this.data;
}
}
10 changes: 8 additions & 2 deletions src/main/java/edu/group5/app/control/Wrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package edu.group5.app.control;

public class Wrapper {

abstract class Wrapper {

protected Wrapper() {
}

public abstract boolean importData();

public abstract Object getData();
}
5 changes: 2 additions & 3 deletions src/main/java/edu/group5/app/model/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* Represents a repository
*/
public abstract class Repository<K, V> {
protected final Map<K, V> content;
protected Map<K, V> content;

public Repository(Map<K, V> content) {
this.content = content;
protected Repository() {
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.group5.app.model.organization;

import edu.group5.app.model.Repository;
import tools.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -10,15 +11,34 @@
* Handles the business logic associated with organizations
*/
public class OrganizationRepository extends Repository<Integer, Organization> {
/**
* Creates a new Organization Repository
*
* @param content holds all current organizations in the repository; must not be null
* @throws NullPointerException if content is null
*/
public OrganizationRepository(Map<Integer, Organization> content) {
super(Objects.requireNonNull(content, "content cannot be null"));
}

@SuppressWarnings("unchecked")
public OrganizationRepository(Object[] input) {

if (input == null) {
throw new NullPointerException("Input cannot be null");
}
for (Object obj : input) {
Map<String, Object> map = (Map<String, Object>) obj;

int orgNumber = Integer.parseInt((String) map.get("org_number"));
String name = (String) map.get("name");

boolean trusted =
"approved".equalsIgnoreCase((String) map.get("status"));
String url = (String) map.get("url");

boolean isPreApproved = (Boolean) map.get("is_pre_approved");

Organization organization = new Organization(
orgNumber, name, trusted, url,
isPreApproved,
"" // description not included in API
);

content.put(orgNumber, organization);
}
}

/**
* Gets all trusted organizations in the repository
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.group5.app.model.user;

public class UserService {
private UserRepository userRepository;


public UserService(UserRepository userRepository) {
if (userRepository == null) {
throw new IllegalArgumentException("UserRepository cannot be null");
}
this.userRepository = userRepository;
}

public boolean registerUser(String role, String firstName, String lastName,
String email, String passwordHash) {
if (role == null || role.trim().isEmpty() ||
firstName == null || firstName.trim().isEmpty() ||
lastName == null || lastName.trim().isEmpty() ||
email == null || email.trim().isEmpty() ||
passwordHash == null || passwordHash.trim().isEmpty()) {
return false;
}
User user;
if (role.equalsIgnoreCase("Customer")) {
user = new Customer(0, firstName, lastName, email, passwordHash);
}
userRepository.addUser(user);
return true;
}

public boolean login(String email, char[] password) {
if (email == null || email.trim().isEmpty() || password == null) {
return false;
}
User user = userRepository.findByEmail(email);
if (user == null) {
return false;
}
String passwordString = new String(password);
if (user.verifyPassword(passwordString)) {
return true;
}
return false;
}
}

0 comments on commit c334f4e

Please sign in to comment.