Skip to content

Commit ebf384b

Browse files
committed
chore: dont circumvent repo for book routes
1 parent c238a5a commit ebf384b

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

src/modules/books/model.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ pub const BookRepository = struct {
316316
return self.db.rowsAffected() > 0;
317317
}
318318

319+
pub fn unarchiveBook(self: *BookRepository, id: i64) !void {
320+
const query =
321+
\\UPDATE books
322+
\\SET status = 'active', archived_at = NULL,
323+
\\ archive_reason = NULL, updated_at = CURRENT_TIMESTAMP
324+
\\WHERE id = ?
325+
;
326+
327+
var stmt = try self.db.prepare(query);
328+
defer stmt.deinit();
329+
330+
try stmt.exec(.{}, .{id});
331+
}
332+
319333
pub fn archiveBook(self: *BookRepository, id: i64, reason: ?[]const u8) !void {
320334
const query =
321335
\\UPDATE books
@@ -445,6 +459,20 @@ pub const AuthorRepository = struct {
445459
return self.db.getLastInsertRowID();
446460
}
447461

462+
pub fn getBooksByAuthor(self: *AuthorRepository, allocator: std.mem.Allocator, author_id: i64) ![]Book {
463+
const query =
464+
\\SELECT id, name, author, file_path, isbn, publication_date, publisher,
465+
\\ description, page_count, file_size, file_format, cover_image_path,
466+
\\ status, archived_at, archive_reason, created_at, updated_at
467+
\\FROM books WHERE author = ? AND status = 'active'
468+
;
469+
470+
var stmt = try self.db.prepare(query);
471+
defer stmt.deinit();
472+
473+
return try stmt.all(Book, allocator, .{}, .{author_id});
474+
}
475+
448476
pub fn getAuthorByName(self: *AuthorRepository, allocator: std.mem.Allocator, name: []const u8) !Author {
449477
const query =
450478
\\SELECT id, name, biography, birth_date, death_date, nationality,

src/modules/books/routes.zig

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ pub fn listAuthors(
282282

283283
// GET /books/authors/:id - Get specific author with books
284284
pub fn getAuthor(
285-
db: *sqlite.Db,
286285
author_repo: *AuthorRepository,
287286
req: *httpz.Request,
288287
res: *httpz.Response,
@@ -310,17 +309,7 @@ pub fn getAuthor(
310309
return err;
311310
};
312311

313-
const query =
314-
\\SELECT id, name, author, file_path, isbn, publication_date, publisher,
315-
\\ description, page_count, file_size, file_format, cover_image_path,
316-
\\ status, archived_at, archive_reason, created_at, updated_at
317-
\\FROM books WHERE author = ? AND status = 'active'
318-
;
319-
320-
var stmt = try db.prepare(query);
321-
defer stmt.deinit();
322-
323-
const books = try stmt.all(Book, allocator, .{}, .{id});
312+
const books = try author_repo.getBooksByAuthor(allocator, id);
324313
for (books) |*book| {
325314
if (book.cover_image_path) |path| {
326315
const coverPath = try CoverManager.transformDBCoverForHTTP(allocator, book.*);
@@ -549,7 +538,7 @@ pub fn archiveBook(
549538

550539
// DELETE /books/:id/archive - Unarchive a book
551540
pub fn unarchiveBook(
552-
db: *sqlite.Db,
541+
book_repo: *BookRepository,
553542
req: *httpz.Request,
554543
res: *httpz.Response,
555544
) !void {
@@ -565,17 +554,7 @@ pub fn unarchiveBook(
565554
return;
566555
};
567556

568-
const query =
569-
\\UPDATE books
570-
\\SET status = 'active', archived_at = NULL,
571-
\\ archive_reason = NULL, updated_at = CURRENT_TIMESTAMP
572-
\\WHERE id = ?
573-
;
574-
575-
var stmt = try db.prepare(query);
576-
defer stmt.deinit();
577-
578-
try stmt.exec(.{}, .{book_id});
557+
try book_repo.unarchiveBook(book_id);
579558

580559
res.status = 200;
581560
try res.json(.{ .message = "Book unarchived successfully" }, .{});

src/server.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fn booksAuthors(ctx: *ServerContext, req: *httpz.Request, res: *httpz.Response)
362362

363363
fn booksAuthor(ctx: *ServerContext, req: *httpz.Request, res: *httpz.Response) !void {
364364
logging.logRequest(req);
365-
try book_routes.getAuthor(&ctx.db.db, ctx.author_repo, req, res);
365+
try book_routes.getAuthor(ctx.author_repo, req, res);
366366
}
367367

368368
// Admin route handlers
@@ -630,7 +630,7 @@ fn booksArchive(ctx: *ServerContext, req: *httpz.Request, res: *httpz.Response)
630630

631631
fn booksUnarchive(ctx: *ServerContext, req: *httpz.Request, res: *httpz.Response) !void {
632632
logging.logRequest(req);
633-
try book_routes.unarchiveBook(&ctx.db.db, req, res);
633+
try book_routes.unarchiveBook(ctx.book_repo, req, res);
634634
}
635635

636636
// Cover route handler

0 commit comments

Comments
 (0)