From efa44f9d4f53fafaec2f6bfdeb926bbccb66324d Mon Sep 17 00:00:00 2001 From: Firman Ayo Coding <41193120+ayocodingit@users.noreply.github.com> Date: Tue, 5 Dec 2023 09:16:49 +0700 Subject: [PATCH] feat(image): add filter by keyword and category (#2) --- .../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,