From 2800ecf2e5093f51ae62ffb4702d05d51bf1008a Mon Sep 17 00:00:00 2001 From: Shiorin Date: Wed, 29 Sep 2021 17:35:53 +0700 Subject: [PATCH] =?UTF-8?q?[API]=20-=20=E0=B8=84=E0=B9=89=E0=B8=99?= =?UTF-8?q?=E0=B8=AB=E0=B8=B2=E0=B9=80=E0=B8=AB=E0=B8=95=E0=B8=B8=E0=B8=81?= =?UTF-8?q?=E0=B8=B2=E0=B8=A3=E0=B8=93=E0=B9=8C=E0=B8=94=E0=B9=88=E0=B8=A7?= =?UTF-8?q?=E0=B8=99=20=E0=B8=9C=E0=B8=A5=E0=B8=A5=E0=B8=B1=E0=B8=9E?= =?UTF-8?q?=E0=B8=98=E0=B9=8C=E0=B8=A1=E0=B8=B2=E0=B9=84=E0=B8=A1=E0=B9=88?= =?UTF-8?q?=E0=B8=84=E0=B8=A3=E0=B8=9A=20#351?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/EmergencyEventController.ts | 79 +++++++++++-------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/api-spanboon/src/api/controllers/EmergencyEventController.ts b/api-spanboon/src/api/controllers/EmergencyEventController.ts index ae2855c68..f16544765 100644 --- a/api-spanboon/src/api/controllers/EmergencyEventController.ts +++ b/api-spanboon/src/api/controllers/EmergencyEventController.ts @@ -36,6 +36,7 @@ import { EmergencyLastestProcessor } from '../processors/emergency/EmergencyLast import { EmergencyShareProcessor } from '../processors/emergency/EmergencyShareProcessor'; import { EmergencyPostLikedProcessor } from '../processors/emergency/EmergencyPostLikedProcessor'; import { DateTimeUtil } from '../../utils/DateTimeUtil'; +import { SearchFilter } from './requests/SearchFilterRequest'; @JsonController('/emergency') export class EmergencyEventController { @@ -115,53 +116,61 @@ export class EmergencyEventController { const filter = search.filter; const hashTag = search.hashTag; - const hashTagIdList = []; - const hashTagMap = {}; - if (hashTag !== null && hashTag !== undefined && hashTag !== '') { - filter.whereConditions = { name: { $regex: '.*' + hashTag + '.*', $options: 'si' } }; - } else { - filter.whereConditions = {}; + // whereConditions will be in object search only + const emergencyEventAggr: any[] = []; + if (filter.whereConditions !== undefined && typeof filter.whereConditions === 'object') { + emergencyEventAggr.push({ $match: filter.whereConditions }); } - const hashTagList: HashTag[] = await this.hashTagService.search(filter); - - if (hashTagList !== null && hashTagList !== undefined && hashTagList.length > 0) { - for (const masterHashTag of hashTagList) { - const id = masterHashTag.id; - hashTagMap[id] = masterHashTag; - hashTagIdList.push(new ObjectID(id)); - } - } else { - const successResponse = ResponseUtil.getSuccessResponse('Hashtag Not Found', []); - return res.status(200).send(successResponse); + if (filter.offset === undefined) { + filter.offset = 0; } - - let emergencyLists: EmergencyEvent[]; - - if (hashTagIdList !== null && hashTagIdList !== undefined && hashTagIdList.length > 0) { - emergencyLists = await this.emergencyEventService.find({ hashTag: { $in: hashTagIdList } }); - } else { - emergencyLists = await this.emergencyEventService.find(); + if (filter.limit === undefined) { + filter.limit = 10; } - if (emergencyLists !== null && emergencyLists !== undefined) { - emergencyLists.map((data) => { - const hashTagKey = data.hashTag; - const emergencyHashTag = hashTagMap[hashTagKey]; - - if (emergencyHashTag) { - const hashTagName = emergencyHashTag.name; + emergencyEventAggr.push({ + $lookup: { + from: 'HashTag', + localField: 'hashTag', + foreignField: '_id', + as: 'hasTagObj' + } + }); + emergencyEventAggr.push({ + $unwind: { + path: '$hasTagObj', + preserveNullAndEmptyArrays: true + } + }); + if (hashTag !== null && hashTag !== undefined && hashTag !== '') { + emergencyEventAggr.push({ + $match: { + 'hasTagObj.name': { $regex: '.*' + hashTag + '.*', $options: 'si' } + } + }); + } + emergencyEventAggr.push({ $skip: filter.offset }); + emergencyEventAggr.push({ $limit: filter.limit }); + emergencyEventAggr.push({ $addFields: { id: '$_id' } }); + emergencyEventAggr.push({ $project: { '_id': 0 } }); + + const emergencyEventAggResult = await this.emergencyEventService.aggregate(emergencyEventAggr); + + if (emergencyEventAggResult !== null && emergencyEventAggResult !== undefined) { + // change hashTag from id to name string + emergencyEventAggResult.map((data) => { + if (data.hasTagObj) { + const hashTagName = data.hasTagObj.name; data.hashTag = hashTagName; } }); - const emergencyResult = ObjectUtil.removeDuplicateJSONValue(emergencyLists, data => data.hashTag); - - const successResponse = ResponseUtil.getSuccessResponse('Successfully Search EmergencyEvent', emergencyResult); + const successResponse = ResponseUtil.getSuccessResponse('Successfully Search EmergencyEvent', emergencyEventAggResult); return res.status(200).send(successResponse); } else { - const errorResponse = ResponseUtil.getSuccessResponse('EmergencyEvent Not Found', undefined); + const errorResponse = ResponseUtil.getSuccessResponse('EmergencyEvent Not Found', []); return res.status(200).send(errorResponse); } }