Initial commit

This commit is contained in:
Jose Selesan
2026-04-08 22:53:11 -03:00
commit 9ae270609d
179 changed files with 28096 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
import { v7 as uuidv7 } from 'uuid'
import { Prisma } from '@/generated/prisma/client'
import { db } from '@/lib/prisma'
export type CreateSportInput = {
name: string
}
export type UpdateSportInput = {
name?: string
isActive?: boolean
}
function slugify(value: string): string {
return value
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
}
async function buildUniqueSlug(
source: string,
excludeSportId?: string,
): Promise<string> {
const base = slugify(source)
const fallback = base.length > 0 ? base : `sport-${uuidv7().slice(0, 8)}`
let candidate = fallback
let index = 1
while (true) {
const existing = await db.sport.findFirst({
where: {
slug: candidate,
...(excludeSportId
? {
id: {
not: excludeSportId,
},
}
: {}),
},
select: { id: true },
})
if (!existing) return candidate
index += 1
candidate = `${fallback}-${index}`
}
}
export async function listSports() {
return db.sport.findMany({
orderBy: { name: 'asc' },
})
}
export async function createSport(input: CreateSportInput) {
const slug = await buildUniqueSlug(input.name)
return db.sport.create({
data: {
id: uuidv7(),
name: input.name.trim(),
slug,
isActive: true,
},
})
}
export async function getSportById(id: string) {
return db.sport.findUnique({ where: { id } })
}
export async function updateSport(id: string, input: UpdateSportInput) {
const data: Prisma.SportUncheckedUpdateInput = {}
if (input.name) {
data.name = input.name
data.slug = await buildUniqueSlug(input.name, id)
}
if (input.isActive !== undefined) {
data.isActive = input.isActive
}
return db.sport.update({
where: { id },
data,
})
}