Add id and created_at fields to db models
This commit is contained in:
parent
dc8877eef3
commit
ab61efc44a
3 changed files with 35 additions and 15 deletions
|
@ -127,7 +127,8 @@ pub const ApiServer = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register(self: *ApiServer, info: RegistrationInfo) !models.Actor {
|
pub fn register(self: *ApiServer, info: RegistrationInfo) !models.Actor {
|
||||||
const id = Uuid.randV4(self.prng.random());
|
const actor_id = Uuid.randV4(self.prng.random());
|
||||||
|
const user_id = Uuid.randV4(self.prng.random());
|
||||||
// TODO: transaction?
|
// TODO: transaction?
|
||||||
|
|
||||||
if (try self.db.existsWhereEq(models.LocalUser, .username, info.username)) {
|
if (try self.db.existsWhereEq(models.LocalUser, .username, info.username)) {
|
||||||
|
@ -144,12 +145,13 @@ pub const ApiServer = struct {
|
||||||
const now = DateTime.now();
|
const now = DateTime.now();
|
||||||
|
|
||||||
const actor = models.Actor{
|
const actor = models.Actor{
|
||||||
.id = id,
|
.id = actor_id,
|
||||||
.handle = info.username,
|
.handle = info.username,
|
||||||
.created_at = now,
|
.created_at = now,
|
||||||
};
|
};
|
||||||
const user = models.LocalUser{
|
const user = models.LocalUser{
|
||||||
.actor_id = id,
|
.id = user_id,
|
||||||
|
.actor_id = actor_id,
|
||||||
.username = info.username,
|
.username = info.username,
|
||||||
.email = info.email,
|
.email = info.email,
|
||||||
.hashed_password = hash,
|
.hashed_password = hash,
|
||||||
|
@ -191,7 +193,8 @@ pub const ApiServer = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn react(self: *ApiServer, note_id: Uuid, ctx: ApiContext) !void {
|
pub fn react(self: *ApiServer, note_id: Uuid, ctx: ApiContext) !void {
|
||||||
try self.db.insert(models.Reaction, .{ .note_id = note_id, .reactor_id = ctx.user_context.user.actor_id.? });
|
const id = Uuid.randV4(self.prng.random());
|
||||||
|
try self.db.insert(models.Reaction, .{ .id = id, .note_id = note_id, .reactor_id = ctx.user_context.user.actor_id.?, .created_at = DateTime.now() });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listReacts(self: *ApiServer, note_id: Uuid, ctx: ApiContext) ![]models.Reaction {
|
pub fn listReacts(self: *ApiServer, note_id: Uuid, ctx: ApiContext) ![]models.Reaction {
|
||||||
|
|
|
@ -104,15 +104,18 @@ pub const Database = struct {
|
||||||
const init_sql_stmts = [_][]const u8{
|
const init_sql_stmts = [_][]const u8{
|
||||||
\\CREATE TABLE IF NOT EXISTS
|
\\CREATE TABLE IF NOT EXISTS
|
||||||
\\actor(
|
\\actor(
|
||||||
\\ id TEXT,
|
\\ id TEXT NOT NULL,
|
||||||
|
\\
|
||||||
\\ handle TEXT NOT NULL,
|
\\ handle TEXT NOT NULL,
|
||||||
\\ created_at INTEGER NOT NULL,
|
\\ created_at INTEGER NOT NULL,
|
||||||
\\
|
\\
|
||||||
\\ PRIMARY KEY (id)
|
\\ PRIMARY KEY(id)
|
||||||
\\) STRICT;
|
\\) STRICT;
|
||||||
,
|
,
|
||||||
\\CREATE TABLE IF NOT EXISTS
|
\\CREATE TABLE IF NOT EXISTS
|
||||||
\\local_user(
|
\\local_user(
|
||||||
|
\\ id TEXT NOT NULL,
|
||||||
|
\\
|
||||||
\\ username TEXT NOT NULL,
|
\\ username TEXT NOT NULL,
|
||||||
\\ actor_id TEXT,
|
\\ actor_id TEXT,
|
||||||
\\ email TEXT,
|
\\ email TEXT,
|
||||||
|
@ -122,30 +125,35 @@ pub const Database = struct {
|
||||||
\\ password_changed_at INTEGER NOT NULL,
|
\\ password_changed_at INTEGER NOT NULL,
|
||||||
\\
|
\\
|
||||||
\\ UNIQUE(actor_id),
|
\\ UNIQUE(actor_id),
|
||||||
\\ FOREIGN KEY (actor_id) REFERENCES actor(id),
|
\\ FOREIGN KEY(actor_id) REFERENCES actor(id),
|
||||||
\\
|
\\
|
||||||
\\ PRIMARY KEY (username)
|
\\ PRIMARY KEY(id)
|
||||||
\\) STRICT;
|
\\) STRICT;
|
||||||
,
|
,
|
||||||
\\CREATE TABLE IF NOT EXISTS
|
\\CREATE TABLE IF NOT EXISTS
|
||||||
\\note(
|
\\note(
|
||||||
\\ id TEXT PRIMARY KEY,
|
\\ id TEXT NOT NULL,
|
||||||
|
\\
|
||||||
\\ content TEXT NOT NULL,
|
\\ content TEXT NOT NULL,
|
||||||
\\ author_id TEXT NOT NULL,
|
\\ author_id TEXT NOT NULL,
|
||||||
\\ created_at INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
\\ created_at INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
\\
|
\\
|
||||||
\\ FOREIGN KEY (author_id) REFERENCES actor(id)
|
\\ FOREIGN KEY(author_id) REFERENCES actor(id),
|
||||||
|
\\
|
||||||
|
\\ PRIMARY KEY(id)
|
||||||
\\) STRICT;
|
\\) STRICT;
|
||||||
,
|
,
|
||||||
\\CREATE TABLE IF NOT EXISTS
|
\\CREATE TABLE IF NOT EXISTS
|
||||||
\\reaction(
|
\\reaction(
|
||||||
|
\\ id TEXT NOT NULL,
|
||||||
|
\\
|
||||||
\\ reactor_id TEXT NOT NULL,
|
\\ reactor_id TEXT NOT NULL,
|
||||||
\\ note_id TEXT NOT NULL,
|
\\ note_id TEXT NOT NULL,
|
||||||
\\
|
\\
|
||||||
\\ FOREIGN KEY(reactor_id) REFERENCES actor(id),
|
\\ FOREIGN KEY(reactor_id) REFERENCES actor(id),
|
||||||
\\ FOREIGN KEY(note_id) REFERENCES note(id),
|
\\ FOREIGN KEY(note_id) REFERENCES note(id),
|
||||||
\\
|
\\
|
||||||
\\ PRIMARY KEY(reactor_id, note_id)
|
\\ PRIMARY KEY(id)
|
||||||
\\) STRICT;
|
\\) STRICT;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,15 @@ const util = @import("util");
|
||||||
const Uuid = util.Uuid;
|
const Uuid = util.Uuid;
|
||||||
const DateTime = util.DateTime;
|
const DateTime = util.DateTime;
|
||||||
|
|
||||||
|
// Used for documentation purposes
|
||||||
|
fn Ref(comptime _: type) type {
|
||||||
|
return Uuid;
|
||||||
|
}
|
||||||
|
|
||||||
pub const Note = struct {
|
pub const Note = struct {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
content: []const u8,
|
content: []const u8,
|
||||||
author_id: Uuid,
|
author_id: Ref(Actor),
|
||||||
|
|
||||||
created_at: DateTime,
|
created_at: DateTime,
|
||||||
};
|
};
|
||||||
|
@ -20,7 +25,8 @@ pub const Actor = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const LocalUser = struct {
|
pub const LocalUser = struct {
|
||||||
actor_id: ?Uuid,
|
id: Uuid,
|
||||||
|
actor_id: ?Ref(Actor),
|
||||||
|
|
||||||
username: []const u8,
|
username: []const u8,
|
||||||
email: ?[]const u8,
|
email: ?[]const u8,
|
||||||
|
@ -32,6 +38,9 @@ pub const LocalUser = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Reaction = struct {
|
pub const Reaction = struct {
|
||||||
reactor_id: Uuid,
|
id: Uuid,
|
||||||
note_id: Uuid,
|
reactor_id: Ref(Actor),
|
||||||
|
note_id: Ref(Note),
|
||||||
|
|
||||||
|
created_at: DateTime,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue