feat: implement error handling and validation middleware, enhance result handling
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { handleResult } from '@/lib/http/handle-result';
|
||||
import { listSports } from '@/modules/sport/services/sport.service';
|
||||
import type { AppContext } from '@/types/hono';
|
||||
|
||||
export async function listSportsHandler(c: AppContext) {
|
||||
const sports = await listSports();
|
||||
return c.json(sports);
|
||||
return handleResult(c, sports);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import { Prisma, Sport } from '@/generated/prisma/client';
|
||||
import { db } from '@/lib/prisma';
|
||||
import { ok, Result } from '@/lib/result';
|
||||
import { v7 as uuidv7 } from 'uuid';
|
||||
|
||||
export type CreateSportInput = {
|
||||
@@ -51,10 +52,11 @@ async function buildUniqueSlug(source: string, excludeSportId?: string): Promise
|
||||
}
|
||||
}
|
||||
|
||||
export async function listSports() {
|
||||
return db.sport.findMany({
|
||||
export async function listSports(): Promise<Result<Sport[]>> {
|
||||
const sports = await db.sport.findMany({
|
||||
orderBy: { name: 'asc' },
|
||||
});
|
||||
return ok(sports);
|
||||
}
|
||||
|
||||
export async function createSport(input: CreateSportInput) {
|
||||
|
||||
@@ -8,18 +8,18 @@ import { zValidator } from '@hono/zod-validator';
|
||||
import { createSportSchema, updateSportSchema } from '@repo/api-contract';
|
||||
import { Hono } from 'hono';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { validate } from '@/lib/http/validate'
|
||||
export const sportRoutes = new Hono<AppEnv>();
|
||||
const sportIdParamsSchema = z.object({ id: z.uuid() });
|
||||
|
||||
sportRoutes.use('*', requireAuth);
|
||||
|
||||
sportRoutes.get('/', listSportsHandler);
|
||||
sportRoutes.post('/', requireSuperAdmin, zValidator('json', createSportSchema), createSportHandler);
|
||||
sportRoutes.post('/', requireSuperAdmin, validate.json(createSportSchema), createSportHandler);
|
||||
sportRoutes.patch(
|
||||
'/:id',
|
||||
requireSuperAdmin,
|
||||
zValidator('param', sportIdParamsSchema),
|
||||
zValidator('json', updateSportSchema),
|
||||
validate.param(sportIdParamsSchema),
|
||||
validate.json(updateSportSchema),
|
||||
updateSportHandler
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user