Build Database connection
This commit is contained in:
parent
9c2bd11526
commit
c130391186
2 changed files with 17 additions and 6 deletions
|
@ -36,11 +36,20 @@ pub fn free(alloc: std.mem.Allocator, val: anytype) void {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn getNote(alloc: std.mem.Allocator) !models.Note {
|
||||
return try clone(alloc, models.Note{
|
||||
.content = "abcd",
|
||||
});
|
||||
}
|
||||
pub const Database = struct {
|
||||
//internal_alloc: std.mem.Allocator,
|
||||
//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" {
|
||||
const T = struct {
|
||||
|
|
|
@ -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 {
|
||||
const note = try db.getNote(srv.alloc);
|
||||
const note = try srv.db.getNote(srv.alloc);
|
||||
defer db.free(srv.alloc, note);
|
||||
|
||||
try respondJson(ctx, note, srv.alloc);
|
||||
|
@ -57,10 +57,12 @@ fn healthcheck(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !vo
|
|||
|
||||
const RequestServer = struct {
|
||||
alloc: std.mem.Allocator,
|
||||
db: db.Database,
|
||||
|
||||
fn init(alloc: std.mem.Allocator) RequestServer {
|
||||
return RequestServer{
|
||||
.alloc = alloc,
|
||||
.db = db.Database.init(),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue