diff --git a/docs/api-docs/routes/projects/guide.md b/docs/api-docs/routes/projects/guide.md index 988ed7b..5c70688 100644 --- a/docs/api-docs/routes/projects/guide.md +++ b/docs/api-docs/routes/projects/guide.md @@ -106,3 +106,37 @@ Fields: --- 1. Checkout [momentJS docs](https://momentjs.com/docs/#/parsing/string/) for formating date and time. --- + + +## PATCH + +### Modify by project ID +Modify a project + +Route: `/projects/:projectId` + +Use any of the fields in create a project except contributors field. +Note: Currently updating contributors filed is not supported + + +
+ Sample Request +
+        {
+        "name": "wonderful API 3",
+        }
+    
+
+ + +
+ Sample Response +
+        {
+            "message": "Updated the following items",
+            "updatedFields": {
+                "name": "wonderful API 3"
+            }
+        }
+    
+
diff --git a/routes/projects.js b/routes/projects.js index 7c2a96b..537ad0e 100644 --- a/routes/projects.js +++ b/routes/projects.js @@ -44,4 +44,36 @@ app.post( '/', async ( { body }, res, next ) => { } catch ( err ) { return next( err ) } } ) +// Update a project using ID +app.patch( '/:projectId', async ( + { + body, + params: { projectId }, + }, + res, + next, +) => { + try { + if ( await GetItemById( ProjectSchema, projectId ) ) { + const updatedFields = {} + + // Add all the incoming fields to object + Object.keys( body ) + .filter( key => key !== 'contributors' ) // Doesn't support updating contributors + .forEach( key => { updatedFields[ key ] = body[ key ] } ) + + // Format dates + Object.keys( body ) + .filter( key => key === 'createdDate' ) + .forEach( key => { updatedFields[ key ] = FormatDateTime( body[ key ] ) } ) + + // update the fields in DB + await ProjectSchema.updateOne( { _id: projectId }, { $set: updatedFields } ).exec() + + return res.json( { message: 'Updated the following items', updatedFields } ) + } + return next( DnE( projectId ) ) + } catch ( err ) { return next( err ) } +} ) + export default app