Merge branch 'testing' of https://github.com/mitteneer/zuletzt into testing

This commit is contained in:
mitteneer 2024-04-08 20:25:00 -04:00
commit ee855dde18
8 changed files with 120 additions and 8 deletions

BIN
src/app/database/data.db Normal file

Binary file not shown.

View file

@ -1,3 +1,4 @@
const std = @import("std");
const jetzig = @import("jetzig");
/// `src/app/views/root.zig` represents the root URL `/`
@ -18,7 +19,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object();
// Add a string to the root object.
try root.put("message", data.string("Welcome to Jetzig!"));
try root.put("welcome_message", data.string("Welcome to Jetzig!"));
// Request params have the same type as a `data.object()` so they can be inserted them
// directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the
@ -26,9 +27,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
// present.
const params = try request.params();
if (params.get("message")) |value| {
try root.put("message_param", value);
}
try root.put("message_param", params.get("message"));
// Set arbitrary response headers as required. `content-type` is automatically assigned for
// HTML, JSON responses.

12
src/app/views/search.zig Normal file
View file

@ -0,0 +1,12 @@
const std = @import("std");
const jetzig = @import("jetzig");
const search = @import("../../db.zig");
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object();
const params = try request.params();
const query = params.get("q");
try root.put("q",query);
//try root.put("q", data.string("Welcome"));
return request.render(.ok);
}

54
src/db.zig Normal file
View file

@ -0,0 +1,54 @@
const std = @import("std");
const sql = @import("sqlite");
pub fn loadDb(path: []u8) !sql.sqlite.Db{
try sql.sqlite.Db.init(.{
.mode = sql.sqlite.Db.Mode{ .File = path},
.open_flags = .{
.write = true,
.create = true,
},
.threading_mode = .MultiThread,
});
}
const addArtist = \\INSERT INTO artists ('artist', 'plays', 'url') VALUES (?,?)
;
const addTrack = \\INSERT INTO tracks ('artist', 'track', 'album', 'plays', 'url') VALUES (?,?,?,?)
;
const getArtist = \\SELECT artist, plays FROM artists WHERE artist == ?
;
const getTrack = \\SELECT artist, track, album, plays FROM tracks WHERE track == ?
;
const getTrackSearch = \\SELECT url FROM artists WHERE artist == ?
;
const getArtistSearch = \\SELECT url FROM artists WHERE artist == ?
;
pub var db = loadDb("/home/swebb/Source/zuletzt/src/app/database/data.db");
pub fn search(query: []const u8) !void{
var artistSearch = try db.prepare(getArtistSearch);
defer artistSearch.deinit();
var trackSearch = try db.prepare(getTrackSearch);
defer trackSearch.deinit();
const artistResults = try artistSearch.one(
struct {
artist: [128:0]u8,
plays: usize,
},
.{},
.{ .artist = query},
);
if (artistResults) |r|{
std.log.debug("Artist: {}, Plays: {}", .{r.name, r.plays});
}
}

View file

@ -4,6 +4,8 @@ pub const jetzig = @import("jetzig");
pub const routes = @import("routes");
pub const sqlite = @import("sqlite");
// Override default settings in `jetzig.config` here:
pub const jetzig_options = struct {
/// Middleware chain. Add any custom middleware here, or use middleware provided in
@ -85,6 +87,37 @@ pub const jetzig_options = struct {
};
pub fn main() !void {
//var db = try sqlite.Db.init(.{
// .mode = sqlite.Db.Mode{ .File = "/home/swebb/Source/zuletzt/src/app/database/data.db" },
// .open_flags = .{
// .write = true,
// .create = true,
// },
// .threading_mode = .MultiThread,
//});
//const create =
// \\CREATE TABLE artists ('artist', 'plays')
//;
//const query =
// \\INSERT INTO artists ('artist', 'plays') VALUES (?,?)
//;
//var build = try db.prepare(create);
//defer build.deinit();
//try build.exec(.{},.{});
//var stmt = try db.prepare(query);
//defer stmt.deinit();
//try stmt.exec(.{}, .{
// .artist = "Wilco",
// .plays = 2500,
//});
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();