64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
|
|
import { eq } from "drizzle-orm";
|
||
|
|
import { database } from "~/database/context";
|
||
|
|
import * as schema from "~/database/schema";
|
||
|
|
|
||
|
|
export type Sport = typeof schema.sports.$inferSelect;
|
||
|
|
export type NewSport = typeof schema.sports.$inferInsert;
|
||
|
|
export type SportType = "team" | "individual";
|
||
|
|
|
||
|
|
export async function createSport(data: NewSport): Promise<Sport> {
|
||
|
|
const db = database();
|
||
|
|
const [sport] = await db
|
||
|
|
.insert(schema.sports)
|
||
|
|
.values(data)
|
||
|
|
.returning();
|
||
|
|
return sport;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function findSportById(id: string): Promise<Sport | undefined> {
|
||
|
|
const db = database();
|
||
|
|
return await db.query.sports.findFirst({
|
||
|
|
where: eq(schema.sports.id, id),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function findSportBySlug(slug: string): Promise<Sport | undefined> {
|
||
|
|
const db = database();
|
||
|
|
return await db.query.sports.findFirst({
|
||
|
|
where: eq(schema.sports.slug, slug),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function findAllSports(): Promise<Sport[]> {
|
||
|
|
const db = database();
|
||
|
|
return await db.query.sports.findMany({
|
||
|
|
orderBy: (sports, { asc }) => [asc(sports.name)],
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function findSportsByType(type: SportType): Promise<Sport[]> {
|
||
|
|
const db = database();
|
||
|
|
return await db.query.sports.findMany({
|
||
|
|
where: eq(schema.sports.type, type),
|
||
|
|
orderBy: (sports, { asc }) => [asc(sports.name)],
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function updateSport(
|
||
|
|
id: string,
|
||
|
|
data: Partial<NewSport>
|
||
|
|
): Promise<Sport> {
|
||
|
|
const db = database();
|
||
|
|
const [sport] = await db
|
||
|
|
.update(schema.sports)
|
||
|
|
.set({ ...data, updatedAt: new Date() })
|
||
|
|
.where(eq(schema.sports.id, id))
|
||
|
|
.returning();
|
||
|
|
return sport;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteSport(id: string): Promise<void> {
|
||
|
|
const db = database();
|
||
|
|
await db.delete(schema.sports).where(eq(schema.sports.id, id));
|
||
|
|
}
|