This commit is contained in:
2025-11-15 09:14:32 +03:30
parent 53ef6220c1
commit 84163f6c8c
7 changed files with 25 additions and 22 deletions
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { FilterQuery } from '@mikro-orm/core';
import { Foods } from '../entities/food.entity';
import { Food } from '../entities/food.entity';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
type FindFoodsOpts = {
@@ -15,20 +15,20 @@ type FindFoodsOpts = {
};
@Injectable()
export class FoodRepository extends EntityRepository<Foods> {
export class FoodRepository extends EntityRepository<Food> {
constructor(readonly em: EntityManager) {
super(em, Foods);
super(em, Food);
}
/**
* Find foods with pagination and optional filters.
* Supports: search (title/content), categoryId, isActive, ordering.
*/
async findAllPaginated(opts: FindFoodsOpts = {}): Promise<PaginatedResult<Foods>> {
async findAllPaginated(opts: FindFoodsOpts = {}): Promise<PaginatedResult<Food>> {
const { page = 1, limit = 10, search, orderBy = 'createdAt', order = 'desc', categoryId, isActive } = opts;
const offset = (page - 1) * limit;
const where: FilterQuery<Foods> = {};
const where: FilterQuery<Food> = {};
if (typeof isActive === 'boolean') {
where.isActive = isActive;
@@ -41,7 +41,7 @@ export class FoodRepository extends EntityRepository<Foods> {
if (categoryId) {
// filter by related categories (typed via FilterQuery)
Object.assign(where, { categories: { id: categoryId } } as unknown as FilterQuery<Foods>);
Object.assign(where, { categories: { id: categoryId } } as unknown as FilterQuery<Food>);
}
const [data, total] = await this.findAndCount(where, {