74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { Product, Prisma } from '@prisma/client';
|
|
|
|
@Injectable()
|
|
export class ProductService {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
async findAll(params: {
|
|
skip?: number;
|
|
take?: number;
|
|
where?: Prisma.ProductWhereInput;
|
|
orderBy?: Prisma.ProductOrderByWithRelationInput;
|
|
}): Promise<{ data: Product[]; total: number }> {
|
|
const { skip, take, where, orderBy } = params;
|
|
const [data, total] = await this.prisma.$transaction([
|
|
this.prisma.product.findMany({
|
|
skip,
|
|
take,
|
|
where,
|
|
orderBy,
|
|
include: {
|
|
productType: true,
|
|
parent: true,
|
|
},
|
|
}),
|
|
this.prisma.product.count({ where }),
|
|
]);
|
|
return { data, total };
|
|
}
|
|
|
|
async findOne(where: Prisma.ProductWhereUniqueInput): Promise<Product | null> {
|
|
return this.prisma.product.findUnique({
|
|
where,
|
|
include: {
|
|
productType: true,
|
|
parent: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async create(data: Prisma.ProductCreateInput): Promise<Product> {
|
|
return this.prisma.product.create({
|
|
data,
|
|
include: {
|
|
productType: true,
|
|
parent: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async update(params: {
|
|
where: Prisma.ProductWhereUniqueInput;
|
|
data: Prisma.ProductUpdateInput;
|
|
}): Promise<Product> {
|
|
return this.prisma.product.update({
|
|
...params,
|
|
include: {
|
|
productType: true,
|
|
parent: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async remove(where: Prisma.ProductWhereUniqueInput): Promise<Product> {
|
|
return this.prisma.product.delete({
|
|
where,
|
|
include: {
|
|
productType: true,
|
|
parent: true,
|
|
},
|
|
});
|
|
}
|
|
} |