95 lines
		
	
	
	
		
			3.8 KiB
		
	
	
	
		
			Zig
		
	
	
	
	
	
		
		
			
		
	
	
			95 lines
		
	
	
	
		
			3.8 KiB
		
	
	
	
		
			Zig
		
	
	
	
	
	
|  | const std = @import("std");
 | ||
|  | const pgmql = @import("pgmql");
 | ||
|  | 
 | ||
|  | const _account = @import("models/account.zig");
 | ||
|  | const _invoice = @import("models/invoice.zig");
 | ||
|  | const _invoice_product = @import("models/invoice_product.zig");
 | ||
|  | 
 | ||
|  | pub const registry = pgmql.Registry(struct {
 | ||
|  | 	pub const Account: pgmql.Model = _account.AccountModel;
 | ||
|  | 	pub const Invoice: pgmql.Model = _invoice.InvoiceModel;
 | ||
|  | 	pub const InvoiceProduct: pgmql.Model = _invoice_product.InvoiceProductModel;
 | ||
|  | }, "Account");
 | ||
|  | 
 | ||
|  | const CreateInvoice = registry.dispatchers.Definition(struct {});
 | ||
|  | 
 | ||
|  | const api = registry.Api(struct {
 | ||
|  | 	pub const Account = registry.dispatchers.Crud(registry.Models.Account).All(.{ .anyAuthenticated = true });
 | ||
|  | 
 | ||
|  | 	pub const Invoice = struct {
 | ||
|  | 		pub const create = (CreateInvoice{
 | ||
|  | 			.policy = .{ .anyAuthenticated = true },
 | ||
|  | 			.run = struct {
 | ||
|  | 				fn f(context: CreateInvoice.Context) void {
 | ||
|  | 					_ = context; //TODO!
 | ||
|  | 				}
 | ||
|  | 			}.f,
 | ||
|  | 		}).builder();
 | ||
|  | 
 | ||
|  | 		pub usingnamespace registry.dispatchers.Crud(registry.Models.Invoice).Read(.{.anyAuthenticated = true});
 | ||
|  | 		pub usingnamespace registry.dispatchers.Crud(registry.Models.Invoice).Update(.{.anyAuthenticated = true});
 | ||
|  | 		pub usingnamespace registry.dispatchers.Crud(registry.Models.Invoice).Delete(.{.anyAuthenticated = true});
 | ||
|  | 	};
 | ||
|  | 
 | ||
|  | 	pub const InvoiceProduct = registry.dispatchers.Crud(registry.Models.InvoiceProduct).All(.{ .any = false });
 | ||
|  | });
 | ||
|  | 
 | ||
|  | test "example dispatchers" {
 | ||
|  | 	const dispatchers = api.getDispatchers();
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqual(12, dispatchers.len);
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("account", dispatchers[0].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("read", dispatchers[0].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("account", dispatchers[1].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("create", dispatchers[1].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("account", dispatchers[2].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("update", dispatchers[2].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("account", dispatchers[3].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("delete", dispatchers[3].getDispatchName());
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice", dispatchers[4].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("read", dispatchers[4].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice", dispatchers[5].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("create", dispatchers[5].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice", dispatchers[6].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("update", dispatchers[6].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice", dispatchers[7].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("delete", dispatchers[7].getDispatchName());
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice-product", dispatchers[8].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("read", dispatchers[8].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice-product", dispatchers[9].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("create", dispatchers[9].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice-product", dispatchers[10].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("update", dispatchers[10].getDispatchName());
 | ||
|  | 
 | ||
|  | 	try std.testing.expectEqualStrings("invoice-product", dispatchers[11].getModelIdentifier());
 | ||
|  | 	try std.testing.expectEqualStrings("delete", dispatchers[11].getDispatchName());
 | ||
|  | }
 | ||
|  | 
 | ||
|  | test "example model HTTP API" {
 | ||
|  | 	_ = api;
 | ||
|  | }
 | ||
|  | 
 | ||
|  | /// Example HTTP API executable.
 | ||
|  | pub fn main() !void {
 | ||
|  | 	var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
 | ||
|  | 	defer _ = gpa.deinit();
 | ||
|  | 	const allocator = gpa.allocator();
 | ||
|  | 
 | ||
|  | 	var httpSrv = pgmql.http.Server.init(allocator, api.getDispatchers());
 | ||
|  | 	defer httpSrv.deinit();
 | ||
|  | 	try httpSrv.start();
 | ||
|  | }
 |