Save accepted ToS to PG
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package database;
|
||||
|
||||
import database.account.AccountRowMapper;
|
||||
import database.drop.GlobalMonsterDropRowMapper;
|
||||
import database.drop.MonsterDropRowMapper;
|
||||
import database.maker.MakerIngredientRowMapper;
|
||||
@@ -26,6 +27,7 @@ public final class JdbiConfig {
|
||||
|
||||
private static List<RowMapper<?>> rowMappers() {
|
||||
return List.of(
|
||||
new AccountRowMapper(),
|
||||
new NoteRowMapper(),
|
||||
new MakerReagentRowMapper(),
|
||||
new MakerRecipeRowMapper(),
|
||||
|
||||
@@ -3,8 +3,12 @@ package database.account;
|
||||
import lombok.Builder;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author Ponk
|
||||
*/
|
||||
@Builder
|
||||
public record Account(String name, String password, boolean acceptedTos, LocalDate birthdate, String pin, String pic,
|
||||
int loggedIn) {
|
||||
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) {
|
||||
}
|
||||
|
||||
@@ -3,6 +3,11 @@ package database.account;
|
||||
import database.PgDatabaseConnection;
|
||||
import org.jdbi.v3.core.Handle;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author Ponk
|
||||
*/
|
||||
public class AccountRepository {
|
||||
private final PgDatabaseConnection connection;
|
||||
|
||||
@@ -10,8 +15,17 @@ public class AccountRepository {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public Account getByName(String name) {
|
||||
return null; // TODO
|
||||
public Optional<Account> findById(int accountId) {
|
||||
String sql = """
|
||||
SELECT id, name, password, pin, pic, logged_in, last_login, birthdate, banned, gender, tos_accepted
|
||||
FROM account
|
||||
WHERE id = :id""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createQuery(sql)
|
||||
.bind("id", accountId)
|
||||
.mapTo(Account.class)
|
||||
.findOne();
|
||||
}
|
||||
}
|
||||
|
||||
public Integer insert(Account account) {
|
||||
@@ -28,4 +42,17 @@ public class AccountRepository {
|
||||
.one();
|
||||
}
|
||||
}
|
||||
|
||||
public void setTos(int accountId, boolean acceptedTos) {
|
||||
String sql = """
|
||||
UPDATE account
|
||||
SET tos_accepted = :acceptedTos
|
||||
WHERE id = :id""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
handle.createUpdate(sql)
|
||||
.bind("id", accountId)
|
||||
.bind("acceptedTos", acceptedTos)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
src/main/java/database/account/AccountRowMapper.java
Normal file
34
src/main/java/database/account/AccountRowMapper.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package database.account;
|
||||
|
||||
import org.jdbi.v3.core.mapper.RowMapper;
|
||||
import org.jdbi.v3.core.statement.StatementContext;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author Ponk
|
||||
*/
|
||||
public class AccountRowMapper implements RowMapper<Account> {
|
||||
|
||||
@Override
|
||||
public Account map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||
return Account.builder()
|
||||
.id(rs.getInt("id"))
|
||||
.name(rs.getString("name"))
|
||||
.password(rs.getString("password"))
|
||||
.pin(rs.getString("pin"))
|
||||
.pic(rs.getString("pic"))
|
||||
.loggedIn(rs.getInt("logged_in"))
|
||||
.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"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user