crud product

This commit is contained in:
2026-01-13 18:19:51 +03:30
parent 984b889587
commit 6522acff5f
27 changed files with 333 additions and 1080 deletions
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { FilterQuery } from '@mikro-orm/core';
import { product } from '../entities/product.entity';
import { Product } from '../entities/product.entity';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
type FindproductsOpts = {
@@ -15,20 +15,17 @@ type FindproductsOpts = {
};
@Injectable()
export class productRepository extends EntityRepository<product> {
export class ProductRepository extends EntityRepository<Product> {
constructor(readonly em: EntityManager) {
super(em, product);
super(em, Product);
}
/**
* Find products with pagination and optional filters.
* Supports: search (title/content), categoryId, isActive, ordering.
*/
async findAllPaginated(restId: string, opts: FindproductsOpts = {}): Promise<PaginatedResult<product>> {
async findAllPaginated(opts: FindproductsOpts = {}): Promise<PaginatedResult<Product>> {
const { page = 1, limit = 10, search, orderBy = 'createdAt', order = 'desc', categoryId, isActive } = opts;
const offset = (page - 1) * limit;
const where: FilterQuery<product> = { restaurant: { id: restId } };
const where: FilterQuery<Product> = {};
if (typeof isActive === 'boolean') {
where.isActive = isActive;
@@ -36,19 +33,19 @@ export class productRepository extends EntityRepository<product> {
if (search) {
const pattern = `%${search}%`;
where.$or = [{ title: { $ilike: pattern } }, { desc: { $ilike: pattern } }];
where.$or = [{ title: { $ilike: pattern } },];
}
if (categoryId) {
// filter by related category (product has a single `category` relation)
Object.assign(where, { category: { id: categoryId } } as unknown as FilterQuery<product>);
Object.assign(where, { category: { id: categoryId } } as unknown as FilterQuery<Product>);
}
const [data, total] = await this.findAndCount(where, {
limit,
offset,
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
populate: ['category', 'inventory'],
populate: ['category'],
});
const totalPages = Math.ceil(total / limit);