Skip to content

Commit

Permalink
Database project
Browse files Browse the repository at this point in the history
  • Loading branch information
absurdtheory committed Dec 18, 2018
1 parent f45b607 commit ff9aa56
Show file tree
Hide file tree
Showing 68 changed files with 2,678 additions and 0 deletions.
138 changes: 138 additions & 0 deletions Database Project/DMQDogDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
-- dog queries

-- get all dog columns and name of adopter and food to populate Manage Dogs page and Browse Dogs page

SELECT d.id, name, birthdate, breed, sex, adoptedBy, foodType, image, a.fname, a.lname, f.bname, f.pname FROM Dogs d INNER JOIN Adopters a ON d.adoptedBy = a.id INNER JOIN FoodSupply f ON d.foodType = f.id ORDER BY d.id;

-- get dog id and name to populate dropdown on Works page

SELECT id, name FROM Dogs;

-- get all dog columns for Browse dog page base on search critera

SELECT id, name, birthdate, breed, sex, adoptedBy, foodType, image FROM Dogs WHERE name = :nameInput, birthdate = :birthdateInput, breed = :breedInput, sex = :sexInput, adoptedBy = :adoptedByInputFromDropDown, foodType = :foodTypeInputFromDropDown;

-- add a new dog

INSERT INTO Dogs (name, birthdate, breed, sex, foodType, image) VALUES (:nameInput, :birthdateInput, :breedInput, :sexInput, :foodTypeInputFromDropDown, :imageInput);

-- delete a dog

DELETE FROM Dogs WHERE id = :idSelectedFromManageDogsPage;

-- autopopulate edit dog Page

SELECT name, birthdate, breed, sex, adoptedby, foodtype, image FROM Dogs WHERE id= :idFromEditButton;

-- edit a dog

UPDATE Dogs SET name = :nameInput, birthdate = :birthdateInput, breed = :breedInput, sex = :sexInput, adoptedBy = :adoptedByIDFromDropDown, foodType = :foodTypeIDFromDropDown, image = :imageInput WHERE id = :idFromDogUpdateButton;

-- adopter queries

-- get all adopter columns to populate Adopters page, use count to dynamically populate number of dogs adopted for one-to-many relationship

SELECT Adopters.id, fname, lname, saddress, city, state, zip, phone, COUNT(d.id) as nad FROM Adopters LEFT JOIN Dogs d ON Adopters.id = d.adoptedBy GROUP BY Adopters.id;

-- get adopter id, fname, and lname columns in Adopters to populate a dropdown for searching by adopter on Browse Dogs page and edit dog page

SELECT id, fname, lname FROM Adopters;

-- add a new adopter

INSERT INTO Adopters (fname, lname, saddress, city, state, zip, phone) VALUES (:fnameInput, :lnameInput, :saddressInput, :cityInput, :stateInput, :zipInput, :phoneInput);

-- delete an adopter

DELETE FROM Adopters WHERE id = :idSelectedfromAdoptersPage;

-- autopopulate adopter edit page

SELECT fname, lname, saddress, city, state, zip, phone FROM Adopters WHERE id = :idFromEditButton;

-- edit an adopter

UPDATE Adopters SET fname = :fnameInput, lname = :lnameInput, saddress = :saddressInput, city = :cityInput, state = :stateInput, zip = :zipInput, phone = :phoneInput WHERE id = idFromAdopterEditButton;

-- Employee queries

-- get all Employee columns to populate Employee page

SELECT id, fname, lname, fsa, phone, title, IF(fsa = 1, 'Yes', 'No') as auth FROM Employees;


-- get employeeID, fname, lname columns of only employes able to order food to populate dropdown on Food Orders page

SELECT id, fname, lname FROM Employees WHERE fsa = TRUE;

-- get employeeID, fname, lname clumns of employess to populate dropdown on Works page

SELECT id, fname, lname FROM Employees;

-- add a new Employee

INSERT INTO Employees (fname, lname, fsa, phone, title) VALUES (:fnameInput, :lnameInput, :fsaInput, :phoneInput, :titleInput);

-- delete an employee

DELETE FROM Employees WHERE id = :idSelectedFromEmployeePage;

-- autopopulate edit employee page;

