From e5368d3b1e0a0e087efc126d74a4c59347c6ef86 Mon Sep 17 00:00:00 2001 From: Dtown Date: Mon, 3 Feb 2020 19:51:04 -0600 Subject: [PATCH] changed update/delete/findby to username --- .../20200129201151_UserAndPatients.js | 15 ++++++- data/seeds/Cities.js | 20 +++++++++ data/seeds/Drivers.js | 6 +-- data/seeds/Patients.js | 6 +-- knexfile.js | 2 +- middleware/updateCheck.js | 8 ++-- routes/driverRoute/driverModel.js | 42 ++++++++++--------- routes/driverRoute/driverRoute.js | 18 ++++---- routes/patientRoute/patientModel.js | 39 +++++++++-------- routes/patientRoute/patientRoute.js | 16 +++---- 10 files changed, 108 insertions(+), 64 deletions(-) create mode 100644 data/seeds/Cities.js diff --git a/data/migrations/20200129201151_UserAndPatients.js b/data/migrations/20200129201151_UserAndPatients.js index 76e58bf..1e29eb4 100644 --- a/data/migrations/20200129201151_UserAndPatients.js +++ b/data/migrations/20200129201151_UserAndPatients.js @@ -1,4 +1,7 @@ exports.up = async knex => { + await knex.schema.createTable("Cities", table => { + table.increments("id"), table.string("City", 50).notNullable(); + }); await knex.schema.createTable("Patients", table => { table.increments(), table.string("FullName", 50).notNullable(); table @@ -10,7 +13,11 @@ exports.up = async knex => { table.date("DueDate"), table.string("Email", 128), table.string("Address", 240), - table.string("City", 240); + table + .integer("City_ID") + .notNullable() + .references("id") + .inTable("Cities"); }); await knex.schema.createTable("Drivers", table => { table.increments(), table.string("FullName", 50).notNullable(); @@ -24,11 +31,15 @@ exports.up = async knex => { table.string("Shift", 128), table.integer("Price"), table.string("Email", 128); - table.string("City", 50).notNullable(); + table + .integer("City_ID") + .notNullable() + .references("id"); }); }; exports.down = async knex => { await knex.schema.dropTableIfExists("Patients"); await knex.schema.dropTableIfExists("Drivers"); + await knex.schema.dropTableIfExists("Cities"); }; diff --git a/data/seeds/Cities.js b/data/seeds/Cities.js new file mode 100644 index 0000000..21cfe3a --- /dev/null +++ b/data/seeds/Cities.js @@ -0,0 +1,20 @@ +exports.seed = function(knex) { + // Deletes ALL existing entries + return knex("Cities") + .del() + .then(function() { + // Inserts seed entries + return knex("Cities").insert([ + { id: 1, City: "Kampala" }, + { id: 2, City: "Gulu" }, + { id: 3, City: "Lira" }, + { id: 4, City: "Mbarara" }, + { id: 5, City: "Jinja" }, + { id: 6, City: "Bwizibwera" }, + { id: 7, City: "Mbale" }, + { id: 8, City: "Mukono" }, + { id: 9, City: "Kasese" }, + { id: 10, City: "Masaka" } + ]); + }); +}; diff --git a/data/seeds/Drivers.js b/data/seeds/Drivers.js index 4f2b7ce..aff60d7 100644 --- a/data/seeds/Drivers.js +++ b/data/seeds/Drivers.js @@ -15,7 +15,7 @@ exports.seed = function(knex) { Shift: "PM", Price: "50", Email: "tomjerry@gmail.com", - City: "Kampala" + City_ID: 1 }, { FullName: "Martha Stewart", @@ -27,7 +27,7 @@ exports.seed = function(knex) { Shift: "AM", Price: "35", Email: "marthastewart@gmail.com", - City: "Kampala" + City_ID: 1 }, { FullName: "Dan Abramov", @@ -39,7 +39,7 @@ exports.seed = function(knex) { Shift: "AM", Price: "42", Email: "danabramov@gmail.com", - City: "Jinja" + City_ID: 2 } ]); }); diff --git a/data/seeds/Patients.js b/data/seeds/Patients.js index 878c936..10f383e 100644 --- a/data/seeds/Patients.js +++ b/data/seeds/Patients.js @@ -14,7 +14,7 @@ exports.seed = function(knex) { DueDate: "6/12/2020", Email: "TerryT@gmail.com", Address: "123 Kampala Street", - City: "Kampala" + City_ID: 1 }, { FullName: "Rihanna", @@ -25,7 +25,7 @@ exports.seed = function(knex) { DueDate: "6/12/2020", Email: "Rihanna@gmail.com", Address: "345 Jinja Street", - City: "Jinja" + City_ID: 1 }, { FullName: "Mary Michaels", @@ -36,7 +36,7 @@ exports.seed = function(knex) { DueDate: "6/12/2020", Email: "MaryM@gmail.com", Address: "123 Gulu Ave", - City: "Gulu" + City_ID: 2 } ]); }); diff --git a/knexfile.js b/knexfile.js index 96a59f9..3e60af3 100644 --- a/knexfile.js +++ b/knexfile.js @@ -2,7 +2,7 @@ module.exports = { development: { client: "pg", useNullAsDefault: true, - connection: `${process.env.DATABASE_URL}?ssl=true`, + connection: `postgres://plspasivdiviwu:2066dc6a782d5350232fa95d161a009abd5042b0727ce2a20b2720d3458cee28@ec2-54-92-174-171.compute-1.amazonaws.com:5432/d5ivfaomp2k08k?ssl=true`, migrations: { directory: "./data/migrations" }, diff --git a/middleware/updateCheck.js b/middleware/updateCheck.js index 1a2337e..2a8efcf 100644 --- a/middleware/updateCheck.js +++ b/middleware/updateCheck.js @@ -3,8 +3,8 @@ const driverModel = require("../routes/driverRoute/driverModel"); function checkForPatient(req, res, next) { return async (req, res, next) => { - const { id } = req.params; - const [findUser] = await patientModel.findBy({ id }); + const { UserName } = req.params; + const [findUser] = await patientModel.findBy({ UserName }); if (!findUser) { res.status(404).json({ Error: "Patient Cannot Be Found" @@ -18,8 +18,8 @@ function checkForPatient(req, res, next) { function checkForDriver(req, res, next) { return async (req, res, next) => { - const { id } = req.params; - const [findUser] = await driverModel.findBy({ id }); + const { UserName } = req.params; + const [findUser] = await driverModel.findBy({ UserName }); if (!findUser) { res.status(404).json({ Error: "Driver Cannot Be Found" diff --git a/routes/driverRoute/driverModel.js b/routes/driverRoute/driverModel.js index 522a0e6..1da0c93 100644 --- a/routes/driverRoute/driverModel.js +++ b/routes/driverRoute/driverModel.js @@ -2,24 +2,26 @@ const db = require("../../data/dbConfig"); const bcrypt = require("bcryptjs"); function find() { - return db("Drivers").select( - "id", - "FullName", - "UserName", - "PhoneNumber", - "Vehicle", - "Shift", - "Price", - "Email", - "City" - ); + return db("Drivers") + .select( + "Drivers.id", + "FullName", + "UserName", + "PhoneNumber", + "Vehicle", + "Shift", + "Price", + "Email", + "c.City" + ) + .join("Cities as c", "c.id", "Drivers.City_ID"); } function findBy(filter) { return db("Drivers") .where(filter) .select( - "id", + "Drivers.id", "FullName", "UserName", "PhoneNumber", @@ -27,8 +29,9 @@ function findBy(filter) { "Shift", "Price", "Email", - "City" - ); + "c.City" + ) + .join("Cities as c", "c.id", "Drivers.City_ID"); } async function updateDriver(data) { if (data.Password) { @@ -36,13 +39,14 @@ async function updateDriver(data) { } await db("Drivers") .update(data) - .where({ id: data.id }); - return findBy({ id: data.id }).first(); + .where({ UserName: data.UserName }); + return findBy({ UserName: data.UserName }).first(); } function loginFindBy(filter) { return db("Drivers") .where(filter) - .select("*"); + .select("*") + .join("Cities as c", "c.id", "Drivers.City_ID"); } async function registerDriver(Driver) { @@ -51,10 +55,10 @@ async function registerDriver(Driver) { return findBy({ UserName: Driver.UserName }).first(); } -function deleteDriver(id) { +function deleteDriver(UserName) { return db("Drivers") .del() - .where({ id }); + .where({ UserName }); } module.exports = { registerDriver, diff --git a/routes/driverRoute/driverRoute.js b/routes/driverRoute/driverRoute.js index ea34634..4a36ad3 100644 --- a/routes/driverRoute/driverRoute.js +++ b/routes/driverRoute/driverRoute.js @@ -14,14 +14,16 @@ router.get("/", authenticationMW, async (req, res, next) => { } }); router.get( - "/:id", + "/:UserName", authenticationMW, checkForDriver(), async (req, res, next) => { try { res .status(200) - .json(await driverModel.findBy({ id: req.params.id }).first()); + .json( + await driverModel.findBy({ UserName: req.params.UserName }).first() + ); } catch (err) { next(err); } @@ -29,14 +31,16 @@ router.get( ); router.put( - "/:id", + "/:UserName", authenticationMW, checkForDriver(), async (req, res, next) => { try { - const { id } = req.params; + const { UserName } = req.params; const changes = req.body; - res.status(200).json(await driverModel.updateDriver({ id, ...changes })); + res + .status(200) + .json(await driverModel.updateDriver({ UserName, ...changes })); } catch (err) { next(err); } @@ -89,12 +93,12 @@ router.post("/login", async (req, res, next) => { }); router.delete( - "/:id", + "/:UserName", authenticationMW, checkForDriver(), async (req, res, next) => { try { - await driverModel.deleteDriver(req.params.id); + await driverModel.deleteDriver(req.params.UserName); res.status(201).json({ Success: "Driver Deleted Successfully!" }); } catch (err) { next(err); diff --git a/routes/patientRoute/patientModel.js b/routes/patientRoute/patientModel.js index 2add8af..a82cc61 100644 --- a/routes/patientRoute/patientModel.js +++ b/routes/patientRoute/patientModel.js @@ -2,30 +2,33 @@ const db = require("../../data/dbConfig"); const bcrypt = require("bcryptjs"); function find() { - return db("Patients").select( - "id", - "FullName", - "UserName", - "PhoneNumber", - "DueDate", - "Email", - "Address", - "City" - ); + return db("Patients") + .select( + "Patients.id", + "FullName", + "UserName", + "PhoneNumber", + "DueDate", + "Email", + "Address", + "c.City" + ) + .join("Cities as c", "c.id", "Patients.City_ID"); } function findBy(filter) { return db("Patients") .where(filter) .select( - "id", + "Patients.id", "FullName", "UserName", "PhoneNumber", "DueDate", "Email", "Address", - "City" - ); + "c.City" + ) + .join("Cities as c", "c.id", "Patients.City_ID"); } function loginFindBy(filter) { return db("Patients") @@ -38,19 +41,19 @@ async function updatePatient(data) { } await db("Patients") .update(data) - .where({ id: data.id }); - return findBy({ id: data.id }).first(); + .where({ UserName: data.UserName }); + return findBy({ UserName: data.UserName }).first(); } async function registerPatient(Patient) { Patient.Password = await bcrypt.hash(Patient.Password, 14); await db("Patients").insert(Patient); - return findBy({ Patient: Patient.UserName }).first(); + return findBy({ UserName: Patient.UserName }).first(); } -function deletePatient(id) { +function deletePatient(UserName) { return db("Patients") .del() - .where({ id }); + .where({ UserName }); } module.exports = { registerPatient, diff --git a/routes/patientRoute/patientRoute.js b/routes/patientRoute/patientRoute.js index 1881ae6..c27d690 100644 --- a/routes/patientRoute/patientRoute.js +++ b/routes/patientRoute/patientRoute.js @@ -15,14 +15,16 @@ router.get("/", authenticationMW, async (req, res, next) => { }); router.get( - "/:id", + "/:UserName", authenticationMW, checkForPatient(), async (req, res, next) => { try { res .status(200) - .json(await patientModel.findBy({ id: req.params.id }).first()); + .json( + await patientModel.findBy({ UserName: req.params.UserName }).first() + ); } catch (err) { next(err); } @@ -30,16 +32,16 @@ router.get( ); router.put( - "/:id", + "/:UserName", authenticationMW, checkForPatient(), async (req, res, next) => { try { - const { id } = req.params; + const { UserName } = req.params; const changes = req.body; res .status(200) - .json(await patientModel.updatePatient({ id, ...changes })); + .json(await patientModel.updatePatient({ UserName, ...changes })); } catch (err) { next(err); } @@ -92,12 +94,12 @@ router.post("/login", async (req, res, next) => { }); router.delete( - "/:id", + "/:UserName", authenticationMW, checkForPatient(), async (req, res, next) => { try { - await patientModel.deletePatient(req.params.id); + await patientModel.deletePatient(req.params.UserName); res.status(201).json({ Success: "Patient Deleted Successfully!" }); } catch (err) { next(err);