Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back end #26

Merged
merged 7 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 78 additions & 67 deletions server/controllers/alert.controller.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,80 @@
import db from "../database/db.js";
import db from '../database/db.js'

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 });
},
};

export default alertController;
getAlert: async (req, res) => {
const { id } = req.params

const alert = await db.query(
`SELECT * FROM "Notification" WHERE "NotificationId" = $1;`,
[id]
)

if (!alert.rowCount)
return res.status(404).json({ error: 'Alert not found' })

res.status(200).json({
message: 'Alert successfully found',
body: alert.rows[0],
})
},

CreateAlert: async (req, res) => {
const { message, type } = req.body

const newAlert = await db.query(
`INSERT INTO "Notification" ("message")
VALUES($1) RETURNING *;`,
[message]
)

if (!newAlert.rowCount)
return res.status(400).json({ error: 'Cannot Post' })

res.status(200).json({
message: 'Alert successfully created',
body: newAlert.rows[0],
})
},

DeleteAlert: async (req, res) => {
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" = $1;`,
[id]
)

alertsArr.filter((item) => item.NotificationId !== Number(id))
return res.status(200).json({
message: 'Alert successfully deleted',
body: alertsArr,
})
} catch (error) {
console.error(error)
res.status(400).json({
error: 'An error occured while deleting alert',
})
}
},

getAllAlerts: async (req, res) => {
const Alerts = await db.query(`SELECT * FROM "Notification";`)

res.status(200).json({
message: 'get Alerts successfully',
body: Alerts.rows,
})
},
}

export default alertController
4 changes: 2 additions & 2 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const authController = {
// @access Private
me: (req, res) => {
try {
res.status(200).json({ user: req.captain })
res.status(200).json({ message: 'You are in', body: req.captain })
} catch (error) {
console.log(error)
res.status(500).json({
Expand All @@ -149,4 +149,4 @@ const authController = {
},
}

export default authController
export default authController
Loading