diff --git a/api/campuses.js b/api/campuses.js new file mode 100644 index 0000000..7c297a2 --- /dev/null +++ b/api/campuses.js @@ -0,0 +1,72 @@ +const express = require("express"); +const router = express.Router(); +const { Campus } = require("../models"); +//Gets every single campus +router.get("/", async (req, res) => { + try { + const campus = await Campus.findAll(); + res.json(campus); + } catch (err) { + res.status(500).json({ error: "Failed to fetch campuses" }); + } +}); +router.post("/", async (req, res) => { + try { + const campus = await Campus.create(req.body); + res.status(201).json(campus); + } catch (err) { + res.status(400).json({ error: err.message }); + } +}); +//gets one campus specifically by ID +router.get("/:id", async (req, res) => { + try { + const campus = await Campus.findByPk(req.params.id); + if (campus) { + res.json(campus); + } else { + res.status(404).json({ error: "Campus not found" }); + } + } catch (error) { + res + .status(500) + .json({ error: "Failed to fetch the specified campus data" }); + } +}); +//deletes campuses by specified id +router.delete("/:id", async (req, res) => { + try { + const erased = await Campus.destroy({ + where: { id: req.params.id }, + }); + if (erased) { + res.status(204).send(); + } else { + res.status(404).json({ error: "Campus not found" }); + } + } catch (err) { + res.status(500).json({ error: "Failed to delete campus" }); + } +}); +router.put("/:id", async (req, res) => { + try { + const updated = await Campus.update(req.body, { + where: { id: req.params.id }, + }); + if (updated) { + const updatedCamp = await Campus.findByPk(req.params.id); + res.json({ + message: `Campus ${updatedCamp.name} has been updated.`, + campus: updatedCamp + }); + } else { + res.status(404).json({ error: "Campus not found! Failed to update.." }); + } + } catch (err) { + res.status(400).json({ + error: err.message, + }); + } +}); + +module.exports = router; diff --git a/api/index.js b/api/index.js index 5b4e809..e45dfec 100644 --- a/api/index.js +++ b/api/index.js @@ -1,7 +1,13 @@ const express = require("express"); const router = express.Router(); const ducksRouter = require("./ducks"); +const campusesRouter = require("./campuses"); +const studentsRouter = require("./students"); + router.use("/ducks", ducksRouter); +router.use("/campuses", campusesRouter); +router.use("/students", studentsRouter); + module.exports = router; diff --git a/api/students.js b/api/students.js new file mode 100644 index 0000000..4f01d50 --- /dev/null +++ b/api/students.js @@ -0,0 +1,70 @@ +console.log("Student route") +const express = require("express"); +const router = express.Router(); +const { Student } = require("../models"); +//Gets every single student +router.get("/", async (req, res) => { + try { + const students = await Student.findAll(); + res.json(students); + } catch (err) { + res.status(500).json({ error: "Failed to fetch students data" }); + } +}); +router.post("/", async (req, res) => { + try { + const students = await Student.create(req.body); + res.status(201).json(students); + } catch (err) { + res.status(400).json({ error: err.message }); + } +}); +//gets one student specifically by ID +router.get("/:id", async (req, res) => { + try { + const students = await Student.findByPk(req.params.id); + if (students) { + res.json(students); + } else { + res.status(404).json({ error: "Student not found" }); + } + } catch (error) { + res + .status(500) + .json({ error: "Failed to fetch the specified student data" }); + } +}); +//deletes students by specified id +router.delete("/:id", async (req, res) => { + try { + const user = await Student.findByPk(req.params.id); + if (!user) { + return res.status(404).json({ error: "User not found" }); + } + await user.destroy(); + res.sendStatus(200); + } catch (error) { + console.error(error); + res.status(500).json({ error: "Failed to delete user" }); + } +}); +router.put("/:id", async (req, res) => { + try { + const user = await Student.findByPk(req.params.id); + if (!user) { + return res.status(404).json({ error: "User not found" }); + } + + await user.update(req.body); + + res.json({ + message: `User ${user.firstName} ${user.lastName} has been updated.`, + student: user, + }); + } catch (error) { + console.error("❌ PUT error:", error); + res.status(500).json({ error: "Failed to update user" }); + } +}); + +module.exports = router; diff --git a/database/db.js b/database/db.js index 0e44c8d..d37f2f9 100644 --- a/database/db.js +++ b/database/db.js @@ -6,7 +6,7 @@ const pg = require("pg"); const dbName = "ttp_crud"; const db = new Sequelize( - process.env.DATABASE_URL || `postgres://localhost:5432/${dbName}`, + process.env.DATABASE_URL || `postgres://postgres:BilalBomb1*@localhost:5432/${dbName}`, { logging: false, // comment this line to enable logging } diff --git a/models/campus.js b/models/campus.js new file mode 100644 index 0000000..4ef2845 --- /dev/null +++ b/models/campus.js @@ -0,0 +1,38 @@ +const { Sequelize, DataTypes } = require("sequelize"); + +const { db } = require("../database"); + +const campus = db.define("campus", { + name: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notEmpty: true, + }, + }, + address: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notEmpty: true, + }, + }, + imageUrl: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: "https://cdn-icons-png.flaticon.com/512/68/68286.png", + }, + description: { + type: DataTypes.TEXT("long"), + allowNull: false, + validate: { + notEmpty: true, + }, + }, +}); + +campus.sync().then(() => { + console.log("Campus tables synced!✅"); +}); + +module.exports = campus; diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..e62bbaa --- /dev/null +++ b/models/index.js @@ -0,0 +1,11 @@ +const { db } = require("../database"); +const campus = require("./campus"); +const Student = require("./student"); +campus.hasMany(Student); +Student.belongsTo(campus); + +module.exports = { + db, + Student, + campus, +}; diff --git a/models/student.js b/models/student.js new file mode 100644 index 0000000..23415b1 --- /dev/null +++ b/models/student.js @@ -0,0 +1,53 @@ +const { Sequelize, DataTypes } = require("sequelize"); + +const { db } = require("../database"); + +const Student = db.define("students", { + firstName: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notEmpty: true, + }, + }, + lastName: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notEmpty: true, + }, + }, + email: { + type: DataTypes.TEXT, + allowNull: false, + validate: { + notEmpty: true, + isEmail: true, + }, + }, + imageUrl: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: "https://cdn-icons-png.flaticon.com/512/70/70906.png", + }, + gpa: { + type: DataTypes.DECIMAL(3, 2), + allowNull: false, + validate: { + min: 0.0, + max: 4.0, + }, + }, + description: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notEmpty: true, + }, + }, +}); +Student.sync().then(() => { + console.log("Student tables synced!✅"); +}); + +module.exports = Student;