Build Database connection

This commit is contained in:
jaina heartles 2022-07-10 17:07:15 -07:00
parent 9c2bd11526
commit c130391186
2 changed files with 17 additions and 6 deletions

View File

@ -36,11 +36,20 @@ pub fn free(alloc: std.mem.Allocator, val: anytype) void {
} }
} }
pub fn getNote(alloc: std.mem.Allocator) !models.Note { pub const Database = struct {
return try clone(alloc, models.Note{ //internal_alloc: std.mem.Allocator,
.content = "abcd", //notes: std.StringHashMap(models.Note),
});
} pub fn init() Database {
return Database{};
}
pub fn getNote(_: *Database, alloc: std.mem.Allocator) !models.Note {
return try clone(alloc, models.Note{
.content = "abcd",
});
}
};
test "clone" { test "clone" {
const T = struct { const T = struct {

View File

@ -45,7 +45,7 @@ fn respondJson(ctx: *http.server.Context, value: anytype, alloc: std.mem.Allocat
} }
fn getNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { fn getNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const note = try db.getNote(srv.alloc); const note = try srv.db.getNote(srv.alloc);
defer db.free(srv.alloc, note); defer db.free(srv.alloc, note);
try respondJson(ctx, note, srv.alloc); try respondJson(ctx, note, srv.alloc);
@ -57,10 +57,12 @@ fn healthcheck(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !vo
const RequestServer = struct { const RequestServer = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
db: db.Database,
fn init(alloc: std.mem.Allocator) RequestServer { fn init(alloc: std.mem.Allocator) RequestServer {
return RequestServer{ return RequestServer{
.alloc = alloc, .alloc = alloc,
.db = db.Database.init(),
}; };
} }