Auto-create account in both MySQL and PG

This commit is contained in:
P0nk
2024-09-26 07:59:27 +02:00
parent bf9c02bc16
commit 647e67f6e8
13 changed files with 158 additions and 49 deletions

View File

@@ -0,0 +1,10 @@
package database.account;
import lombok.Builder;
import java.time.LocalDate;
@Builder
public record Account(String name, String password, boolean acceptedTos, LocalDate birthdate, String pin, String pic,
int loggedIn) {
}

View File

@@ -0,0 +1,31 @@
package database.account;
import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle;
public class AccountRepository {
private final PgDatabaseConnection connection;
public AccountRepository(PgDatabaseConnection connection) {
this.connection = connection;
}
public Account getByName(String name) {
return null; // TODO
}
public Integer insert(Account account) {
String sql = """
INSERT INTO account (name, password, birthdate)
VALUES (:name, :password, :birthdate)""";
try (Handle handle = connection.getHandle()) {
return handle.createUpdate(sql)
.bind("name", account.name())
.bind("password", account.password())
.bind("birthdate", account.birthdate())
.executeAndReturnGeneratedKeys("id")
.mapTo(Integer.class)
.one();
}
}
}

View File

@@ -64,6 +64,7 @@ public class CharacterSaver {
try (Handle handle = pgConnection.getHandle()) {
handle.useTransaction(h -> doPostgresSave(h, chr));
} catch (Exception e) {
System.err.println("Error saving chr to PG: " + e.getMessage());
log.error("Error saving chr {} to PG", chr.getName(), e);
}