diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-14 17:04:21 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-14 17:04:21 +0500 |
| commit | e21c194861198f298af34ac07032c04979ac5c1e (patch) | |
| tree | 5e664ef5a35dca60366c3ff332617239c11622e1 /backend/src | |
| parent | b88d91870d40a84ad6941b2394838d6784f81db2 (diff) | |
get ranged weapons by id
Diffstat (limited to 'backend/src')
| -rw-r--r-- | backend/src/API/WeaponsAPI.zig | 23 | ||||
| -rw-r--r-- | backend/src/Database/Connection.zig | 4 | ||||
| -rw-r--r-- | backend/src/Database/RangedWeaponsAccessLayer.zig | 15 | ||||
| -rw-r--r-- | backend/src/Models/RangedWeapon.zig | 20 |
4 files changed, 49 insertions, 13 deletions
diff --git a/backend/src/API/WeaponsAPI.zig b/backend/src/API/WeaponsAPI.zig index 6c24721..d095944 100644 --- a/backend/src/API/WeaponsAPI.zig +++ b/backend/src/API/WeaponsAPI.zig @@ -6,8 +6,8 @@ const db = @import("../Database/Connection.zig"); pub fn RegisterEndpoints(router: *httpz.Router(void, *const fn (*httpz.request.Request, *httpz.response.Response) anyerror!void)) void { router.get("/weapons", testEndpoint, .{}); router.post("/weapons/ranged", newRangedWeapon, .{}); + router.get("/weapons/ranged/:id", getRangedWeaponById, .{}); router.get("/weapons/ranged", getAllRangedWeapons, .{}); - // router.get("/weapons/ranged/:id", getAllRangedWeapons, .{}); } fn testEndpoint(_: *httpz.Request, res: *httpz.Response) !void { @@ -31,13 +31,9 @@ fn testEndpoint(_: *httpz.Request, res: *httpz.Response) !void { try res.json(testType, .{}); } -fn getAllRangedWeapons(_: *httpz.Request, res: *httpz.Response) !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const allocator = gpa.allocator(); - defer _ = gpa.deinit(); - - var found = try db.RangedWeapons.GetAll(allocator); - defer found.deinit(allocator); +fn getAllRangedWeapons(req: *httpz.Request, res: *httpz.Response) !void { + var found = try db.RangedWeapons.GetAll(req.arena); + defer found.deinit(req.arena); try res.json(found.items, .{}); } @@ -48,7 +44,16 @@ fn getRangedWeaponById(req: *httpz.Request, res: *httpz.Response) !void { return; }; - _ = id; + const found = db.RangedWeapons.GetByDisplayName(req.arena, id) catch |err| { + // Empty result handling + if (err == db.ResultErrors.NotFound) { + res.status = 204; + return; + } + return err; + }; + + try res.json(found, .{}); } fn newRangedWeapon(req: *httpz.Request, res: *httpz.Response) !void { diff --git a/backend/src/Database/Connection.zig b/backend/src/Database/Connection.zig index 736c4e6..623165f 100644 --- a/backend/src/Database/Connection.zig +++ b/backend/src/Database/Connection.zig @@ -2,6 +2,10 @@ const std = @import("std"); const pg = @import("pg"); const dotenv = @import("dotenv"); +pub const ResultErrors = error { + NotFound, +}; + pub var pool: *pg.Pool = undefined; pub fn Connect(alloc: std.mem.Allocator) !void { diff --git a/backend/src/Database/RangedWeaponsAccessLayer.zig b/backend/src/Database/RangedWeaponsAccessLayer.zig index ed35064..ca41197 100644 --- a/backend/src/Database/RangedWeaponsAccessLayer.zig +++ b/backend/src/Database/RangedWeaponsAccessLayer.zig @@ -13,12 +13,19 @@ pub fn GetAll(alloc: std.mem.Allocator) !std.ArrayList(model.RangedWeaponType) { var outp: std.ArrayList(model.RangedWeaponType) = .empty; while (try result.next()) |row| { - const newRW = try model.RangedWeaponType.Map(&row); + const newRW = try model.RangedWeaponType.MapWithAlloc(alloc, &row); try outp.append(alloc, newRW); } return outp; } -// pub fn GetByDisplayName(alloc: std.mem.Allocator, displayName: []const u8) !model.RangedWeaponType { - // const query = "SELECT * FROM RangedWeapons WHERE Id = "; -// } +pub fn GetByDisplayName(allocator: std.mem.Allocator, displayName: []const u8) !model.RangedWeaponType { + const query = "SELECT * FROM RangedWeapons WHERE Id = $1"; + var row = try conn.pool.row(query, .{displayName}) orelse { + return conn.ResultErrors.NotFound; + }; + + const outp = try model.RangedWeaponType.MapWithAlloc(allocator, &row.row); + try row.deinit(); + return outp; +} diff --git a/backend/src/Models/RangedWeapon.zig b/backend/src/Models/RangedWeapon.zig index 14884d2..3864fc5 100644 --- a/backend/src/Models/RangedWeapon.zig +++ b/backend/src/Models/RangedWeapon.zig @@ -55,6 +55,26 @@ pub const RangedWeaponType = struct { }; } + // Parses the db row and returns the result. You are expected to use the + // actual DB schema and use * as a fields selector + pub fn MapWithAlloc(allocator: std.mem.Allocator, row: *const pg.Row) !RangedWeaponType { + return RangedWeaponType { + .Id = try allocator.dupe(u8, try row.get([]const u8, 0)), + .Name = try allocator.dupe(u8, try row.get([]const u8, 1)), + .WeaponType = try allocator.dupe(u8, try row.get([]const u8, 2)), + .Accuracy = try row.get(i32, 3), + .Concealability = try allocator.dupe(u8, try row.get([]const u8, 4)), + .Avaliability = try allocator.dupe(u8, try row.get([]const u8, 5)), + .Damage = try allocator.dupe(u8, try row.get([]const u8, 6)), + .Ammunition = try allocator.dupe(u8, try row.get([]const u8, 7)), + .NumberOfShots = @intCast(try row.get(i32, 8)), + .RateOfFire = @intCast(try row.get(i32, 9)), + .Reliability = try allocator.dupe(u8, try row.get([]const u8, 10)), + .CreatedAt = try row.get(i64, 11), + .UpdatedAt = try row.get(i64, 12), + }; + } + }; // ==================== tests ==================== |
