From 114f51fafbcb8bca9c04c9120ecb028bc5fd33fb Mon Sep 17 00:00:00 2001 From: Muhammad Date: Tue, 1 Jul 2025 17:05:00 -0400 Subject: [PATCH 01/11] somewhat done with student table --- api/ducks.js | 4 ++-- database/duck.js | 1 + database/seed.js | 4 ++-- database/student.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 database/student.js 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/database/duck.js b/database/duck.js index 93df65e..f3515dd 100644 --- a/database/duck.js +++ b/database/duck.js @@ -2,6 +2,7 @@ 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, 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..6371afc --- /dev/null +++ b/database/student.js @@ -0,0 +1,42 @@ +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, + }, + + 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: { + type: DataTypes.BLOB, + allowNull: false, //wont allow a coloum to be left blank + }, + + email: { + type: DataTypes.FLOAT, + validate: { + isEmail: true, + }, + }, +}); From 548d0f17c94382030bede0928e004cb5cb179ad1 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Wed, 2 Jul 2025 01:25:57 -0400 Subject: [PATCH 02/11] almost done --- database/campuses.js | 27 +++++++++++++++++++++++++++ database/student.js | 5 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 database/campuses.js diff --git a/database/campuses.js b/database/campuses.js new file mode 100644 index 0000000..52e1e73 --- /dev/null +++ b/database/campuses.js @@ -0,0 +1,27 @@ +const express = require("express"); +const { Sequelize, DataTypes } = require("sequelize"); +const db = require("./db"); +const { toDefaultValue } = require("sequelize/lib/utils"); + + +const campuses = db.define("Campuses" { +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, +}, + + +adress:{ +type:DataTypes.INTEGER.STRING, +allowNull:false, + +} + + +}) \ No newline at end of file diff --git a/database/student.js b/database/student.js index 6371afc..5b926fd 100644 --- a/database/student.js +++ b/database/student.js @@ -13,6 +13,7 @@ const Student = db.define("student", { type: DataTypes.STRING, allowNull: false, }, + lastName: { type: DataTypes.STRING, allowNull: false, @@ -28,7 +29,9 @@ const Student = db.define("student", { }, }, - Image: { + 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, allowNull: false, //wont allow a coloum to be left blank }, From 91f76c7d01d47f2afefbaf648ba50d08980e78dc Mon Sep 17 00:00:00 2001 From: Muhammad Date: Wed, 2 Jul 2025 10:36:23 -0400 Subject: [PATCH 03/11] done --- database/campus.js | 28 ++++++++++++++++++++++++++++ database/campuses.js | 27 --------------------------- database/duck.js | 12 ------------ database/index.js | 9 +++++++-- database/student.js | 6 +++--- 5 files changed, 38 insertions(+), 44 deletions(-) create mode 100644 database/campus.js delete mode 100644 database/campuses.js delete mode 100644 database/duck.js 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/campuses.js b/database/campuses.js deleted file mode 100644 index 52e1e73..0000000 --- a/database/campuses.js +++ /dev/null @@ -1,27 +0,0 @@ -const express = require("express"); -const { Sequelize, DataTypes } = require("sequelize"); -const db = require("./db"); -const { toDefaultValue } = require("sequelize/lib/utils"); - - -const campuses = db.define("Campuses" { -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, -}, - - -adress:{ -type:DataTypes.INTEGER.STRING, -allowNull:false, - -} - - -}) \ No newline at end of file diff --git a/database/duck.js b/database/duck.js deleted file mode 100644 index f3515dd..0000000 --- a/database/duck.js +++ /dev/null @@ -1,12 +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/student.js b/database/student.js index 5b926fd..97ba323 100644 --- a/database/student.js +++ b/database/student.js @@ -16,7 +16,7 @@ const Student = db.define("student", { lastName: { type: DataTypes.STRING, - allowNull: false, + allowNull: false, //wont allow a coloum to be left blank }, gpa: { @@ -33,13 +33,13 @@ const Student = db.define("student", { 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, - allowNull: false, //wont allow a coloum to be left blank }, email: { - type: DataTypes.FLOAT, + type: DataTypes.STRING, validate: { isEmail: true, }, }, }); +module.exports = Student; From c550965eb6edf031116517368e54211528fabbb1 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 15:12:59 -0400 Subject: [PATCH 04/11] almost done with this function --- api/campus.js | 36 ++++++++++++++++++++++++++++++++++++ api/student.js | 0 2 files changed, 36 insertions(+) create mode 100644 api/campus.js create mode 100644 api/student.js diff --git a/api/campus.js b/api/campus.js new file mode 100644 index 0000000..0b094d1 --- /dev/null +++ b/api/campus.js @@ -0,0 +1,36 @@ +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) => { + const userId = req.params.id; //Acess the primary key from the user URL + try { + const ID = await Campus.findByPk(id); //find a entry from a table using provied key + if() + } catch (error) { + console.log("error has accrued"); + } +}); + +// POST new campuses + +// PUT campus by ID + +// DELETE campus by ID diff --git a/api/student.js b/api/student.js new file mode 100644 index 0000000..e69de29 From 6b9be733a84778d81a65b3dc81687772ea2434a0 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 15:27:43 -0400 Subject: [PATCH 05/11] done with get by id --- api/campus.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/api/campus.js b/api/campus.js index 0b094d1..f280593 100644 --- a/api/campus.js +++ b/api/campus.js @@ -20,12 +20,21 @@ router.get("/", async (req, res) => { // GET campus by ID router.get("/id", async (req, res) => { - const userId = req.params.id; //Acess the primary key from the user URL + //Acess the primary key from the user URL try { - const ID = await Campus.findByPk(id); //find a entry from a table using provied key - if() - } catch (error) { - console.log("error has accrued"); + 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).(campusDetails); + + }catch (error) { + console.log(err); } }); From 9851f77f747028b0cf7513e468f3312b4ea69966 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 15:40:19 -0400 Subject: [PATCH 06/11] almost done --- api/campus.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api/campus.js b/api/campus.js index f280593..395709b 100644 --- a/api/campus.js +++ b/api/campus.js @@ -39,7 +39,13 @@ router.get("/id", async (req, res) => { }); // POST new campuses +router.post("/",async (req,res)=> { +try{ + const post = req.body; + await campus.create(campus); +} +}) // PUT campus by ID // DELETE campus by ID From 5cebc0613ce2f46aba7105cfc31af99ca0731576 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 15:43:03 -0400 Subject: [PATCH 07/11] fix some mistakes and finished the post campus function --- api/campus.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/api/campus.js b/api/campus.js index 395709b..556ba74 100644 --- a/api/campus.js +++ b/api/campus.js @@ -26,26 +26,27 @@ router.get("/id", async (req, res) => { 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).(campusDetails); - - }catch (error) { + 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); - -} -}) +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 // DELETE campus by ID From 93f66a92f91113167a23c12f85241c0a2ec5c345 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 15:56:39 -0400 Subject: [PATCH 08/11] done with delte function --- api/campus.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api/campus.js b/api/campus.js index 556ba74..d9f3937 100644 --- a/api/campus.js +++ b/api/campus.js @@ -50,3 +50,16 @@ router.post("/", async (req, res) => { // PUT campus by ID // 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); + } +}); From 93ff0e5affe83e100844bb54e716532527c146cb Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 16:18:15 -0400 Subject: [PATCH 09/11] done with put campus by ID --- api/campus.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api/campus.js b/api/campus.js index d9f3937..2508145 100644 --- a/api/campus.js +++ b/api/campus.js @@ -48,7 +48,24 @@ router.post("/", async (req, res) => { } }); // 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 { From cb6b01bbcbff2ffe9c78c005b0fb768b7aece646 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 16:19:46 -0400 Subject: [PATCH 10/11] forgot about the moudle. --- api/campus.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/campus.js b/api/campus.js index 2508145..837594e 100644 --- a/api/campus.js +++ b/api/campus.js @@ -80,3 +80,5 @@ router.delete("/:id", async (req, res) => { console.log(err); } }); + +moudle.exports = router; From e2b27c27100844f443e21230214b8b61b6b7c82a Mon Sep 17 00:00:00 2001 From: Muhammad Date: Sat, 5 Jul 2025 19:14:06 -0400 Subject: [PATCH 11/11] made sure they could be no duplicate files --- api/student.js | 3 +++ database/student.js | 1 + 2 files changed, 4 insertions(+) diff --git a/api/student.js b/api/student.js index e69de29..3585765 100644 --- a/api/student.js +++ 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/student.js b/database/student.js index 97ba323..e718cf1 100644 --- a/database/student.js +++ b/database/student.js @@ -37,6 +37,7 @@ const Student = db.define("student", { email: { type: DataTypes.STRING, + unique: true, validate: { isEmail: true, },