Retrieve note dynamically

This commit is contained in:
jaina heartles 2022-07-10 17:12:52 -07:00
parent 80c94711ae
commit 5933e37f85
2 changed files with 17 additions and 10 deletions

View file

@ -37,18 +37,25 @@ pub fn free(alloc: std.mem.Allocator, val: anytype) void {
}
pub const Database = struct {
//internal_alloc: std.mem.Allocator,
//notes: std.StringHashMap(models.Note),
internal_alloc: std.mem.Allocator,
notes: std.StringHashMap(models.Note),
pub fn init() Database {
return Database{};
}
pub fn init(alloc: std.mem.Allocator) !Database {
var db = Database{
.internal_alloc = alloc,
.notes = std.StringHashMap(models.Note).init(alloc),
};
pub fn getNote(_: *Database, alloc: std.mem.Allocator) !models.Note {
return try clone(alloc, models.Note{
try db.notes.put("1", models.Note{
.id = "1",
.content = "abcd",
});
return db;
}
pub fn getNote(self: *Database, alloc: std.mem.Allocator) !models.Note {
return try clone(alloc, self.notes.get("1").?);
}
};

View file

@ -59,10 +59,10 @@ const RequestServer = struct {
alloc: std.mem.Allocator,
db: db.Database,
fn init(alloc: std.mem.Allocator) RequestServer {
fn init(alloc: std.mem.Allocator) !RequestServer {
return RequestServer{
.alloc = alloc,
.db = db.Database.init(),
.db = try db.Database.init(alloc),
};
}
@ -85,6 +85,6 @@ const RequestServer = struct {
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var srv = RequestServer.init(gpa.allocator());
var srv = try RequestServer.init(gpa.allocator());
srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
}