diff --git a/api/campus.js b/api/campus.js new file mode 100644 index 0000000..837594e --- /dev/null +++ b/api/campus.js @@ -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; diff --git a/api/ducks.js b/api/ducks.js index 1dc7c79..bdc86e9 100644 --- a/api/ducks.js +++ b/api/ducks.js @@ -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); }); diff --git a/api/student.js b/api/student.js new file mode 100644 index 0000000..3585765 --- /dev/null +++ b/api/student.js @@ -0,0 +1,3 @@ +const express = require("express"); +const router = express.Router(); +const { Campus, Student, student } = require("../database"); diff --git a/database/campus.js b/database/campus.js new file mode 100644 index 0000000..180bba5 --- /dev/null +++ b/database/campus.js @@ -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; diff --git a/database/duck.js b/database/duck.js deleted file mode 100644 index 93df65e..0000000 --- a/database/duck.js +++ /dev/null @@ -1,11 +0,0 @@ -const { DataTypes } = require("sequelize"); -const db = require("./db"); - -// You porbably don't need a Duck model, this is just for demonstration purposes -const Duck = db.define("duck", { - name: { - type: DataTypes.STRING, - }, -}); - -module.exports = Duck; diff --git a/database/index.js b/database/index.js index 0e8c8bd..b9e2155 100644 --- a/database/index.js +++ b/database/index.js @@ -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, }; diff --git a/database/seed.js b/database/seed.js index 46c7a42..92038da 100644 --- a/database/seed.js +++ b/database/seed.js @@ -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" }, diff --git a/database/student.js b/database/student.js new file mode 100644 index 0000000..e718cf1 --- /dev/null +++ b/database/student.js @@ -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;