Create putNote

This commit is contained in:
jaina heartles 2022-07-10 17:58:39 -07:00
parent b3a3a66068
commit d8f8a3ec89

View file

@ -47,7 +47,7 @@ pub const Database = struct {
.notes = std.StringHashMap(models.Note).init(alloc), .notes = std.StringHashMap(models.Note).init(alloc),
}; };
try db.notes.put("1", models.Note{ try db.putNote(models.Note{
.id = "1", .id = "1",
.content = "abcd", .content = "abcd",
}); });
@ -55,10 +55,21 @@ pub const Database = struct {
return db; return db;
} }
pub fn containsNote(self: *Database, id: Id) !bool {
return self.notes.contains(id);
}
pub fn getNote(self: *Database, id: Id, alloc: std.mem.Allocator) !?models.Note { pub fn getNote(self: *Database, id: Id, alloc: std.mem.Allocator) !?models.Note {
const note = self.notes.get(id) orelse return null; const note = self.notes.get(id) orelse return null;
return try clone(alloc, note); return try clone(alloc, note);
} }
pub fn putNote(self: *Database, note: models.Note) !void {
if (try self.containsNote(note.id)) unreachable;
const copy = try clone(self.internal_alloc, note);
try self.notes.put(copy.id, copy);
}
}; };
test "clone" { test "clone" {