feat: sessão #3 — lib (db/auth/email/validations), API routes, NextAuth v5, middleware, páginas account/shelters/shelter-dashboard, Prisma v7 fix
This commit is contained in:
51
app/api/animals/route.ts
Normal file
51
app/api/animals/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db/prisma';
|
||||
import { animalFilterSchema } from '@/lib/validations/animal';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
const parsed = animalFilterSchema.safeParse(
|
||||
Object.fromEntries(searchParams.entries())
|
||||
);
|
||||
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Parâmetros inválidos.', details: parsed.error.flatten() },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const { district, species, sex, sterilized, urgent, status, page, limit } =
|
||||
parsed.data;
|
||||
|
||||
const where = {
|
||||
...(species && { species }),
|
||||
...(sex && { sex }),
|
||||
...(sterilized !== undefined && { sterilized }),
|
||||
...(urgent !== undefined && { urgent }),
|
||||
status: status ?? 'AVAILABLE',
|
||||
...(district && {
|
||||
shelter: { district: { contains: district, mode: 'insensitive' as const } },
|
||||
}),
|
||||
};
|
||||
|
||||
const [animals, total] = await Promise.all([
|
||||
prisma.animal.findMany({
|
||||
where,
|
||||
include: {
|
||||
photos: { where: { isPrimary: true }, take: 1 },
|
||||
shelter: { select: { id: true, name: true, district: true } },
|
||||
},
|
||||
orderBy: [{ urgent: 'desc' }, { createdAt: 'desc' }],
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
}),
|
||||
prisma.animal.count({ where }),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
animals,
|
||||
pagination: { page, limit, total, pages: Math.ceil(total / limit) },
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user