SELECT id, fname, lname, fsa, phone, title, IF(fsa = 1, 'Yes', 'No') as auth FROM Employees WHERE id=:idFromEditButton;

-- Edit an employee

UPDATE Employees SET fname = :fnameInput, lname = :lnameInput, fsa = :fsaInput, phone = :phoneInput, title = :titleInput WHERE id = :idFromEmployeeEditButton;

-- Food Supply Queries

-- get all columns from FoodSupply to populate Food Supply Page, use count to dynamically calculated the number of dogs on a food type

SELECT f.id, bname, pname, COUNT(d.id) AS numberd, amount FROM FoodSupply f LEFT JOIN Dogs d ON f.id = d.foodType GROUP BY f.id

-- get id, bname, and lname from FoodSupply to populate dropdowns on Order Food Page and Manage Dog page

SELECT id, bname, pname FROM FoodSupply;

-- add a new Food Supply

INSERT INTO FoodSupply (bname, pname) VALUES (:bnameInput, :pnameInput);

-- delete a food supply

DELETE FROM FoodSupply WHERE id = :idSelectedFromFoodSupplyPage;

-- autopopulate food supply edit page

SELECT bname, pname, quantity FROM FoodSupply WHERE id = :idFromEditButton;

-- edit food supply

UPDATE FoodSupply SET bname = :bnameInput, pname = :pnameInput, quantity = :amountInput WHERE id = :idFromFoodSupplyEditButton;

-- food order queries

-- add a new Food order

INSERT INTO Orders (FoodSupplyID, EmployeeID, quantity) VALUES (:foodInputFromDropDown, :idInput, :quantityInput);

-- delete from food order (M-to-N relationship)

DELETE FROM Orders WHERE EmployeeID = :pidSelectedFromOrdersPage and FoodSupplyID = :fidSelectedFromOrdersPage;

-- get employee id, fname, lname, and foodsupply bname, pname, and orders quantity and date ordered to display on Food Orders page
SELECT Employees.id as eid, fname, lname, FoodSupply.id as fid, bname, pname, Orders.quantity, DATE_FORMAT(dateOrdered, '%Y-%m-%d %H:%i:%s') AS newDate FROM Employees INNER JOIN Orders ON Employees.id = Orders.EmployeeID INNER JOIN FoodSupply ON FoodSupply.id = Orders.FoodSupplyID ORDER BY Orders.dateOrdered;

-- Work Queries

-- add a new work relationship

INSERT IGNORE INTO Works (EmployeeID, DogID) VALUES (:pidInput, :didInput);

-- delete from works (M-to-N relationship)

DELETE FROM Works WHERE EmployeeID = :pidSelectedFromWorksPage and DogID = :didSelectedFromWorksPage;

-- get employee id, fname, lname and dog id, name to display on Works Page
SELECT Employees.id, fname, lname, title, Dogs.id, name FROM Employees INNER JOIN Works ON Employees.id = Works.EmployeeID INNER JOIN Dogs ON Dogs.id = Works.DogID;
118 changes: 118 additions & 0 deletions Database Project/DogDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
DROP TABLE IF EXISTS `Orders`;
DROP TABLE IF EXISTS `Works`;
DROP TABLE IF EXISTS `Dogs`;
DROP TABLE IF EXISTS `Adopters`;
DROP TABLE IF EXISTS `FoodSupply`;
DROP TABLE IF EXISTS `Employees`;

