Add users
This commit is contained in:
parent
b3c9c3b600
commit
16d093b4f7
4 changed files with 52 additions and 7 deletions
|
@ -63,6 +63,16 @@ pub const ApiServer = struct {
|
|||
return note;
|
||||
}
|
||||
|
||||
pub fn createUser(self: *ApiServer, info: CreateInfo(models.User)) !models.User {
|
||||
const id = Uuid.randV4(self.prng.random());
|
||||
// TODO: check for dupes
|
||||
|
||||
const user = reify(models.User, id, info);
|
||||
try self.db.putUser(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
pub fn getNote(self: *ApiServer, id: Uuid, alloc: std.mem.Allocator) !?models.Note {
|
||||
return self.db.getNote(id, alloc);
|
||||
}
|
||||
|
|
|
@ -51,18 +51,15 @@ pub fn free(alloc: std.mem.Allocator, val: anytype) void {
|
|||
pub const Database = struct {
|
||||
internal_alloc: std.mem.Allocator,
|
||||
notes: std.AutoHashMap(Uuid, models.Note),
|
||||
users: std.AutoHashMap(Uuid, models.User),
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator) !Database {
|
||||
var db = Database{
|
||||
.internal_alloc = alloc,
|
||||
.notes = std.AutoHashMap(Uuid, models.Note).init(alloc),
|
||||
.users = std.AutoHashMap(Uuid, models.User).init(alloc),
|
||||
};
|
||||
|
||||
try db.putNote(models.Note{
|
||||
.id = Uuid{ .data = @bitCast([16]u8, @as(u128, 1)) },
|
||||
.content = "abcd",
|
||||
});
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
|
@ -97,6 +94,29 @@ pub const Database = struct {
|
|||
}
|
||||
try self.notes.put(key, copy);
|
||||
}
|
||||
|
||||
pub fn containsUser(self: *Database, id: Uuid) !bool {
|
||||
return self.users.contains(id);
|
||||
}
|
||||
|
||||
// returns a copy of the note data from storage. memory is allocated with the provided
|
||||
// allocator. can be freed using free() above
|
||||
pub fn getUser(self: *Database, id: Uuid, alloc: std.mem.Allocator) !?models.User {
|
||||
const note = self.users.get(id) orelse return null;
|
||||
return try clone(alloc, note);
|
||||
}
|
||||
|
||||
pub fn putUser(self: *Database, note: models.User) !void {
|
||||
const copy = try clone(self.internal_alloc, note);
|
||||
errdefer free(self.internal_alloc, copy);
|
||||
|
||||
const key = copy.id;
|
||||
|
||||
if (self.users.fetchRemove(key)) |e| {
|
||||
free(self.internal_alloc, e.value);
|
||||
}
|
||||
try self.users.put(key, copy);
|
||||
}
|
||||
};
|
||||
|
||||
test "clone" {
|
||||
|
|
|
@ -14,8 +14,9 @@ const RouteArgs = http.RouteArgs;
|
|||
const router = Router{
|
||||
.routes = &[_]Route{
|
||||
Route.new(.GET, "/healthcheck", healthcheck),
|
||||
Route.new(.GET, "/:id", getNote),
|
||||
Route.new(.POST, "/", createNote),
|
||||
Route.new(.GET, "/notes/:id", getNote),
|
||||
Route.new(.POST, "/notes", createNote),
|
||||
Route.new(.POST, "/users", createUser),
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -70,6 +71,15 @@ fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !voi
|
|||
try respondJson(ctx, .created, note, srv.alloc);
|
||||
}
|
||||
|
||||
fn createUser(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
|
||||
const info = try parseRequestBody(api.CreateInfo(models.User), ctx, srv.alloc);
|
||||
defer freeRequestBody(info, srv.alloc);
|
||||
|
||||
const user = try srv.api.createUser(info);
|
||||
|
||||
try respondJson(ctx, .created, user, srv.alloc);
|
||||
}
|
||||
|
||||
fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
|
||||
const id_str = args.get("id") orelse return error.NotFound;
|
||||
const id = Uuid.parse(id_str) catch return respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc);
|
||||
|
|
|
@ -4,3 +4,8 @@ pub const Note = struct {
|
|||
id: Uuid,
|
||||
content: []const u8,
|
||||
};
|
||||
|
||||
pub const User = struct {
|
||||
id: Uuid,
|
||||
handle: []const u8,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue