Skip to content

Commit

Permalink
feat: update a project using ID
Browse files Browse the repository at this point in the history
note: cannot update the contributors as of now.

related #6
  • Loading branch information
saihaj committed Jul 14, 2020
1 parent 4e926b3 commit f707f3d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/api-docs/routes/projects/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
<details>
<Summary>Sample Request</Summary>
<pre>
{
"name": "wonderful API 3",
}
</pre>
</details>

<!-- Sample Response -->
<details>
<Summary>Sample Response</Summary>
<pre>
{
"message": "Updated the following items",
"updatedFields": {
"name": "wonderful API 3"
}
}
</pre>
</details>
32 changes: 32 additions & 0 deletions routes/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f707f3d

Please # to comment.