Skip to content

Commit 26e8d0d

Browse files
committed
feat(generators): rename updatePartial to update & remove PUT update
1 parent 4647eaa commit 26e8d0d

File tree

8 files changed

+12
-95
lines changed

8 files changed

+12
-95
lines changed

generators/app/templates/<%= compNameParamCasePlural %>/controller.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,14 @@ const get = (req: Request, res: Response, next: NextFunction): void => {
5757
}
5858
};
5959

60-
const updatePartial = (
60+
const update = (
6161
req: Request,
6262
res: Response,
6363
next: NextFunction,
6464
): void => {
65-
try {
66-
// TODO: update partial object
67-
const object = { ...(req as MyRequest)[ENTITY], ...req.body };
68-
69-
res.send(object);
70-
} catch (err) {
71-
next(new HttpError(err.code ?? StatusCodes.INTERNAL_SERVER_ERROR, err));
72-
}
73-
};
74-
75-
const update = (req: Request, res: Response, next: NextFunction): void => {
7665
try {
7766
// TODO: update object
78-
const object = { ...req.body };
67+
const object = { ...(req as MyRequest)[ENTITY], ...req.body };
7968

8069
res.send(object);
8170
} catch (err) {
@@ -92,4 +81,4 @@ const del = (req: Request, res: Response, next: NextFunction): void => {
9281
}
9382
};
9483

95-
export default { getById, list, create, get, updatePartial, update, del };
84+
export default { getById, list, create, get, update, del };

generators/app/templates/<%= compNameParamCasePlural %>/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ const routes = (_: RouteOptions): Router => {
1616
router
1717
.route('/:id')
1818
.get(controller.get)
19-
.patch(controller.updatePartial)
20-
.put(controller.update)
19+
.patch(controller.update)
2120
.delete(controller.del);
2221

2322
return router;

generators/with-mongo/templates/<%= compNameParamCasePlural %>/controller.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,6 @@ const get = (req: Request, res: Response, next: NextFunction): void => {
7676
}
7777
};
7878

79-
const updatePartial = async (
80-
req: Request,
81-
res: Response,
82-
next: NextFunction,
83-
): Promise<void> => {
84-
try {
85-
console.log((req as MyRequest)[ENTITY]);
86-
// update partial object
87-
const object = await repository.updatePartial(
88-
(req as MyRequest)[ENTITY].id,
89-
req.body,
90-
);
91-
92-
res.send(object);
93-
} catch (err) {
94-
next(new HttpError(err.code ?? StatusCodes.INTERNAL_SERVER_ERROR, err));
95-
}
96-
};
97-
9879
const update = async (
9980
req: Request,
10081
res: Response,
@@ -128,4 +109,4 @@ const del = async (
128109
}
129110
};
130111

131-
export default { getById, list, create, get, updatePartial, update, del };
112+
export default { getById, list, create, get, update, del };

generators/with-mongo/templates/<%= compNameParamCasePlural %>/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ const routes = (_: RouteOptions): Router => {
1616
router
1717
.route('/:id')
1818
.get(controller.get)
19-
.patch(controller.updatePartial)
20-
.put(controller.update)
19+
.patch(controller.update)
2120
.delete(controller.del);
2221

2322
return router;

generators/with-mongo/templates/<%= compNameParamCasePlural %>/repository.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ const get = async (id: string): Promise<<%= compNamePascalCase %>> => {
2828
return transform(document);
2929
};
3030

31-
const updatePartial = async (id: string, object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
32-
// get document
33-
const document = await Model.findById(id).exec();
34-
if (document === null) {
35-
throw new MyError('Document not found');
36-
}
37-
38-
// update partial document
39-
document.set(object);
40-
await document.save();
41-
42-
return transform(document);
43-
};
44-
4531
const update = async (id: string, object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
4632
// get document
4733
const document = await Model.findById(id).exec();
@@ -50,7 +36,7 @@ const update = async (id: string, object: Omit<<%= compNamePascalCase %>, 'id'>)
5036
}
5137

5238
// update document
53-
document.overwrite(object);
39+
document.set(object);
5440
await document.save();
5541

5642
return transform(document);
@@ -78,4 +64,4 @@ const transform = (document: Document): <%= compNamePascalCase %> => {
7864
return { ...restObject, id };
7965
};
8066

81-
export default { list, create, get, updatePartial, update, del };
67+
export default { list, create, get, update, del };

generators/with-postgres/templates/<%= compNameParamCasePlural %>/controller.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,6 @@ const get = (req: Request, res: Response, next: NextFunction): void => {
7676
}
7777
};
7878

79-
const updatePartial = async (
80-
req: Request,
81-
res: Response,
82-
next: NextFunction,
83-
): Promise<void> => {
84-
try {
85-
console.log((req as MyRequest)[ENTITY]);
86-
// update partial object
87-
const object = await repository.updatePartial(
88-
(req as MyRequest)[ENTITY].id,
89-
req.body,
90-
);
91-
92-
res.send(object);
93-
} catch (err) {
94-
next(new HttpError(err.code ?? StatusCodes.INTERNAL_SERVER_ERROR, err));
95-
}
96-
};
97-
9879
const update = async (
9980
req: Request,
10081
res: Response,
@@ -128,4 +109,4 @@ const del = async (
128109
}
129110
};
130111

131-
export default { getById, list, create, get, updatePartial, update, del };
112+
export default { getById, list, create, get, update, del };

generators/with-postgres/templates/<%= compNameParamCasePlural %>/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ const routes = (_: RouteOptions): Router => {
1616
router
1717
.route('/:id')
1818
.get(controller.get)
19-
.patch(controller.updatePartial)
20-
.put(controller.update)
19+
.patch(controller.update)
2120
.delete(controller.del);
2221

2322
return router;

generators/with-postgres/templates/<%= compNameParamCasePlural %>/repository.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,6 @@ const get = async (id: number): Promise<<%= compNamePascalCase %>> => {
2828
return transform(instance);
2929
};
3030

31-
const updatePartial = async (
32-
id: number,
33-
object: Omit<<%= compNamePascalCase %>, 'id'>,
34-
): Promise<<%= compNamePascalCase %>> => {
35-
// get instance
36-
const instance = await Model.findByPk(id);
37-
if (instance === null) {
38-
throw new MyError('Instance not found');
39-
}
40-
41-
// update partial instance
42-
instance.set(object);
43-
await instance.save();
44-
45-
return transform(instance);
46-
};
47-
4831
const update = async (
4932
id: number,
5033
object: Omit<<%= compNamePascalCase %>, 'id'>,
@@ -56,7 +39,7 @@ const update = async (
5639
}
5740

5841
// update instance
59-
instance.set({ id, ...object }, { reset: true });
42+
instance.set(object);
6043
await instance.save();
6144

6245
return transform(instance);
@@ -80,4 +63,4 @@ const transform = (instance: Instance): <%= compNamePascalCase %> => {
8063
return instance.toJSON() as <%= compNamePascalCase %>;
8164
};
8265

83-
export default { list, create, get, updatePartial, update, del };
66+
export default { list, create, get, update, del };

0 commit comments

Comments
 (0)