Skip to content
Open
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
72 changes: 72 additions & 0 deletions api/campuses.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -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;
70 changes: 70 additions & 0 deletions api/students.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion database/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 38 additions & 0 deletions models/campus.js
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
53 changes: 53 additions & 0 deletions models/student.js
Original file line number Diff line number Diff line change
@@ -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;