Update lottery winner by id.
Endpoint, controller and repository function for updating lottery winner by id and json payload. Keeps previous definition for undefined/null values.
This commit is contained in:
		| @@ -98,6 +98,36 @@ const winnerById = (req, res) => { | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| const updateWinnerById = (req, res) => { | ||||
|   const { id } = req.params; | ||||
|   const { winner } = req.body; | ||||
|  | ||||
|   if (id == null || id == "undefined") { | ||||
|     return res.status(400).send({ | ||||
|       message: "Unable to update without id.", | ||||
|       success: false | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   return winnerRepository | ||||
|     .updateWinnerById(id, winner) | ||||
|     .then(winner => | ||||
|       res.send({ | ||||
|         winner, | ||||
|         message: `Updated winner: ${winner.name}`, | ||||
|         success: true | ||||
|       }) | ||||
|     ) | ||||
|     .catch(error => { | ||||
|       const { statusCode, message } = error; | ||||
|  | ||||
|       return res.status(statusCode || 500).send({ | ||||
|         message: message || "Unexpected error occured while updating winner by id.", | ||||
|         success: false | ||||
|       }); | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| const deleteWinnerById = (req, res) => { | ||||
|   const isAdmin = req.isAuthenticated(); | ||||
|   const { id } = req.params; | ||||
| @@ -152,6 +182,7 @@ module.exports = { | ||||
|   addWinners, | ||||
|   allWinners, | ||||
|   winnerById, | ||||
|   updateWinnerById, | ||||
|   deleteWinnerById, | ||||
|   deleteWinners | ||||
| }; | ||||
|   | ||||
| @@ -43,6 +43,28 @@ const winnerById = (id, isAdmin = false) => { | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| const updateWinnerById = (id, updateModel) => { | ||||
|   return VirtualWinner.findOne({ id: id }).then(winner => { | ||||
|     if (winner == null) { | ||||
|       throw new WinnerNotFound(); | ||||
|     } | ||||
|  | ||||
|     const updatedWinner = { | ||||
|       name: updateModel.name != null ? updateModel.name : winner.name, | ||||
|       phoneNumber: updateModel.phoneNumber != null ? updateModel.phoneNumber : winner.phoneNumber, | ||||
|       red: updateModel.red != null ? updateModel.red : winner.red, | ||||
|       green: updateModel.green != null ? updateModel.green : winner.green, | ||||
|       blue: updateModel.blue != null ? updateModel.blue : winner.blue, | ||||
|       yellow: updateModel.yellow != null ? updateModel.yellow : winner.yellow, | ||||
|       timestamp_drawn: updateModel.timestamp_drawn != null ? updateModel.timestamp_drawn : winner.timestamp_drawn, | ||||
|       timestamp_limit: updateModel.timestamp_limit != null ? updateModel.timestamp_limit : winner.timestamp_limit, | ||||
|       timestamp_sent: updateModel.timestamp_sent != null ? updateModel.timestamp_sent : winner.timestamp_sent | ||||
|     }; | ||||
|  | ||||
|     return VirtualWinner.updateOne({ id: id }, updatedWinner).then(_ => updatedWinner); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| const deleteWinnerById = id => { | ||||
|   return VirtualWinner.findOne({ _id: id }).then(winner => { | ||||
|     if (winner == null) { | ||||
| @@ -61,6 +83,7 @@ module.exports = { | ||||
|   addWinners, | ||||
|   allWinners, | ||||
|   winnerById, | ||||
|   updateWinnerById, | ||||
|   deleteWinnerById, | ||||
|   deleteWinners | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user