-
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: merge feat/wrapper into feat/user
- Loading branch information
Showing
8 changed files
with
171 additions
and
18 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
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
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,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(); | ||
| } | ||
| } |
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,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; | ||
| } | ||
| } |
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,5 +1,11 @@ | ||
| package edu.group5.app.control; | ||
|
|
||
| public class Wrapper { | ||
|
|
||
| abstract class Wrapper { | ||
|
|
||
| protected Wrapper() { | ||
| } | ||
|
|
||
| public abstract boolean importData(); | ||
|
|
||
| public abstract Object getData(); | ||
| } |
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
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
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,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; | ||
| } | ||
| } |