Skip to content
Snippets Groups Projects
Commit 44b5ecf2 authored by Eirik Steira's avatar Eirik Steira Committed by Mads Lundegaard
Browse files

Add docstrings to Dao interface and UserDao, add relevant null checks

Add *.properties to .gitignore
parent a3763838
No related branches found
No related tags found
1 merge request!13Weekly Merge To Master
Showing
with 329 additions and 43 deletions
......@@ -4,6 +4,7 @@
target/
.idea/
*.properties
*.iml
# User-specific stuff
.idea/**/workspace.xml
......
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="ExternalSystem" externalSystem="Maven" />
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:mac:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:mac:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:mac:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:mac:13" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.30" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:win:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:win:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:win:13" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.19" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:13" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:win:13" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.2" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -20,7 +20,7 @@ public class App extends Application {
stage.show();
}
static void setRoot(String fxml) throws IOException {
public static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
......
package NTNU.IDATT1002;
public class LoggedIn {
}
package NTNU.IDATT1002.controllers;
public class LoggedInController {
}
\ No newline at end of file
package NTNU.IDATT1002;
package NTNU.IDATT1002.controllers;
import java.io.IOException;
import NTNU.IDATT1002.App;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class PrimaryController {
public class LoginController {
@FXML
private void switchToSecondary() throws IOException {
public void switchToSecondary() throws IOException {
App.setRoot("signup");
}
......
package NTNU.IDATT1002;
package NTNU.IDATT1002.controllers;
import java.io.IOException;
import NTNU.IDATT1002.App;
import javafx.fxml.FXML;
public class SecondaryController {
public class SignUpController {
@FXML
private void switchToPrimary() throws IOException {
public void switchToPrimary() throws IOException {
App.setRoot("login");
}
}
\ No newline at end of file
package NTNU.IDATT1002.models;
public class Login {
private Integer id;
private User user;
private String password;
private String passwordHash;
private String passwordSalt;
public Login() {
}
public Login(int id, User user, String password, String passwordHash, String passwordSalt) {
this.id = id;
this.user = user;
this.password = password;
this.passwordHash = passwordHash;
this.passwordSalt = passwordSalt;
}
public int getId() {
return id;
}
public User getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getPasswordHash() {
return passwordHash;
}
public String getPasswordSalt() {
return passwordSalt;
}
}
package NTNU.IDATT1002.models;
import java.util.Date;
public class User {
private Integer id;
private String email;
private String username;
private String firstName;
private String lastName;
private String callingCode;
private String phoneNumber;
private Date birthDate;
private boolean isAdmin;
private boolean isActive;
public User(int id, String email, String username, String firstName, String lastName, String callingCode, String phoneNumber, Date birthDate, boolean isAdmin) {
this.id = id;
this.email = email;
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.callingCode = callingCode;
this.phoneNumber = phoneNumber;
this.birthDate = birthDate;
this.isAdmin = isAdmin;
this.isActive = true;
}
public User(User user) {
this(user.getId(),
user.getEmail(),
user.getUsername(),
user.getFirstName(),
user.getLastName(),
user.getCallingCode(),
user.getPhoneNumber(),
user.getBirthDate(),
user.isAdmin());
}
public Integer getId() {
return id;
}
public String getEmail() {
return email;
}
public String getUsername() {
return username;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getCallingCode() {
return callingCode;
}
public String getPhoneNumber() {
return phoneNumber;
}
public Date getBirthDate() {
return birthDate;
}
public boolean isAdmin() {
return isAdmin;
}
public boolean isActive() {
return isActive;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id.equals(user.id);
}
}
package NTNU.IDATT1002.repository;
import java.util.Optional;
/**
* Entity Repository Interface Dummy. Supports regular Create, Read, Update and Delete operations.
* @param <T> type of entity
* @param <ID> type of entity id
*/
public interface Repository<T, ID> {
/**
* Saves a given entity and returns the saved instance.
*
* @param entity not null
* @return the saved entity
*/
T save(T entity) throws IllegalArgumentException;
/**
* Updates the given entity. This will completely override the given instance.
*
* @param entity not null
* @return the updated entity
*/
Optional<T> update(T entity);
/**
* Retrieves all instances of the type.
*
* @return all entities
*/
Iterable<T> findAll();
/**
* Retrieves an entity with the given id.
*
* @param id not null
* @return the entity with the given id if found, else Optional.empty()
*/
Optional<T> findById(ID id);
/**
* Deletes an entity with the given id.
*
* @param id not null
*/
void deleteById(ID id);
/**
* Deletes the given entity.
*
* @param entity not null
*/
void delete(T entity);
/**
* Return the number of entities.
*
* @return the number of entities.
*/
long count();
/**
* Return whether the given entity exists.
*
* @param entity not null
* @return true if the entity exist, else false
*/
boolean exists(T entity);
}
package NTNU.IDATT1002.repository;
import NTNU.IDATT1002.models.User;
import java.util.*;
/**
* User repository to support interacting with a simulated database.
*/
public class UserRepository implements Repository<User, Integer> {
/**
* The "database".
*/
private static List<User> users = new ArrayList<>();
/**
* Supply repository with initial test data.
*/
static {
users.add(new User(1, "test@mail.com", "test", "Test", "Testesen", "+47", "00000000", new Date(), false));
users.add(new User(2, "test2@mail.com", "test2", "Test2", "Testesen2", "+47", "00000001", new Date(), false));
users.add(new User(3, "test3@mail.com", "test3", "Test3", "Testesen3", "+47", "00000002", new Date(), false));
}
@Override
public User save(User user) throws IllegalArgumentException {
if (user == null)
throw new IllegalArgumentException("User cannot be null");
User savedUser = new User(user);
users.add(savedUser);
return savedUser;
}
@Override
public Optional<User> update(User user) {
if (user == null)
throw new IllegalArgumentException("User cannot be null");
Optional<User> foundUser = users.stream()
.filter(user::equals)
.findFirst();
if (foundUser.isPresent()) {
delete(foundUser.get());
User updatedUser = save(user);
return Optional.of(updatedUser);
}
return Optional.empty();
}
@Override
public List<User> findAll() {
return users;
}
@Override
public Optional<User> findById(Integer id) {
return users.stream()
.filter(user -> user.getId() == id)
.findFirst();
}
public User findByUsername(String username) {
return users.stream()
.filter(user -> user.getUsername().equals(username))
.findFirst()
.orElseThrow(NoSuchElementException::new);
}
@Override
public void deleteById(Integer id) {
Optional<User> foundUser = findById(id);
foundUser.ifPresent(this::delete);
}
@Override
public void delete(User user) {
users.remove(user);
}
@Override
public long count() {
return users.size();
}
@Override
public boolean exists(User user) {
if (user == null)
throw new IllegalArgumentException("User cannot be null");
return users.stream()
.anyMatch(user::equals);
}
}
......@@ -4,7 +4,7 @@
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="NTNU.IDATT1002.LoggedIn">
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="NTNU.IDATT1002.controllers.LoggedInController">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="200.0" layoutY="125.0" pickOnBounds="true" preserveRatio="true">
<image>
......
......@@ -11,7 +11,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox alignment="CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="NTNU.IDATT1002.PrimaryController">
<VBox alignment="CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="NTNU.IDATT1002.controllers.LoginController">
<children>
<Label text="Login Page" />
<GridPane hgap="10.0" maxWidth="300.0" prefHeight="90.0" prefWidth="200.0" vgap="10.0">
......
......@@ -11,7 +11,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox alignment="CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/" fx:controller="NTNU.IDATT1002.SecondaryController">
<VBox alignment="CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/" fx:controller="NTNU.IDATT1002.controllers.SignUpController">
<children>
<Label text="Sign Up page" />
<GridPane hgap="10.0" maxWidth="-Infinity" prefHeight="110.0" prefWidth="300.0" vgap="10.0">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment