53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
@Injectable()
|
|
export class PetsService {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
async getPets(query: any) {
|
|
const { page = 1, limit = 10, search = '' } = query;
|
|
const skip = (Number(page) - 1) * Number(limit);
|
|
|
|
const where = search
|
|
? { name: { contains: search, mode: 'insensitive' as any } }
|
|
: {};
|
|
|
|
const [data, total] = await Promise.all([
|
|
this.prisma.pet.findMany({
|
|
where,
|
|
skip,
|
|
take: Number(limit),
|
|
orderBy: { createdAt: 'desc' },
|
|
include: {
|
|
user: {
|
|
select: {
|
|
firstName: true,
|
|
lastName: true,
|
|
email: true,
|
|
mobile: true,
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
this.prisma.pet.count({ where }),
|
|
]);
|
|
|
|
return {
|
|
data,
|
|
meta: {
|
|
total,
|
|
page: Number(page),
|
|
limit: Number(limit),
|
|
lastPage: Math.ceil(total / Number(limit)),
|
|
},
|
|
};
|
|
}
|
|
|
|
async deletePet(id: string) {
|
|
const pet = await this.prisma.pet.findUnique({ where: { id } });
|
|
if (!pet) throw new NotFoundException('Pet not found');
|
|
return this.prisma.pet.delete({ where: { id } });
|
|
}
|
|
}
|