2025-10-12 21:16:00 -07:00
|
|
|
import { eq, and } from "drizzle-orm";
|
|
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
|
|
|
|
|
|
|
|
|
export type SeasonTemplateSport = typeof schema.seasonTemplateSports.$inferSelect;
|
|
|
|
|
export type NewSeasonTemplateSport = typeof schema.seasonTemplateSports.$inferInsert;
|
|
|
|
|
|
|
|
|
|
export async function addSportToTemplate(
|
|
|
|
|
templateId: string,
|
2026-06-04 21:31:49 +00:00
|
|
|
sportId: string
|
2025-10-12 21:16:00 -07:00
|
|
|
): Promise<SeasonTemplateSport> {
|
|
|
|
|
const db = database();
|
|
|
|
|
const [link] = await db
|
|
|
|
|
.insert(schema.seasonTemplateSports)
|
|
|
|
|
.values({
|
|
|
|
|
templateId,
|
2026-06-04 21:31:49 +00:00
|
|
|
sportId,
|
2025-10-12 21:16:00 -07:00
|
|
|
})
|
|
|
|
|
.returning();
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function addMultipleSportsToTemplate(
|
|
|
|
|
data: NewSeasonTemplateSport[]
|
|
|
|
|
): Promise<SeasonTemplateSport[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db
|
|
|
|
|
.insert(schema.seasonTemplateSports)
|
|
|
|
|
.values(data)
|
|
|
|
|
.returning();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function removeSportFromTemplate(
|
|
|
|
|
templateId: string,
|
2026-06-04 21:31:49 +00:00
|
|
|
sportId: string
|
2025-10-12 21:16:00 -07:00
|
|
|
): Promise<void> {
|
|
|
|
|
const db = database();
|
|
|
|
|
await db
|
|
|
|
|
.delete(schema.seasonTemplateSports)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(schema.seasonTemplateSports.templateId, templateId),
|
2026-06-04 21:31:49 +00:00
|
|
|
eq(schema.seasonTemplateSports.sportId, sportId)
|
2025-10-12 21:16:00 -07:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findSeasonTemplateSportsByTemplateId(
|
|
|
|
|
templateId: string
|
|
|
|
|
): Promise<SeasonTemplateSport[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.seasonTemplateSports.findMany({
|
|
|
|
|
where: eq(schema.seasonTemplateSports.templateId, templateId),
|
|
|
|
|
with: {
|
2026-06-04 21:31:49 +00:00
|
|
|
sport: true,
|
2025-10-12 21:16:00 -07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 21:31:49 +00:00
|
|
|
export async function findSeasonTemplateSportsBySportId(
|
|
|
|
|
sportId: string
|
2025-10-12 21:16:00 -07:00
|
|
|
): Promise<SeasonTemplateSport[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.seasonTemplateSports.findMany({
|
2026-06-04 21:31:49 +00:00
|
|
|
where: eq(schema.seasonTemplateSports.sportId, sportId),
|
2025-10-12 21:16:00 -07:00
|
|
|
with: {
|
|
|
|
|
template: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteSeasonTemplateSport(id: string): Promise<void> {
|
|
|
|
|
const db = database();
|
|
|
|
|
await db.delete(schema.seasonTemplateSports).where(eq(schema.seasonTemplateSports.id, id));
|
|
|
|
|
}
|