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:
2021-02-18 21:05:40 +01:00
parent fc029f80df
commit d0fa89b92b
2 changed files with 54 additions and 0 deletions

View File

@@ -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
};