milestone 18: collapse duplicated infrastructure into shared listener core, crud list helper, resource shells, transport race, name and line helpers, ui modules
This commit is contained in:
@@ -87,10 +87,7 @@ pub fn applyCreate(
|
||||
arena: Allocator,
|
||||
item: model.BlocklistSource,
|
||||
) error{OutOfMemory}!Created {
|
||||
const database = switch (mutations.configDb(state)) {
|
||||
.database => |value| value,
|
||||
.fail => |failure| return .{ .fail = failure },
|
||||
};
|
||||
const database = mutations.requireConfigDb(state) catch return .{ .fail = mutations.no_config_db };
|
||||
if (try mutations.checkSource(arena, item)) |problem| return .{ .fail = .{ .invalid = problem } };
|
||||
|
||||
state.config_lock.lockUncancelable(io);
|
||||
@@ -109,10 +106,7 @@ pub fn applyUpdate(
|
||||
id: i64,
|
||||
item: model.BlocklistSource,
|
||||
) error{OutOfMemory}!?Failure {
|
||||
const database = switch (mutations.configDb(state)) {
|
||||
.database => |value| value,
|
||||
.fail => |failure| return failure,
|
||||
};
|
||||
const database = mutations.requireConfigDb(state) catch return mutations.no_config_db;
|
||||
if (try mutations.checkSource(arena, item)) |problem| return .{ .invalid = problem };
|
||||
|
||||
state.config_lock.lockUncancelable(io);
|
||||
@@ -124,10 +118,7 @@ pub fn applyUpdate(
|
||||
}
|
||||
|
||||
pub fn applyDelete(state: *server.WebState, io: std.Io, id: i64) ?Failure {
|
||||
const database = switch (mutations.configDb(state)) {
|
||||
.database => |value| value,
|
||||
.fail => |failure| return failure,
|
||||
};
|
||||
const database = mutations.requireConfigDb(state) catch return mutations.no_config_db;
|
||||
|
||||
state.config_lock.lockUncancelable(io);
|
||||
const written = sources_repo.deleteSource(database, id);
|
||||
@@ -189,32 +180,19 @@ pub fn applyRefresh(state: *server.WebState, io: std.Io, out: []manager_mod.Sour
|
||||
// routes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub fn list(state: *server.WebState, io: std.Io, request: *Request) HandlerError!void {
|
||||
_ = io;
|
||||
const database = switch (mutations.configDb(state)) {
|
||||
.database => |value| value,
|
||||
.fail => |failure| return mutations.respondFailure(request, failure, "listing blocklists"),
|
||||
};
|
||||
const resource = mutations.Resource(.{
|
||||
.Row = sources_repo.SourceRow,
|
||||
.list = sources_repo.listSourceRows,
|
||||
.get = sources_repo.getSource,
|
||||
.remove = applyDelete,
|
||||
.label = "a blocklist",
|
||||
.plural = "blocklists",
|
||||
.envelope = "blocklists",
|
||||
});
|
||||
|
||||
const rows = sources_repo.listSourceRows(database, request.arena) catch |err|
|
||||
return mutations.respondFailure(request, .{ .internal = err }, "listing blocklists");
|
||||
|
||||
return http_util.respondJson(request, .ok, .{ .blocklists = rows.items }, &.{});
|
||||
}
|
||||
|
||||
pub fn get(state: *server.WebState, io: std.Io, request: *Request) HandlerError!void {
|
||||
_ = io;
|
||||
const database = switch (mutations.configDb(state)) {
|
||||
.database => |value| value,
|
||||
.fail => |failure| return mutations.respondFailure(request, failure, "reading a blocklist"),
|
||||
};
|
||||
|
||||
const row = sources_repo.getSource(database, request.arena, request.id.?) catch |err|
|
||||
return mutations.respondFailure(request, .{ .internal = err }, "reading a blocklist");
|
||||
const found = row orelse return mutations.respondFailure(request, .not_found, "");
|
||||
|
||||
return http_util.respondJson(request, .ok, found, &.{});
|
||||
}
|
||||
pub const list = resource.list;
|
||||
pub const get = resource.get;
|
||||
pub const remove = resource.remove;
|
||||
|
||||
pub fn create(state: *server.WebState, io: std.Io, request: *Request) HandlerError!void {
|
||||
const parsed = http_util.parseBody(Body, request) catch |err|
|
||||
@@ -251,13 +229,6 @@ pub fn update(state: *server.WebState, io: std.Io, request: *Request) HandlerErr
|
||||
}, &.{});
|
||||
}
|
||||
|
||||
pub fn remove(state: *server.WebState, io: std.Io, request: *Request) HandlerError!void {
|
||||
if (applyDelete(state, io, request.id.?)) |failure| {
|
||||
return mutations.respondFailure(request, failure, "deleting a blocklist");
|
||||
}
|
||||
return http_util.respondEmpty(request, .no_content);
|
||||
}
|
||||
|
||||
/// `POST /api/blocklists/update`.
|
||||
pub fn refresh(state: *server.WebState, io: std.Io, request: *Request) HandlerError!void {
|
||||
const statuses = try request.arena.alloc(manager_mod.SourceStatus, max_statuses);
|
||||
|
||||
Reference in New Issue
Block a user