I'm realizing what I have is extremely fragile, so not ideal, but not a bad starting place I would say
123 lines
3.7 KiB
Zig
123 lines
3.7 KiB
Zig
const std = @import("std");
|
|
const jetzig = @import("jetzig");
|
|
const jetquery = @import("jetzig").jetquery;
|
|
|
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
|
var root = try request.data(.object);
|
|
var albums_view = try root.put("albums", .array);
|
|
const query = jetzig.database.Query(.Album).select(.{}).orderBy(.{ .name = .asc });
|
|
const albums = try request.repo.all(query);
|
|
for (albums) |album| {
|
|
var album_view = try albums_view.append(.object);
|
|
try album_view.put("name", album.name);
|
|
try album_view.put("url", album.id);
|
|
}
|
|
return request.render(.ok);
|
|
}
|
|
|
|
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|
const album = try jetzig.database.Query(.Album).find(id).execute(request.repo);
|
|
var root = try request.data(.object);
|
|
try root.put("album", album.?.name);
|
|
var songs_view = try root.put("songs", .array);
|
|
const query = jetzig.database.Query(.Albumsong).include(.song, .{ .select = .{ .name, .id } }).join(.inner, .album).where(.{ .album = .{ .id = id } });
|
|
const songs = try request.repo.all(query);
|
|
for (songs) |song| {
|
|
var song_view = try songs_view.append(.object);
|
|
try song_view.put("name", song.song.name);
|
|
try song_view.put("url", song.song.id);
|
|
}
|
|
return request.render(.ok);
|
|
}
|
|
|
|
pub fn new(request: *jetzig.Request) !jetzig.View {
|
|
return request.render(.ok);
|
|
}
|
|
|
|
pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|
_ = id;
|
|
return request.render(.ok);
|
|
}
|
|
|
|
pub fn post(request: *jetzig.Request) !jetzig.View {
|
|
return request.render(.created);
|
|
}
|
|
|
|
pub fn put(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|
_ = id;
|
|
return request.render(.ok);
|
|
}
|
|
|
|
pub fn patch(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|
_ = id;
|
|
return request.render(.ok);
|
|
}
|
|
|
|
pub fn delete(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|
_ = id;
|
|
return request.render(.ok);
|
|
}
|
|
|
|
test "index" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.GET, "/album", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|
|
|
|
test "get" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.GET, "/album/example-id", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|
|
|
|
test "new" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.GET, "/album/new", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|
|
|
|
test "edit" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.GET, "/album/example-id/edit", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|
|
|
|
test "post" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.POST, "/album", .{});
|
|
try response.expectStatus(.created);
|
|
}
|
|
|
|
test "put" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.PUT, "/album/example-id", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|
|
|
|
test "patch" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.PATCH, "/album/example-id", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|
|
|
|
test "delete" {
|
|
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
defer app.deinit();
|
|
|
|
const response = try app.request(.DELETE, "/album/example-id", .{});
|
|
try response.expectStatus(.ok);
|
|
}
|