Spaces:
Running
Running
Merge pull request #57 from Modarb-Ai-Trainer/searchUpdate
Browse files
src/lib/services/crud.service.ts
CHANGED
@@ -72,6 +72,43 @@ export const CrudService = <ModelDoc extends Document>(
|
|
72 |
return { docs, paginationData };
|
73 |
}
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
async findOne(
|
76 |
filter: FilterQuery<ModelDoc>,
|
77 |
options?: {
|
|
|
72 |
return { docs, paginationData };
|
73 |
}
|
74 |
|
75 |
+
async search(
|
76 |
+
filter: FilterQuery<ModelDoc>,
|
77 |
+
paginationOptions: {
|
78 |
+
limit?: number;
|
79 |
+
skip?: number;
|
80 |
+
} = {
|
81 |
+
limit: 10,
|
82 |
+
skip: 1,
|
83 |
+
},
|
84 |
+
options?: {
|
85 |
+
populateArray: any
|
86 |
+
},
|
87 |
+
): Promise<{
|
88 |
+
docs: ModelDoc[];
|
89 |
+
paginationData: {
|
90 |
+
total: number;
|
91 |
+
page: number;
|
92 |
+
perPage: number;
|
93 |
+
};
|
94 |
+
}> {
|
95 |
+
const queryInstruction = this.model
|
96 |
+
.find(filter)
|
97 |
+
.limit(paginationOptions.limit)
|
98 |
+
.skip(paginationOptions.skip);
|
99 |
+
if (options?.populateArray) queryInstruction.populate(options.populateArray);
|
100 |
+
|
101 |
+
const docs = await queryInstruction;
|
102 |
+
const total = await this.model.countDocuments(filter);
|
103 |
+
const paginationData = {
|
104 |
+
total: total,
|
105 |
+
page: paginationOptions.skip,
|
106 |
+
perPage: paginationOptions.limit,
|
107 |
+
};
|
108 |
+
|
109 |
+
return { docs, paginationData };
|
110 |
+
}
|
111 |
+
|
112 |
async findOne(
|
113 |
filter: FilterQuery<ModelDoc>,
|
114 |
options?: {
|
src/modules/users/modules/exercises/controllers/exercises.controller.ts
CHANGED
@@ -19,6 +19,7 @@ export class ExerciseController extends BaseController {
|
|
19 |
private exercisesService = new ExerciseService();
|
20 |
|
21 |
setRoutes(): void {
|
|
|
22 |
this.router.get("/", asyncHandler(this.list));
|
23 |
this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
|
24 |
}
|
@@ -55,4 +56,56 @@ export class ExerciseController extends BaseController {
|
|
55 |
res
|
56 |
);
|
57 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
}
|
|
|
19 |
private exercisesService = new ExerciseService();
|
20 |
|
21 |
setRoutes(): void {
|
22 |
+
this.router.get("/search", asyncHandler(this.search));
|
23 |
this.router.get("/", asyncHandler(this.list));
|
24 |
this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
|
25 |
}
|
|
|
56 |
res
|
57 |
);
|
58 |
};
|
59 |
+
|
60 |
+
@SwaggerGet()
|
61 |
+
@SwaggerResponse([ExerciseSerialization])
|
62 |
+
search = async (req: Request, res: Response): Promise<Response> => {
|
63 |
+
const paginationQuery = parsePaginationQuery(req.query);
|
64 |
+
let query = {};
|
65 |
+
let searchTerm = req.query.searchTerm;
|
66 |
+
|
67 |
+
let isNum = !isNaN(parseInt(String(searchTerm)));
|
68 |
+
|
69 |
+
if (isNum) {
|
70 |
+
query =
|
71 |
+
{
|
72 |
+
$or: [
|
73 |
+
{ reps: { $eq: searchTerm } },
|
74 |
+
{ sets: { $eq: searchTerm } },
|
75 |
+
{ duration: { $eq: searchTerm } }
|
76 |
+
]
|
77 |
+
};
|
78 |
+
}
|
79 |
+
|
80 |
+
else {
|
81 |
+
if (req.query.filter === "category") {
|
82 |
+
query = { category: { $regex: searchTerm, $options: "i" } };
|
83 |
+
}
|
84 |
+
else {
|
85 |
+
query = {
|
86 |
+
$or: [
|
87 |
+
{ name: { $regex: searchTerm, $options: "i" } },
|
88 |
+
{ category: { $regex: searchTerm, $options: "i" } },
|
89 |
+
{ benefits: { $regex: searchTerm, $options: "i" } },
|
90 |
+
{ description: { $regex: searchTerm, $options: "i" } },
|
91 |
+
{ instructions: { $regex: searchTerm, $options: "i" } },
|
92 |
+
{ url: { $regex: searchTerm, $options: "i" } }
|
93 |
+
],
|
94 |
+
};
|
95 |
+
}
|
96 |
+
}
|
97 |
+
|
98 |
+
const { docs, paginationData } = await this.exercisesService.search(
|
99 |
+
query,
|
100 |
+
paginationQuery
|
101 |
+
);
|
102 |
+
|
103 |
+
return JsonResponse.success(
|
104 |
+
{
|
105 |
+
data: serialize(docs, ExerciseSerialization),
|
106 |
+
meta: paginationData,
|
107 |
+
},
|
108 |
+
res
|
109 |
+
);
|
110 |
+
};
|
111 |
}
|