Rework login, get account from PG
This commit is contained in:
@@ -4,11 +4,18 @@ import lombok.Builder;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author Ponk
|
||||
*/
|
||||
@Builder
|
||||
public record Account(int id, String name, String password, boolean acceptedTos, byte gender, LocalDate birthdate,
|
||||
String pin, String pic, int chrSlots, int loggedIn, LocalDateTime lastLogin, boolean banned) {
|
||||
public record Account(int id, String name, String password, boolean acceptedTos, Byte gender, LocalDate birthdate,
|
||||
String pin, String pic, byte chrSlots, byte loginState, LocalDateTime lastLogin, boolean banned,
|
||||
LocalDateTime tempBanTimestamp) {
|
||||
public Account {
|
||||
Objects.requireNonNull(name);
|
||||
Objects.requireNonNull(password);
|
||||
Objects.requireNonNull(birthdate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,24 @@ public class AccountRepository {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public Optional<Account> findByNameIgnoreCase(String name) {
|
||||
String sql = """
|
||||
SELECT id, name, password, pin, pic, birthdate, gender, tos_accepted, chr_slots, login_state,
|
||||
last_login, banned, temp_ban_timestamp
|
||||
FROM account
|
||||
WHERE lower(name) = lower(:name)""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createQuery(sql)
|
||||
.bind("name", name)
|
||||
.mapTo(Account.class)
|
||||
.findOne();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> findById(int accountId) {
|
||||
String sql = """
|
||||
SELECT id, name, password, pin, pic, logged_in, last_login, birthdate, banned, gender, tos_accepted
|
||||
SELECT id, name, password, pin, pic, birthdate, gender, tos_accepted, chr_slots, login_state,
|
||||
last_login, banned, temp_ban_timestamp
|
||||
FROM account
|
||||
WHERE id = :id""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
@@ -30,13 +45,15 @@ public class AccountRepository {
|
||||
|
||||
public Integer insert(Account account) {
|
||||
String sql = """
|
||||
INSERT INTO account (name, password, birthdate)
|
||||
VALUES (:name, :password, :birthdate)""";
|
||||
INSERT INTO account (name, password, birthdate, chr_slots, login_state)
|
||||
VALUES (:name, :password, :birthdate, :chrSlots, :loginState)""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createUpdate(sql)
|
||||
.bind("name", account.name())
|
||||
.bind("password", account.password())
|
||||
.bind("birthdate", account.birthdate())
|
||||
.bind("chrSlots", account.chrSlots())
|
||||
.bind("loginState", account.loginState())
|
||||
.executeAndReturnGeneratedKeys("id")
|
||||
.mapTo(Integer.class)
|
||||
.one();
|
||||
|
||||
@@ -21,14 +21,20 @@ public class AccountRowMapper implements RowMapper<Account> {
|
||||
.password(rs.getString("password"))
|
||||
.pin(rs.getString("pin"))
|
||||
.pic(rs.getString("pic"))
|
||||
.loggedIn(rs.getInt("logged_in"))
|
||||
.birthdate(rs.getDate("birthdate").toLocalDate())
|
||||
.gender(Optional.ofNullable(rs.getObject("gender", Short.class))
|
||||
.map(Short::byteValue)
|
||||
.orElse(null))
|
||||
.acceptedTos(rs.getBoolean("tos_accepted"))
|
||||
.chrSlots(rs.getByte("chr_slots"))
|
||||
.loginState(rs.getByte("login_state"))
|
||||
.lastLogin(Optional.ofNullable(rs.getTimestamp("last_login"))
|
||||
.map(Timestamp::toLocalDateTime)
|
||||
.orElse(null))
|
||||
.birthdate(rs.getDate("birthdate").toLocalDate())
|
||||
.banned(rs.getBoolean("banned"))
|
||||
.gender(rs.getByte("gender"))
|
||||
.acceptedTos(rs.getBoolean("tos_accepted"))
|
||||
.tempBanTimestamp(Optional.ofNullable(rs.getTimestamp("temp_ban_timestamp"))
|
||||
.map(Timestamp::toLocalDateTime)
|
||||
.orElse(null))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user