init
This commit is contained in:
86
src/Account.cs
Normal file
86
src/Account.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using Npgsql;
|
||||
|
||||
public class Account {
|
||||
NpgsqlConnection _connection = new();
|
||||
public KUID KUID { get; private set; } = new();
|
||||
|
||||
|
||||
public static Account NewAccount(NpgsqlConnection connection, string name) {
|
||||
KUID kuid = new("special placeholder");
|
||||
|
||||
NpgsqlCommand command = new("""
|
||||
INSERT INTO accounts VALUES (
|
||||
($1),
|
||||
($2)
|
||||
)
|
||||
""", connection) {
|
||||
Parameters = {
|
||||
new NpgsqlParameter<string>() { Value = kuid.Text },
|
||||
new NpgsqlParameter<string>() { Value = name }
|
||||
}
|
||||
};
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
return AccountFromKUID(connection, kuid);
|
||||
}
|
||||
|
||||
public static Account AccountFromKUID(NpgsqlConnection connection, KUID kuid) {
|
||||
return new() {
|
||||
_connection = connection,
|
||||
KUID = kuid
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public void AddPoki(string[] names, int[]? parentIDs = null, string[]? tags = null) {
|
||||
NpgsqlCommand command = new("""
|
||||
INSERT INTO poki VALUES (
|
||||
($1),
|
||||
DEFAULT,
|
||||
($2),
|
||||
($3),
|
||||
DEFAULT, DEFAULT, DEFAULT,
|
||||
($4)
|
||||
)
|
||||
""", _connection) {
|
||||
Parameters = {
|
||||
new() { Value = KUID.Text },
|
||||
new() { Value = names },
|
||||
new() { Value = (parentIDs == null ? DBNull.Value : parentIDs) },
|
||||
new() { Value = (tags == null ? DBNull.Value : tags) },
|
||||
}
|
||||
};
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
if (parentIDs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
command = new("""
|
||||
UPDATE poki
|
||||
SET child_poki = child_poki || currval('poki_id_seq')
|
||||
WHERE id = ANY($1)
|
||||
""", _connection) {
|
||||
Parameters = {
|
||||
new() { Value = parentIDs }
|
||||
}
|
||||
};
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void GetPokiByName(string name) {
|
||||
NpgsqlCommand command = new("""
|
||||
SELECT id
|
||||
FROM poki
|
||||
WHERE ($1) IN names
|
||||
LIMIT 1
|
||||
""", _connection) {
|
||||
Parameters = {
|
||||
new() { Value = name }
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user