Add digital catalog with shareable flipbook and ktg.montzar.ir deploy files.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { EffectId } from "../shared/types";
|
||||
import { DEFAULT_FLIP_MS } from "../shared/types";
|
||||
import type { DbBundle } from "./db";
|
||||
import { catalogsTable } from "./schema";
|
||||
|
||||
export type CatalogRow = {
|
||||
id: string;
|
||||
deleteToken: string;
|
||||
title: string;
|
||||
effect: EffectId;
|
||||
pageCount: number;
|
||||
flipMs: number;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type CatalogInsert = Omit<CatalogRow, "createdAt" | "flipMs"> & {
|
||||
createdAt?: string;
|
||||
flipMs?: number;
|
||||
};
|
||||
|
||||
function toRow(record: typeof catalogsTable.$inferSelect): CatalogRow {
|
||||
return {
|
||||
id: record.id,
|
||||
deleteToken: record.deleteToken,
|
||||
title: record.title,
|
||||
effect: record.effect as EffectId,
|
||||
pageCount: record.pageCount,
|
||||
flipMs: record.flipMs ?? DEFAULT_FLIP_MS,
|
||||
createdAt: record.createdAt,
|
||||
};
|
||||
}
|
||||
|
||||
export function createCatalogRepo(db: DbBundle) {
|
||||
return {
|
||||
create(input: CatalogInsert): CatalogRow {
|
||||
const createdAt = input.createdAt ?? new Date().toISOString();
|
||||
const flipMs = input.flipMs ?? DEFAULT_FLIP_MS;
|
||||
db.drizzle.insert(catalogsTable).values({
|
||||
id: input.id,
|
||||
deleteToken: input.deleteToken,
|
||||
title: input.title,
|
||||
effect: input.effect,
|
||||
pageCount: input.pageCount,
|
||||
flipMs,
|
||||
createdAt,
|
||||
}).run();
|
||||
db.persist();
|
||||
return {
|
||||
id: input.id,
|
||||
deleteToken: input.deleteToken,
|
||||
title: input.title,
|
||||
effect: input.effect,
|
||||
pageCount: input.pageCount,
|
||||
flipMs,
|
||||
createdAt,
|
||||
};
|
||||
},
|
||||
|
||||
getById(id: string): CatalogRow | null {
|
||||
const row = db.drizzle
|
||||
.select()
|
||||
.from(catalogsTable)
|
||||
.where(eq(catalogsTable.id, id))
|
||||
.get();
|
||||
return row ? toRow(row) : null;
|
||||
},
|
||||
|
||||
deleteById(id: string): boolean {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return false;
|
||||
db.drizzle.delete(catalogsTable).where(eq(catalogsTable.id, id)).run();
|
||||
db.persist();
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export type CatalogRepo = ReturnType<typeof createCatalogRepo>;
|
||||
Reference in New Issue
Block a user