Created
September 28, 2021 08:07
-
-
Save hieptl/bb0f408e2542666455ecd700e6b4b895 to your computer and use it in GitHub Desktop.
requests.js - API - Update Matching Request - Tinder Clone
This file contains hidden or 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
const getUpdateRequest = (id, status) => { | |
if (status && status === constants.matchRequestStatus.accepted) { | |
return { | |
payload: [status, new Date(), id], | |
sql: "UPDATE match_request SET match_request_status = ?, accepted_date = ? WHERE id = ?" | |
}; | |
} | |
return { | |
payload: [status, id], | |
sql: "UPDATE match_request SET match_request_status = ? WHERE id = ?" | |
}; | |
} | |
app.post('/requests/update', (req, res) => { | |
const { id, status } = req.body; | |
if (id && status) { | |
const sql = "SELECT * FROM match_request WHERE id = ?"; | |
dbConn.query(sql, [id], function (err, result) { | |
if (err) { | |
res.status(200).jsonp({ message: "The system error, cannot update the match request. Please try again" }); | |
} else if (!result || result.length === 0) { | |
res.status(200).jsonp({ message: "The match request did not exist in the system" }); | |
} else { | |
const updateRequest = getUpdateRequest(id, status); | |
dbConn.query(updateRequest.sql, updateRequest.payload, function (error, result) { | |
if (error || !result || result.length === 0) { | |
res.status(200).jsonp({ message: 'Cannot update the match request. Please try again' }); | |
} else { | |
res.status(200).jsonp({ message: 'The match request was updated successfully' }); | |
} | |
}); | |
} | |
}); | |
} else { | |
res.status(200).jsonp({ message: "Please provide id and status for the match request" }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment