-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature #159068502] Add /PUT/:id entries endpoint
- Loading branch information
1 parent
038fc80
commit cfcce8f
Showing
5 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { validationResult } from 'express-validator/check'; | ||
import entries from '../db/entries'; | ||
|
||
const modifyEntry = (req, res) => { | ||
const error = validationResult(req); | ||
const indexOfFound = entries.findIndex(entry => entry.id === Number(req.params.id)); | ||
|
||
if (indexOfFound > -1 && error.isEmpty()) { | ||
const found = { ...entries[indexOfFound] }; | ||
entries[indexOfFound] = { ...found, ...req.body }; | ||
return res.status(200).json(entries[indexOfFound]); | ||
} | ||
if (indexOfFound === -1) { | ||
return res.status(404).json({ errors: [{ msg: 'Entry does not exist' }] }); | ||
} | ||
return res.status(400).json({ errors: error.array() }); | ||
}; | ||
|
||
export default modifyEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters