Spaces:
Sleeping
Sleeping
Merge pull request #90 from Modarb-Ai-Trainer/search
Browse files
src/modules/users/modules/exercises/controllers/exercises.controller.ts
CHANGED
@@ -99,6 +99,8 @@ export class UsersExerciseController extends BaseController {
|
|
99 |
@SwaggerSummary("Search for exercises")
|
100 |
@SwaggerDescription("You can use filters in search like category")
|
101 |
@SwaggerQuery({
|
|
|
|
|
102 |
searchTerm: "string",
|
103 |
filter: "string"
|
104 |
})
|
@@ -108,6 +110,8 @@ export class UsersExerciseController extends BaseController {
|
|
108 |
let searchTerm = req.query.searchTerm;
|
109 |
let isNum = !isNaN(parseInt(String(searchTerm)));
|
110 |
|
|
|
|
|
111 |
if (isNum) {
|
112 |
query =
|
113 |
{
|
@@ -120,19 +124,15 @@ export class UsersExerciseController extends BaseController {
|
|
120 |
}
|
121 |
|
122 |
else {
|
123 |
-
if (
|
124 |
-
query = {
|
|
|
|
|
|
|
125 |
}
|
126 |
else {
|
127 |
query = {
|
128 |
-
$
|
129 |
-
{ name: { $regex: searchTerm, $options: "i" } },
|
130 |
-
{ category: { $regex: searchTerm, $options: "i" } },
|
131 |
-
{ benefits: { $regex: searchTerm, $options: "i" } },
|
132 |
-
{ description: { $regex: searchTerm, $options: "i" } },
|
133 |
-
{ instructions: { $regex: searchTerm, $options: "i" } },
|
134 |
-
{ url: { $regex: searchTerm, $options: "i" } }
|
135 |
-
],
|
136 |
};
|
137 |
}
|
138 |
}
|
|
|
99 |
@SwaggerSummary("Search for exercises")
|
100 |
@SwaggerDescription("You can use filters in search like category")
|
101 |
@SwaggerQuery({
|
102 |
+
limit: "number",
|
103 |
+
skip: "number",
|
104 |
searchTerm: "string",
|
105 |
filter: "string"
|
106 |
})
|
|
|
110 |
let searchTerm = req.query.searchTerm;
|
111 |
let isNum = !isNaN(parseInt(String(searchTerm)));
|
112 |
|
113 |
+
let filterVal = req.query.filter;
|
114 |
+
|
115 |
if (isNum) {
|
116 |
query =
|
117 |
{
|
|
|
124 |
}
|
125 |
|
126 |
else {
|
127 |
+
if (filterVal) {
|
128 |
+
query = {
|
129 |
+
category: filterVal,
|
130 |
+
name: { $regex: searchTerm, $options: "i" }
|
131 |
+
};
|
132 |
}
|
133 |
else {
|
134 |
query = {
|
135 |
+
name: { $regex: searchTerm, $options: "i" }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
};
|
137 |
}
|
138 |
}
|
src/modules/users/modules/ingredients/controller/ingredients.controller.ts
CHANGED
@@ -24,6 +24,7 @@ export class UsersIngredientsController extends BaseController {
|
|
24 |
|
25 |
setRoutes(): void {
|
26 |
this.router.get("/", asyncHandler(this.list));
|
|
|
27 |
}
|
28 |
|
29 |
@SwaggerGet()
|
@@ -59,4 +60,55 @@ export class UsersIngredientsController extends BaseController {
|
|
59 |
res
|
60 |
);
|
61 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
}
|
|
|
24 |
|
25 |
setRoutes(): void {
|
26 |
this.router.get("/", asyncHandler(this.list));
|
27 |
+
this.router.get("/search", asyncHandler(this.search));
|
28 |
}
|
29 |
|
30 |
@SwaggerGet()
|
|
|
60 |
res
|
61 |
);
|
62 |
};
|
63 |
+
|
64 |
+
@SwaggerGet('/search')
|
65 |
+
@SwaggerResponse([IngredientSerialization])
|
66 |
+
@SwaggerSummary("Search for Ingredients")
|
67 |
+
@SwaggerDescription("You can search for ingredients by entering characters or numbers.")
|
68 |
+
@SwaggerQuery({
|
69 |
+
limit: "number",
|
70 |
+
skip: "number",
|
71 |
+
searchTerm: "string",
|
72 |
+
})
|
73 |
+
search = async (req: Request, res: Response): Promise<Response> => {
|
74 |
+
const paginationQuery = parsePaginationQuery(req.query);
|
75 |
+
let query = {};
|
76 |
+
let searchTerm = req.query.searchTerm;
|
77 |
+
let isNum = !isNaN(parseInt(String(searchTerm)));
|
78 |
+
|
79 |
+
|
80 |
+
if (isNum) {
|
81 |
+
query =
|
82 |
+
{
|
83 |
+
$or: [
|
84 |
+
{ fats: { $eq: searchTerm } },
|
85 |
+
{ proteins: { $eq: searchTerm } },
|
86 |
+
{ proteins: { $eq: searchTerm } },
|
87 |
+
{ carbs: { $eq: searchTerm } },
|
88 |
+
{ calories: { $eq: searchTerm } },
|
89 |
+
{ serving_size: { $eq: searchTerm } },
|
90 |
+
{ servings_count: { $eq: searchTerm } }
|
91 |
+
]
|
92 |
+
};
|
93 |
+
}
|
94 |
+
|
95 |
+
else {
|
96 |
+
query = {
|
97 |
+
name: { $regex: searchTerm, $options: "i" }
|
98 |
+
}
|
99 |
+
}
|
100 |
+
|
101 |
+
const { docs, paginationData } = await this.ingredientsService.search(
|
102 |
+
query,
|
103 |
+
paginationQuery,
|
104 |
+
);
|
105 |
+
|
106 |
+
return JsonResponse.success(
|
107 |
+
{
|
108 |
+
data: serialize(docs, IngredientSerialization),
|
109 |
+
meta: paginationData,
|
110 |
+
},
|
111 |
+
res
|
112 |
+
);
|
113 |
+
};
|
114 |
}
|