Skip to content

Commit

Permalink
changed update/delete/findby to username
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyotown committed Feb 4, 2020
1 parent 9cefd1b commit e5368d3
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 64 deletions.
15 changes: 13 additions & 2 deletions data/migrations/20200129201151_UserAndPatients.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
Expand All @@ -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");
};
20 changes: 20 additions & 0 deletions data/seeds/Cities.js
Original file line number Diff line number Diff line change
@@ -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" }
]);
});
};
6 changes: 3 additions & 3 deletions data/seeds/Drivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.seed = function(knex) {
Shift: "PM",
Price: "50",
Email: "[email protected]",
City: "Kampala"
City_ID: 1
},
{
FullName: "Martha Stewart",
Expand All @@ -27,7 +27,7 @@ exports.seed = function(knex) {
Shift: "AM",
Price: "35",
Email: "[email protected]",
City: "Kampala"
City_ID: 1
},
{
FullName: "Dan Abramov",
Expand All @@ -39,7 +39,7 @@ exports.seed = function(knex) {
Shift: "AM",
Price: "42",
Email: "[email protected]",
City: "Jinja"
City_ID: 2
}
]);
});
Expand Down
6 changes: 3 additions & 3 deletions data/seeds/Patients.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.seed = function(knex) {
DueDate: "6/12/2020",
Email: "[email protected]",
Address: "123 Kampala Street",
City: "Kampala"
City_ID: 1
},
{
FullName: "Rihanna",
Expand All @@ -25,7 +25,7 @@ exports.seed = function(knex) {
DueDate: "6/12/2020",
Email: "[email protected]",
Address: "345 Jinja Street",
City: "Jinja"
City_ID: 1
},
{
FullName: "Mary Michaels",
Expand All @@ -36,7 +36,7 @@ exports.seed = function(knex) {
DueDate: "6/12/2020",
Email: "[email protected]",
Address: "123 Gulu Ave",
City: "Gulu"
City_ID: 2
}
]);
});
Expand Down
2 changes: 1 addition & 1 deletion knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions middleware/updateCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
42 changes: 23 additions & 19 deletions routes/driverRoute/driverModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@ 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",
"Vehicle",
"Shift",
"Price",
"Email",
"City"
);
"c.City"
)
.join("Cities as c", "c.id", "Drivers.City_ID");
}
async function updateDriver(data) {
if (data.Password) {
data.Password = await bcrypt.hash(data.Password, 14);
}
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) {
Expand All @@ -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,
Expand Down
18 changes: 11 additions & 7 deletions routes/driverRoute/driverRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@ 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);
}
}
);

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);
}
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 21 additions & 18 deletions routes/patientRoute/patientModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down
Loading

0 comments on commit e5368d3

Please sign in to comment.