61 lines
1.2 KiB
Zig
61 lines
1.2 KiB
Zig
|
const std = @import("std");
|
||
|
const util = @import("util");
|
||
|
const auth = @import("./auth.zig");
|
||
|
|
||
|
const Uuid = util.Uuid;
|
||
|
const DateTime = util.DateTime;
|
||
|
const getRandom = @import("../api.zig").getRandom;
|
||
|
|
||
|
pub const Note = struct {
|
||
|
id: Uuid,
|
||
|
|
||
|
author_id: Uuid,
|
||
|
content: []const u8,
|
||
|
created_at: DateTime,
|
||
|
};
|
||
|
|
||
|
const DbModel = struct {
|
||
|
id: Uuid,
|
||
|
|
||
|
author_id: Uuid,
|
||
|
content: []const u8,
|
||
|
created_at: DateTime,
|
||
|
};
|
||
|
|
||
|
pub fn create(
|
||
|
db: anytype,
|
||
|
author: Uuid,
|
||
|
content: []const u8,
|
||
|
) !Uuid {
|
||
|
const id = Uuid.randV4(getRandom());
|
||
|
|
||
|
try db.insert("note", .{
|
||
|
.id = id,
|
||
|
.author_id = author,
|
||
|
.content = content,
|
||
|
.created_at = DateTime.now(),
|
||
|
});
|
||
|
|
||
|
return id;
|
||
|
}
|
||
|
|
||
|
pub fn get(db: anytype, id: Uuid, alloc: std.mem.Allocator) !Note {
|
||
|
const result = (try db.execRow(
|
||
|
&.{ Uuid, []const u8, DateTime },
|
||
|
\\SELECT author_id, content, created_at
|
||
|
\\FROM note
|
||
|
\\WHERE id = ?
|
||
|
\\LIMIT 1
|
||
|
,
|
||
|
.{id},
|
||
|
alloc,
|
||
|
)) orelse return error.NotFound;
|
||
|
|
||
|
return Note{
|
||
|
.id = id,
|
||
|
.author_id = result[0],
|
||
|
.content = result[1],
|
||
|
.created_at = result[2],
|
||
|
};
|
||
|
}
|