CREATE TABLE `Adopters` (
`id` int NOT NULL AUTO_INCREMENT,
`fname` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`saddress` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(255) NOT NULL,
`zip` varchar(255) NOT NULL,
`phone` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;


INSERT INTO `Adopters` (`id`, `fname`, `lname`, `saddress`, `city`, `state`, `zip`, `phone`) VALUES
(1, 'Alexis', 'Tucker', '12345 Abc Street', 'Fairfax', 'Virginia', '12345', '(111)222-3333'),
(2, 'Gabrielle', 'Mamani', '1600 Pennsylvania Ave.', 'Washington DC', 'Washington DC', '12344', '(444)555-6666'),
(3, 'Nicolas', 'Scherrs', '1826 MidNowhere Lane', 'Eau Claire', 'Wisconsin', '56431', '(566)123-4567');


CREATE TABLE `Employees` (
`id` int NOT NULL AUTO_INCREMENT,
`fname` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`fsa` boolean NOT NULL DEFAULT FALSE,
`phone` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;


INSERT INTO `Employees` (`id`, `fname`, `lname`, `fsa`, `phone`, `title`) VALUES
(1, 'Nick', 'Haase', 1, '(222) 333-4444', 'CEO'),
(2, 'Chelsea', 'Norod', 0, '(777)888-9999', 'Vet'),
(3, 'Robin', 'DeRosa', 1, '(123)456-7890', 'Event Coordinator'),
(12, 'Rebecca', 'Cole', 1, '(345)134-2345', 'Manager');



CREATE TABLE `FoodSupply` (
`id` int NOT NULL AUTO_INCREMENT,
`bname` varchar(255) NOT NULL,
`pname` varchar(255) NOT NULL,
`amount` int NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;


INSERT INTO `FoodSupply` (`id`, `bname`, `pname`, `amount`) VALUES
(1, 'Natural Balance', 'Sweet Potato & Fish', 5),
(2, 'Performatin Ultra', 'Chicken and Sweet Potato', 5),
(3, 'Performatin Ultra', 'Limited Ingredient Venison & Sweet Potato', 15);



CREATE TABLE `Dogs` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`birthdate` date NOT NULL,
`breed` varchar(255) NOT NULL,
`sex` varchar(255) NOT NULL,
`adoptedBy` int,
`foodType` int NOT NULL,
`image` varchar(255),
PRIMARY KEY (`id`),
FOREIGN KEY (`adoptedBy`) REFERENCES Adopters(`id`) ON DELETE SET NULL,
FOREIGN KEY (`foodType`) REFERENCES FoodSupply(`id`)
) ENGINE=InnoDB;


INSERT INTO `Dogs` (`id`, `name`, `birthdate`, `breed`, `sex`, `adoptedBy`, `foodType`, `image`) VALUES
(1, 'Natalia', '2014-04-04', 'Papillion/ Pekingese Mix', 'Female', 2, 1, '/static/images/natasha.jpg'),
(2, 'Kingston', '2014-12-14', 'Cockapoo', 'Male', NULL, 2, '/static/images/kingston.jpg'),
(3, 'Loki', '2012-06-06', 'Norwegian Elkhound', 'Male', 2, 2, NULL),
(5, 'Milla', '2012-12-05', 'Husky Mix', 'Female', 3, 2, '/static/images/milla.jpg'),
(6, 'Gypsy', '2014-07-13', 'Blue Heeler Mix', 'Female', 3, 1, ''),
(10, 'Oliver', '2018-02-21', 'Papillion', 'Male', NULL, 1, '/static/images/papillon.jpg'),
(11, 'Fluffy', '2016-10-12', 'Poodle', 'Female', NULL, 2, 'https://images.unsplash.com/photo-1505044197374-4d4ae3f9d566?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=af3dab3922f397a5fde45b3145a0cbdb&auto=format&fit=crop&w=1868&q=80'),
(12, 'Rex', '2016-12-25', 'Golden Retriever', 'Female', NULL, 3, 'https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12232719/Golden-Retriever-On-White-05.jpg'),
(14, 'Spudz ', '2016-12-25', 'chocolate lab', 'Male', NULL, 1, '');


CREATE TABLE `Orders` (
`FoodSupplyID` int NOT NULL,
`EmployeeID` int NOT NULL,
`DateOrdered` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`quantity` int NOT NULL,
PRIMARY KEY(`FoodSupplyID`, `EmployeeID`, `DateOrdered`),
FOREIGN KEY(`FoodSupplyID`) REFERENCES `FoodSupply`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(`EmployeeID`) REFERENCES `Employees`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB;


INSERT INTO `Orders` VALUES (1, 1, DEFAULT, 5), (1, 2, DEFAULT, 10);


CREATE TABLE `Works` (
`EmployeeID` int NOT NULL,
`DogID` int NOT NULL,
PRIMARY KEY(`EmployeeID`, `DogID`),
FOREIGN KEY(`EmployeeID`) REFERENCES `Employees`(`id`) ON DELETE CASCADE,
FOREIGN KEY(`DogID`) REFERENCES `Dogs`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;

INSERT INTO `Works` (`EmployeeID`, `DogID`) VALUES
(1, 1),
(1, 2),
(1, 11),
(2, 3),
(12, 6);


126 changes: 126 additions & 0 deletions Database Project/adopters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// JavaScript source code
/***************************************************************************************
* --SOURCE--
* Title: people.js
* Authors: Justin Wolford,Samarendra Hedao
* Initial Publish Date: 10/25/2017
* Availability: https://github.com/knightsamar/CS340-Sample-Web-App
***************************************************************************************/
module.exports = function () {
var express = require('express');
var router = express.Router();

//function to select adopters to populate the adopter page. Selects all information from adopters, and LEFT JOINS on dogs to calculate the number of dogs
//each adopter has adopted using COUNT()
function getAdopters(res, mysql, context, complete) {
mysql.pool.query("SELECT Adopters.id, fname, lname, saddress, city, state, zip, phone, COUNT(d.id) as nad FROM Adopters LEFT JOIN Dogs d ON Adopters.id = d.adoptedBy GROUP BY Adopters.id",
function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.adopters = results; //define results as "adopters"
complete(); //call the complete function to increase callbackcount
});
}

//function to select one adopter prepopulate the edit page
function getAdopter(res, mysql, context, id, complete) {
var sql = "SELECT Adopters.id, fname, lname, saddress, city, state, zip, phone FROM Adopters WHERE Adopters.id = ?";
var inserts = [id];
mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.adopter = results[0]; //define result as "adopter"
complete(); //call the complete function to increase callbackcount
});
}

//GET request to render the /adopters page when it loads, calls appropriate functions to populate page
router.get('/', function (req, res) {
var callbackCount = 0;
var context = {};
var mysql = req.app.get('mysql');
getAdopters(res, mysql, context, complete);
function complete() {
callbackCount++;
if (callbackCount >= 1) {
res.render('adopters', context);
}

}
});

//POST request to insert data into the adopters table
router.post('/', function (req, res) {
if (req.body.fname != "" & req.body.lname != "" & req.body.saddress != "" & req.body.city != "" & req.body.state != "" & req.body.zip != "" & req.body.phone != "") {
console.log(req.body)
var mysql = req.app.get('mysql');
var sql = "INSERT INTO Adopters (fname, lname, saddress, city, state, zip, phone) VALUES(?,?,?,?,?,?,?)";
var inserts = [req.body.fname, req.body.lname, req.body.saddress, req.body.city, req.body.state, req.body.zip, req.body.phone];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(JSON.stringify(error))
res.write(JSON.stringify(error));
res.end();
} else {
res.redirect('/adopters');
}
});
}
});

