+ database methods for user
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
07924cfbdf
commit
5e21307980
7
pom.xml
7
pom.xml
@ -154,6 +154,13 @@
|
|||||||
<version>2.4.1</version>
|
<version>2.4.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mindrot</groupId>
|
||||||
|
<artifactId>jbcrypt</artifactId>
|
||||||
|
<version>0.3m</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--JUnit-->
|
<!--JUnit-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
@ -32,11 +32,15 @@ public class Database {
|
|||||||
final String tableAuthorBook = "author_book";
|
final String tableAuthorBook = "author_book";
|
||||||
final String tablePublisher = "publisher";
|
final String tablePublisher = "publisher";
|
||||||
final String tableCategory = "category";
|
final String tableCategory = "category";
|
||||||
|
final String tableUser = "user";
|
||||||
|
final String tableUserAddress = "user_address";
|
||||||
|
final String tableAddress = "address";
|
||||||
|
|
||||||
private AuthorDB authorDB;
|
private AuthorDB authorDB;
|
||||||
private BookDB bookDB;
|
private BookDB bookDB;
|
||||||
private CategoryDB categoryDB;
|
private CategoryDB categoryDB;
|
||||||
private PublisherDB publisherDB;
|
private PublisherDB publisherDB;
|
||||||
|
private UserDB userDB;
|
||||||
|
|
||||||
private static Database instance;
|
private static Database instance;
|
||||||
|
|
||||||
@ -124,6 +128,12 @@ public class Database {
|
|||||||
return publisherDB;
|
return publisherDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UserDB user() {
|
||||||
|
if(userDB == null)
|
||||||
|
userDB = new UserDB(this);
|
||||||
|
return userDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
89
src/main/java/de/hsel/itech/db/UserDB.java
Normal file
89
src/main/java/de/hsel/itech/db/UserDB.java
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package de.hsel.itech.db;
|
||||||
|
|
||||||
|
import de.hsel.itech.db.pojo.User;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.mindrot.jbcrypt.BCrypt;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Johannes Theiner
|
||||||
|
* @version 0.1
|
||||||
|
* @since 0.5
|
||||||
|
**/
|
||||||
|
public class UserDB {
|
||||||
|
|
||||||
|
private Database database;
|
||||||
|
|
||||||
|
private final int logRounds = 11;
|
||||||
|
|
||||||
|
UserDB(Database database) {
|
||||||
|
this.database = database;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public User get(long id) {
|
||||||
|
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableUser, id);
|
||||||
|
assert entry != null;
|
||||||
|
|
||||||
|
ResultSet rs = entry.getKey();
|
||||||
|
try {
|
||||||
|
return new User(rs.getString("email"), rs.getString("name"), rs.getString("password"), rs.getShort("type"));
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
entry.getValue().close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean verify(@NotNull User user, @NotNull String password) {
|
||||||
|
return verifyHash(password, user.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int insert(@NotNull User user) {
|
||||||
|
user.setPassword(hash(user.getPassword()));
|
||||||
|
|
||||||
|
|
||||||
|
Connection connection = database.getConnection();
|
||||||
|
int updateCount = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert connection != null;
|
||||||
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO user (name, email, password, type) values (?, ?, ?, ?)");
|
||||||
|
statement.setString(1, user.getName());
|
||||||
|
statement.setString(2, user.getEmail());
|
||||||
|
statement.setString(3, user.getEmail());
|
||||||
|
statement.setShort(4, user.getType());
|
||||||
|
|
||||||
|
updateCount += statement.executeUpdate();
|
||||||
|
connection.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return updateCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int delete(@NotNull User user) {
|
||||||
|
return database.delete(user.getId(), database.tableUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String hash(@NotNull String password) {
|
||||||
|
return BCrypt.hashpw(password, BCrypt.gensalt(logRounds));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean verifyHash(@NotNull String plaintext, @NotNull String hash) {
|
||||||
|
return BCrypt.checkpw(plaintext, hash);
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class Address {
|
public class Address {
|
||||||
|
|
||||||
@NonNull private Customer customer;
|
@NonNull private User user;
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private String name;
|
@NonNull private String name;
|
||||||
@NonNull private String street;
|
@NonNull private String street;
|
||||||
|
@ -21,7 +21,7 @@ import java.time.LocalDate;
|
|||||||
public class CreditCardPayment {
|
public class CreditCardPayment {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private Customer customer;
|
@NonNull private User user;
|
||||||
@NonNull private int number;
|
@NonNull private int number;
|
||||||
@NonNull private String owner;
|
@NonNull private String owner;
|
||||||
@NonNull private LocalDate expiration;
|
@NonNull private LocalDate expiration;
|
||||||
|
@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class DebitCardPayment {
|
public class DebitCardPayment {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private Customer customer;
|
@NonNull private User user;
|
||||||
@NonNull private String iban;
|
@NonNull private String iban;
|
||||||
@NonNull private String bic;
|
@NonNull private String bic;
|
||||||
@NonNull private String owner;
|
@NonNull private String owner;
|
||||||
|
@ -19,5 +19,5 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class InvoicePayment {
|
public class InvoicePayment {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private Customer customer;
|
@NonNull private User user;
|
||||||
}
|
}
|
@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class PayPalPayment {
|
public class PayPalPayment {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private Customer customer;
|
@NonNull private User user;
|
||||||
@NonNull private String email;
|
@NonNull private String email;
|
||||||
@NonNull private String authCode;
|
@NonNull private String authCode;
|
||||||
}
|
}
|
@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class ShoppingCart {
|
public class ShoppingCart {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private Customer customer;
|
@NonNull private User user;
|
||||||
@NonNull private Book article;
|
@NonNull private Book article;
|
||||||
@NonNull private int count;
|
@NonNull private int count;
|
||||||
}
|
}
|
@ -6,7 +6,7 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POJO for Customer.
|
* POJO for User.
|
||||||
*
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
@ -16,11 +16,11 @@ import lombok.RequiredArgsConstructor;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class Customer {
|
public class User {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@NonNull private String email;
|
@NonNull private String email;
|
||||||
@NonNull private String name;
|
@NonNull private String name;
|
||||||
@NonNull private String password;
|
@NonNull private String password;
|
||||||
@NonNull private boolean admin;
|
@NonNull private short type;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user