From 12247349e2a2de01e94ebe64f9b7ff98e9ec8699 Mon Sep 17 00:00:00 2001 From: ayocodingit Date: Tue, 5 Dec 2023 09:10:13 +0700 Subject: [PATCH] feat(image): add filter by keyword and category --- .../images/repository/mongo/repository.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/modules/images/repository/mongo/repository.ts b/src/modules/images/repository/mongo/repository.ts index 7cdb1fd..6bb4f8a 100644 --- a/src/modules/images/repository/mongo/repository.ts +++ b/src/modules/images/repository/mongo/repository.ts @@ -5,9 +5,23 @@ import imageSchema from '../../../../database/mongo/schemas/image.schema' class Repository { constructor(private logger: Logger) {} - public async Fetch({ offset, limit }: RequestParams) { - const data = await imageSchema.find().skip(offset).limit(limit) - const count = await imageSchema.find().count() + public async Fetch({ offset, limit, keyword, category }: RequestParams) { + const filter = {} + + if (keyword) + Object.assign(filter, { + title: { + $regex: new RegExp(keyword, 'i'), + }, + }) + + if (category) + Object.assign(filter, { + category: category, + }) + + const data = await imageSchema.find(filter).skip(offset).limit(limit) + const count = await imageSchema.find(filter).count() return { data,