-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from CMP26Projects/Notifications_Backend
Alerts Backend in progress
- Loading branch information
Showing
4 changed files
with
96 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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; |
This file contains 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
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 |
This file contains 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
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; |