brackt/app/models/season-template.ts
chrisp f657293430
All checks were successful
🚀 Deploy / 🧪 Test (push) Successful in 2m32s
🚀 Deploy / ʦ🔍 Typecheck & Lint (push) Successful in 1m18s
🚀 Deploy / 🐳 Build (push) Successful in 1m10s
🚀 Deploy / 🚀 Deploy (push) Successful in 12s
claude/sports-templates-conversion-ILWAM (#71)
Co-authored-by: Claude <noreply@anthropic.com>
Reviewed-on: #71
2026-06-04 21:31:49 +00:00

84 lines
2.4 KiB
TypeScript

import { eq, sql } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
export async function countSeasonTemplates(): Promise<number> {
const db = database();
const [{ count }] = await db
.select({ count: sql<number>`count(*)::int` })
.from(schema.seasonTemplates);
return count;
}
export type SeasonTemplate = typeof schema.seasonTemplates.$inferSelect;
export type NewSeasonTemplate = typeof schema.seasonTemplates.$inferInsert;
export async function createSeasonTemplate(data: NewSeasonTemplate): Promise<SeasonTemplate> {
const db = database();
const [template] = await db
.insert(schema.seasonTemplates)
.values(data)
.returning();
return template;
}
export async function findSeasonTemplateById(id: string): Promise<SeasonTemplate | undefined> {
const db = database();
return await db.query.seasonTemplates.findFirst({
where: eq(schema.seasonTemplates.id, id),
});
}
export async function findSeasonTemplateWithSports(id: string) {
const db = database();
return await db.query.seasonTemplates.findFirst({
where: eq(schema.seasonTemplates.id, id),
with: {
seasonTemplateSports: {
with: {
sport: true,
},
},
},
});
}
export async function findActiveSeasonTemplates(): Promise<SeasonTemplate[]> {
const db = database();
return await db.query.seasonTemplates.findMany({
where: eq(schema.seasonTemplates.isActive, true),
orderBy: (templates, { asc }) => [asc(templates.name)],
});
}
export async function findAllSeasonTemplates(): Promise<SeasonTemplate[]> {
const db = database();
return await db.query.seasonTemplates.findMany({
orderBy: (templates, { asc }) => [asc(templates.name)],
});
}
export async function updateSeasonTemplate(
id: string,
data: Partial<NewSeasonTemplate>
): Promise<SeasonTemplate> {
const db = database();
const [template] = await db
.update(schema.seasonTemplates)
.set({ ...data, updatedAt: new Date() })
.where(eq(schema.seasonTemplates.id, id))
.returning();
return template;
}
export async function setSeasonTemplateActive(
id: string,
isActive: boolean
): Promise<SeasonTemplate> {
return await updateSeasonTemplate(id, { isActive });
}
export async function deleteSeasonTemplate(id: string): Promise<void> {
const db = database();
await db.delete(schema.seasonTemplates).where(eq(schema.seasonTemplates.id, id));
}