Spaces:
Running
Running
Commit
·
61c7d9c
1
Parent(s):
b798149
update: update crud service
Browse files
src/lib/services/crud.service.ts
CHANGED
@@ -11,14 +11,26 @@ export const CrudService = <ModelDoc extends Document>(
|
|
11 |
return this.model.create(data);
|
12 |
}
|
13 |
|
14 |
-
async
|
15 |
filter: FilterQuery<ModelDoc>,
|
16 |
data: AnyKeys<ModelDoc>
|
17 |
): Promise<ModelDoc> {
|
18 |
-
|
|
|
|
|
19 |
}
|
20 |
|
21 |
-
async
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return this.model.findOneAndDelete(filter);
|
23 |
}
|
24 |
|
@@ -59,10 +71,16 @@ export const CrudService = <ModelDoc extends Document>(
|
|
59 |
}
|
60 |
|
61 |
async findOneOrFail(filter: FilterQuery<ModelDoc>): Promise<ModelDoc> {
|
|
|
62 |
const document = await this.findOne(filter);
|
63 |
-
if (!document) throw new HttpError(404, "No Matching Result Found.");
|
64 |
|
65 |
return document;
|
66 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
};
|
68 |
};
|
|
|
11 |
return this.model.create(data);
|
12 |
}
|
13 |
|
14 |
+
async updateOne(
|
15 |
filter: FilterQuery<ModelDoc>,
|
16 |
data: AnyKeys<ModelDoc>
|
17 |
): Promise<ModelDoc> {
|
18 |
+
await this.existsOrThrow(filter);
|
19 |
+
await this.model.updateOne(filter, data);
|
20 |
+
return this.findOneOrFail(filter);
|
21 |
}
|
22 |
|
23 |
+
async updateMany(
|
24 |
+
filter: FilterQuery<ModelDoc>,
|
25 |
+
data: AnyKeys<ModelDoc>
|
26 |
+
): Promise<ModelDoc[]> {
|
27 |
+
await this.existsOrThrow(filter);
|
28 |
+
await this.model.updateMany(filter, data);
|
29 |
+
return this.model.find(filter);
|
30 |
+
}
|
31 |
+
|
32 |
+
async deleteOne(filter: FilterQuery<ModelDoc>): Promise<ModelDoc> {
|
33 |
+
await this.existsOrThrow(filter);
|
34 |
return this.model.findOneAndDelete(filter);
|
35 |
}
|
36 |
|
|
|
71 |
}
|
72 |
|
73 |
async findOneOrFail(filter: FilterQuery<ModelDoc>): Promise<ModelDoc> {
|
74 |
+
await this.existsOrThrow(filter);
|
75 |
const document = await this.findOne(filter);
|
|
|
76 |
|
77 |
return document;
|
78 |
}
|
79 |
+
|
80 |
+
private async existsOrThrow(filter: FilterQuery<ModelDoc>) {
|
81 |
+
if (!(await this.model.exists(filter))) {
|
82 |
+
throw new HttpError(404, "No Matching Result Found.");
|
83 |
+
}
|
84 |
+
}
|
85 |
};
|
86 |
};
|
src/modules/console/modules/admins/controllers/admins.controller.ts
CHANGED
@@ -81,7 +81,7 @@ export class AdminsController extends BaseController {
|
|
81 |
};
|
82 |
|
83 |
update = async (req: Request, res: Response): Promise<Response> => {
|
84 |
-
const admin = await this.adminsService.
|
85 |
{
|
86 |
_id: req.params.id,
|
87 |
},
|
@@ -97,7 +97,7 @@ export class AdminsController extends BaseController {
|
|
97 |
};
|
98 |
|
99 |
delete = async (req: Request, res: Response): Promise<Response> => {
|
100 |
-
const admin = await this.adminsService.
|
101 |
_id: req.params.id,
|
102 |
});
|
103 |
|
|
|
81 |
};
|
82 |
|
83 |
update = async (req: Request, res: Response): Promise<Response> => {
|
84 |
+
const admin = await this.adminsService.updateOne(
|
85 |
{
|
86 |
_id: req.params.id,
|
87 |
},
|
|
|
97 |
};
|
98 |
|
99 |
delete = async (req: Request, res: Response): Promise<Response> => {
|
100 |
+
const admin = await this.adminsService.deleteOne({
|
101 |
_id: req.params.id,
|
102 |
});
|
103 |
|
src/modules/console/modules/workouts/controllers/workouts.controller.ts
CHANGED
@@ -78,7 +78,7 @@ export class WorkoutController extends BaseController {
|
|
78 |
};
|
79 |
|
80 |
update = async (req: Request, res: Response) => {
|
81 |
-
const data = await this.workoutsService.
|
82 |
{ _id: req.params.id },
|
83 |
req.body
|
84 |
);
|
@@ -91,7 +91,7 @@ export class WorkoutController extends BaseController {
|
|
91 |
};
|
92 |
|
93 |
delete = async (req: Request, res: Response) => {
|
94 |
-
const data = await this.workoutsService.
|
95 |
return JsonResponse.success(
|
96 |
{
|
97 |
data: serialize(data.toJSON(), WorkoutSerialization),
|
|
|
78 |
};
|
79 |
|
80 |
update = async (req: Request, res: Response) => {
|
81 |
+
const data = await this.workoutsService.updateOne(
|
82 |
{ _id: req.params.id },
|
83 |
req.body
|
84 |
);
|
|
|
91 |
};
|
92 |
|
93 |
delete = async (req: Request, res: Response) => {
|
94 |
+
const data = await this.workoutsService.deleteOne({ _id: req.params.id });
|
95 |
return JsonResponse.success(
|
96 |
{
|
97 |
data: serialize(data.toJSON(), WorkoutSerialization),
|