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
84 changes: 84 additions & 0 deletions api/campus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const express = require("express");
const router = express.Router();
const { Student, Campus, Campus } = require("../database");

// get all campaus
router.get("/", async (req, res) => {
try {
const campus = await Campus.findAll(); //it looks through the table and and checks all the rows
res.status(200).send(campus); //returns a sucessfull
} catch (error) {
//if the condtion is not met return error
console.log("error has accrued ");
}
console.log(campus);
});

// req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like
// res' (response) which is used to send the HTTP response to the client which allows the modification of headers and
// consists of status codes, and resources.

// GET campus by ID
router.get("/id", async (req, res) => {
//Acess the primary key from the user URL
try {
const campausID = Number(req.params.id); //Acess the primary key from the user URL
const campus = await Campus.findByPk(campausID); //find a entry from a table using provied key
if (campus === null); //if it null no campus with that id exists
return res.sendStatus(404); //send a 404 not found response
const students = await Student.findAll({ where: { campusId: campusID } });
const campusDetails = {
campus: campus,
students: students,
};
res.status(200).send(campusDetails);
} catch (error) {
console.log(err);
}
});

// POST new campuses
router.post("/", async (req, res) => {
try {
const post = req.body;
await campus.create(campus);
res.status(201);
} catch (err) {
console.log(err);
}
});
// PUT campus by ID
router.put("/:id", async (req, res) => {
try {
const updatedInfo = req.body;
const campusID = Number(req.params.id);
const campus = await Campus.findByPk(campusID);
if (campus === null) return res.sendStatus(404);

campus.name = updatedInfo.name;
campus.address = updatedInfo.address;
campus.imageUrl = updatedInfo.imageUrl;
campus.description = updatedInfo.description;
campus.save();
res.sendStatus(200);
} catch (err) {
console.log(err);
res.sendStatus(400);
}
});
// DELETE campus by ID
router.delete("/:id", async (req, res) => {
try {
const campausID = Number(req.params.id); //Acess the primary key from the user URL
const campus = await Campus.findByPk(campausID); //find a entry from a table using provied key
if (campus === null); //if it null no campus with that id exists
return res.sendStatus(404); //send a 404 not found response

await campus.destroy();
res.status(200).send(campusDetails);
} catch (error) {
console.log(err);
}
});

moudle.exports = router;
4 changes: 2 additions & 2 deletions api/ducks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require("express");
const router = express.Router();
const { Duck } = require("../database");
const { Student } = require("../database"); //for handling requests related to students

// GET all ducks
// students
router.get("/", async (req, res) => {
res.sendStatus(501);
});
Expand Down
3 changes: 3 additions & 0 deletions api/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const express = require("express");
const router = express.Router();
const { Campus, Student, student } = require("../database");
28 changes: 28 additions & 0 deletions database/campus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require("express");
const { Sequelize, DataTypes } = require("sequelize");
const db = require("./db");
const { toDefaultValue } = require("sequelize/lib/utils");

const Campus = db.define("campus", {
Name: {
type: DataTypes.STRING,
allowNull: false,
},

imageURL: {
type: DataTypes.BLOB,
defaultValue:
"https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.highereddive.com%2Fnews%2Fharvard-university-rejects-trump-demands%2F745331%2F&psig=AOvVaw2pnishPOzpbZ0_yp8Xhz43&ust=1751519725413000&source=images&cd=vfe&opi=89978449&ved=0CBQQjRxqFwoTCPC2v8y1nY4DFQAAAAAdAAAAABAM",
allowNull: false,
},

address: {
type: DataTypes.STRING,
allowNull: false,
},

description: {
type: DataTypes.TEXT,
},
});
module.exports = Campus;
11 changes: 0 additions & 11 deletions database/duck.js

This file was deleted.

9 changes: 7 additions & 2 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const db = require("./db");
const Duck = require("./duck");
const Student = require("./student");
const Campus = require("./campus");

Student.belongsTo(Campus);
Campus.hasMany(Student);

module.exports = {
db,
Duck,
Student,
Campus,
};
4 changes: 2 additions & 2 deletions database/seed.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const db = require("./db");
const { Duck } = require("./index");
const { student } = require("./index");

const seed = async () => {
db.logging = false;
await db.sync({ force: true }); // Drop and recreate tables
const ducks = await Duck.bulkCreate([
const students = await Duck.bulkCreate([
{ name: "James Pond" },
{ name: "Quakie Chan" },
{ name: "Goose" },
Expand Down
46 changes: 46 additions & 0 deletions database/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const express = require("express");
const { Sequelize, DataTypes } = require("sequelize");
const db = require("./db");

const Student = db.define("student", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true, // somthing
},

firstName: {
type: DataTypes.STRING,
allowNull: false,
},

lastName: {
type: DataTypes.STRING,
allowNull: false, //wont allow a coloum to be left blank
},

gpa: {
//gpa - decimal between 0.0 and 4.0
type: DataTypes.FLOAT,
allowNull: false, //the coloum has some sort of value
validate: {
max: 4.0,
min: 0.0,
},
},

image: {
defaultValue:
"https://www.google.com/url?sa=i&url=https%3A%2F%2Far.pinterest.com%2Fpin%2Fthe-random-person-i-made--19844054600819630%2F&psig=AOvVaw1nA8orGcyvOp7LVd0LXrV_&ust=1751490608495000&source=images&cd=vfe&opi=89978449&ved=0CBQQjRxqFwoTCLj8x4zJnI4DFQAAAAAdAAAAABAZ",
type: DataTypes.BLOB,
},

email: {
type: DataTypes.STRING,
unique: true,
validate: {
isEmail: true,
},
},
});
module.exports = Student;