//DELETE request to delete data from the Adopter table, receives the id of the adopter to be deleted from from addAdopter.js
router.delete('/:id', function (req, res) {
var mysql = req.app.get('mysql');
var sql = "DELETE FROM Adopters WHERE id = ?";
var inserts = [req.params.id];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.status(400);
res.end();
} else {
res.status(202).end();
}
})
})

//GET request gets the id of the adopter being edited, calls the appropriate function, then renders the edit page with prepopulated data
router.get('/:id', function (req, res) {
callbackCount = 0;
var context = {};
var mysql = req.app.get('mysql');
getAdopter(res, mysql, context, req.params.id, complete);
function complete() {
callbackCount++;
if (callbackCount >= 1) {
res.render('adopteredit', context);
}

}
});

//PUT request updates an adopter with the information from the edit page and returns to the main adopter page
router.put('/:id', function (req, res) {
var mysql = req.app.get('mysql');
console.log(req.body)
console.log(req.params.id)
var sql = "UPDATE Adopters SET fname = ?, lname = ?, saddress = ?, city = ?, state = ?, zip = ?, phone = ? WHERE id = ?;"
var inserts = [req.body.fname, req.body.lname, req.body.saddress, req.body.city, req.body.state, req.body.zip, req.body.phone, req.params.id];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(error)
res.write(JSON.stringify(error));
res.end();
} else {
res.status(200);
res.end();
}
});
});

return router;
}();
Loading

0 comments on commit ff9aa56

Please sign in to comment.