From fc3d5ac31a6a9f898545937d8e6e430cb683551b Mon Sep 17 00:00:00 2001 From: moepoi Date: Sun, 21 Nov 2021 12:30:35 +0700 Subject: [PATCH] Add subscribe API --- routes/api.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/routes/api.js b/routes/api.js index 79f1a6c..541c57d 100644 --- a/routes/api.js +++ b/routes/api.js @@ -448,4 +448,49 @@ router.post("/transaction", async (req, res) => { } }); +router.get("/subscribe", (req, res) => { + if (!req.query.email) { + res.status(400); + res.json({ + message: "Bad Request", + }); + } else { + const conn = db.connect(); + conn.execute( + "SELECT * FROM `subscribe` WHERE `email` = ?", + [req.query.email], + function (err, results) { + if (!results[0]) { + let data = { + email: req.query.email, + type: "email" + }; + conn.query( + "INSERT INTO subscribe SET ?", + data, + function (err, resp) { + if (err) { + res.status(400); + res.json({ + message: "Bad Request", + }); + } else { + res.status(201); + res.json({ + message: "Success", + }); + } + } + ); + } else { + res.status(200); + res.json({ + message: "Success", + }); + } + } + ); + } +}); + module.exports = router; \ No newline at end of file