From 7dda4a2bab4e9ab8bbb2ce1747900837fc94aa48 Mon Sep 17 00:00:00 2001 From: "[Abdelruhman Sami]" <[abdelruhamn.mohamad04@eng-stu.cu.edu.eg]> Date: Sat, 23 Dec 2023 19:45:57 +0200 Subject: [PATCH] Alerts Backend in progress --- server/.env.example | 22 -------- server/app.js | 36 +++++++------- server/controllers/alert.controller.js | 69 ++++++++++++++++++++++++++ server/controllers/auth.controller.js | 2 +- server/database/db.js | 20 ++++---- server/routes/alert.route.js | 9 ++++ 6 files changed, 108 insertions(+), 50 deletions(-) delete mode 100644 server/.env.example create mode 100644 server/controllers/alert.controller.js create mode 100644 server/routes/alert.route.js diff --git a/server/.env.example b/server/.env.example deleted file mode 100644 index 59dfe3ad..00000000 --- a/server/.env.example +++ /dev/null @@ -1,22 +0,0 @@ -# DB Config Variables -DB_HOST= -DB_PORT= -DB_USER= -DB_PASS= -DB_DATABASE= - -# Token Config Variables -JWT_SECRET= -JWT_EXPIRES_IN= - -########## Example ########## -# DB_HOST=localhost -# DB_PORT=5432 -# DB_USER=postgres -# DB_PASS=******** -# DB_DATABASE=scoutsManagementSystem - -# To get a random string for JWT_SECRET -# Run This in terminal => node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" - -# JWT_EXPIRES_IN='48h' \ No newline at end of file diff --git a/server/app.js b/server/app.js index 622cdf5b..a493e2a0 100644 --- a/server/app.js +++ b/server/app.js @@ -1,25 +1,27 @@ -const express = require('express') -const cors = require('cors') -const db = require('./database/db') -const app = express() -const PORT = process.env.PORT || 3000 -const authRouter = require('./routes/auth.route') +const express = require("express"); +const cors = require("cors"); +const db = require("./database/db"); +const app = express(); +const PORT = process.env.PORT || 3000; +const authRouter = require("./routes/auth.route"); +const alertRouter = require("./routes/alert.route"); db.connect() - .then(() => { - console.log('Database is connected') - }) - .catch((err) => { - if (err) return console.error(err) - }) + .then(() => { + console.log("Database is connected"); + }) + .catch((err) => { + if (err) return console.error(err); + }); -app.use(cors()) -app.use(express.json()) +app.use(cors()); +app.use(express.json()); // Routes app.use("/auth", authRouter); +app.use("/alert", alertRouter); app.listen(PORT, (err) => { - if (err) return console.error(err) - console.log(`Server started listening at port ${PORT}`) -}) \ No newline at end of file + if (err) return console.error(err); + console.log(`Server started listening at port ${PORT}`); +}); diff --git a/server/controllers/alert.controller.js b/server/controllers/alert.controller.js new file mode 100644 index 00000000..d8b276e7 --- /dev/null +++ b/server/controllers/alert.controller.js @@ -0,0 +1,69 @@ +const db = require("../database/db"); + +const alertController = { + getAlert: async (req, res) => { + console.log(req.params); + const { id } = req.params; //string + + const alert = await db.query( + `SELECT * FROM "Notification" WHERE "NotificationId" = ${Number(id)};` + ); + + if (!alert) return res.status(404).json({ error: "Alert not found" }); + + res + .status(200) + .json({ success: true, message: "Alert successfully found", alert }); + }, + + CreateAlert: async (req, res) => { + console.log(req.body); + const { title, message, type } = req.body; + if (!title || !message || !type) + return res.status(400).json({ error: "Missing input" }); + + const newAlert = await db.query( + `INSERT INTO "Notification" ( "message" ) + VALUES( '${message}' ) RETURNING *;` + ); + + if (!newAlert) return res.status(400).json({ error: "Cannot Post" }); + + res.status(200).json({ success: true, newAlert }); + }, + + DeleteAlert: async (req, res) => { + console.log(req.params); + const { id } = req.params; + + const Alerts = await db.query(`SELECT * FROM "Notification";`); + + const alertsArr = Alerts.rows; + + if (!alertsArr.find((item) => item.NotificationId === Number(id))) + return res.status(404).json({ error: "Alert to be deleted not found" }); + + try { + await db.query( + `DELETE FROM "Notification" WHERE "NotificationId" = ${Number(id)};` + ); + + alertsArr.filter((item) => item.NotificationId !== Number(id)); + return res.status(200).json({ success: true }); + } catch (error) { + console.error(error); + res.status(400).json({ success: false }); + } + }, + + getAllAlerts: async (req, res) => { + const Alerts = await db.query(`SELECT * FROM "Notification";`); + + if (!Alerts.rows.length) + return res.status(400).json({ error: "No alerts found" }); + + res.status(200).json({ status: true, data: Alerts.rows }); + }, +}; + +module.exports = alertController; diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index 27840b52..b577e2c3 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -36,7 +36,7 @@ const authController = { // Generate a JWT token containing the captain's id // Bearer token is the token that we will send to the client - const token = jwt.sign( + const token = jwt.sign( { id: newCaptain.captainId }, // Payload process.env.JWT_SECRET, { diff --git a/server/database/db.js b/server/database/db.js index bacd5a53..c97b4b00 100644 --- a/server/database/db.js +++ b/server/database/db.js @@ -1,13 +1,13 @@ -const pg = require('pg') -const dotenv = require('dotenv') +const pg = require("pg"); +const dotenv = require("dotenv"); -dotenv.config() +dotenv.config(); const db = new pg.Pool({ - host: process.env.DB_HOST, - port: process.env.DB_PORT, - user: process.env.DB_USER, - password: process.env.DB_PASS, - database: process.env.DB_DATABASE, -}) + host: process.env.DB_HOST, + port: process.env.DB_PORT, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_DATABASE, +}); -module.exports = db \ No newline at end of file +module.exports = db; diff --git a/server/routes/alert.route.js b/server/routes/alert.route.js new file mode 100644 index 00000000..03ad320b --- /dev/null +++ b/server/routes/alert.route.js @@ -0,0 +1,9 @@ +const alertRouter = require("express").Router(); +const alertController = require("../controllers/alert.controller"); + +alertRouter.get("/", alertController.getAllAlerts); +alertRouter.get("/:id", alertController.getAlert); +alertRouter.post("/post", alertController.CreateAlert); +alertRouter.delete("/:id", alertController.DeleteAlert); + +module.exports = alertRouter;