Skip to content

Commit

Permalink
Merge pull request #13 from CMP26Projects/Notifications_Backend
Browse files Browse the repository at this point in the history
Alerts Backend in progress
  • Loading branch information
AbdelruhmanSamy authored Dec 23, 2023
2 parents 55c2f18 + 7a09d7f commit 243ebf3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 16 deletions.
19 changes: 10 additions & 9 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ const app = express()
const PORT = process.env.PORT || 3000

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(cookieParser())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.use('/api', apiRouter)
app.use("/alert", alertRouter);
app.use(notFound)
app.use(errorHandler)

app.listen(PORT, (err) => {
if (err) return console.error(err)
console.log(`Server started listening at port ${PORT}`)
})
if (err) return console.error(err);
console.log(`Server started listening at port ${PORT}`);
});
69 changes: 69 additions & 0 deletions server/controllers/alert.controller.js
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 8 additions & 7 deletions server/database/db.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import pg from 'pg'
import dotenv from '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,
});


export default db
9 changes: 9 additions & 0 deletions server/routes/alert.route.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 243ebf3

Please sign in to comment.