Skip to content

Commit

Permalink
commit 1. Finished all folders required.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gayla Cloud authored and Gayla Cloud committed Oct 1, 2020
1 parent b217937 commit 99bc1df
Show file tree
Hide file tree
Showing 12 changed files with 934 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Connect Node to MySQL2.
var mysql2 = require("mysql2");

var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "2011Blue",
database: "burgers_db"
});

// Make connection.
connection.connect(function(err) {
if (err) {
console.error("error connecting: " + err.stack);
return;
}
console.log("connected as id " + connection.threadId);
});

// Export connection for our ORM to use.
module.exports = connection;
100 changes: 100 additions & 0 deletions config/orm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Require connection.js
var connection = require("../config/connection.js");

// Helper function for SQL syntax.
function printQuestionMarks(num) {
var arr = [];
for (var i = 0; i < num; i++) {
arr.push("?");
}
return arr.toString();
}

// Helper function to convert object key/value pairs to SQL syntax
function objToSql(ob) {
var arr = [];
// loop through the keys and push the key/value as a string int arr
for (var key in ob) {
var value = ob[key];
// check to skip hidden properties
if (Object.hasOwnProperty.call(ob, key)) {
// if string with spaces, add quotations (Lana Del Grey => 'Lana Del Grey')
if (typeof value === "string" && value.indexOf(" ") >= 0) {
value = "'" + value + "'";
}
// e.g. {name: 'Lana Del Grey'} => ["name='Lana Del Grey'"]
// e.g. {sleepy: true} => ["sleepy=true"]
arr.push(key + "=" + value);
}
}
// translate array of strings to a single comma-separated string
return arr.toString();
}

var orm = {
// Display all burgers in the db.
selectAll: function(table, cb) {
var queryString = "SELECT * FROM " + table + ";";

connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
cb(result);
});
},
// Add a burger to the db.
insertOne: function(table, cols, vals, cb) {
var queryString = "INSERT INTO " + table;
queryString += " (";
queryString += cols.toString();
queryString += ") ";
queryString += "VALUES (";
queryString += printQuestionMarks(vals.length);
queryString += ") ";

console.log(queryString);

connection.query(queryString, vals, function(err, result) {
if (err) {
throw err
}
cb(result);
});
},
// Set burger devoured status to true.
updateOne: function(table, objColVals, condition, cb) {
var queryString = "UPDATE " + table;
queryString += " SET ";
queryString += objToSql(objColVals);
queryString += " WHERE ";
queryString += condition;

console.log(queryString);

connection.query(queryString, function(err, result) {
if (err) {
throw err
}
cb(result);
});
},
// Delete a burger from the db.
deleteOne: function(table, condition, cb) {
var queryString = "DELETE FROM " + table;
queryString += " WHERE ";
queryString += condition;

console.log(queryString);

connection.query(queryString, function(err, result) {
if (err) {
throw err
}
cb(result);
});
}
};

// Export the ORM object in module.exports.
module.exports = orm;
55 changes: 55 additions & 0 deletions controllers/burgers_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Dependencies
var express = require("express");
// Import the model to use its db functions for burger.js
var burger = require("../models/burger.js");

// Create the router for the app, and export the router at the end of your file.
var router = express.Router();
// Create routes and set up logic where required.
router.get("/", function (req, res) {
burger.selectAll(function(data) {
var hbsObject = {
burgers: data
};
console.log(hbsObject);
res.render("index", hbsObject);
});
});
// Add new burger to the db.
router.post("/api/burgers", function (req, res) {
burger.insertOne(["burger_name", "devoured"], [req.body.burger_name, req.body.devoured], function(result) {
// Send back the ID of the new burger
res.json({ id: result.insertId });
});
});
// Set burger devoured status to true.
router.put("/api/burgers/:id", function(req, res) {
var condition = "id = " + req.params.id;

console.log("condition", condition);

burger.updateOne({ devoured: req.body.devoured }, condition, function(result) {
if (result.changedRows === 0) {
// If no rows were changed, then the ID must not exist, so 404.
return res.status(404).end();
} else {
res.status(200).end();
}
});
});
// Delete burger from db.
router.delete("/api/burgers/:id", function(req, res) {
var condition = "id = " + req.params.id;
console.log("condition", condition);

burger.deleteOne(condition, function(result) {
if (result.changedRows === 0) {
// If no rows were changed, then the ID must not exist, so 404.
return res.status(404).end();
} else {
res.status(200).end();
}
});
});

module.exports = router;
12 changes: 12 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Create the database burgers_db.
CREATE DATABASE burgers_db;
USE burgers_db;

-- Create the table burgers.
CREATE TABLE burgers
(
id INT NOT NULL AUTO_INCREMENT,
burger_name VARCHAR(50) NOT NULL,
devoured BOOLEAN,
PRIMARY KEY (id)
);
5 changes: 5 additions & 0 deletions db/seeds.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Select burger database --
USE burgers_db;
-- Insert rows --
INSERT INTO burgers (burger_name, devoured)
VALUES ("Good Burger", FALSE), ("Mondo Bueger", FALSE), ("Ham & Cheese Burger", FALSE);
32 changes: 32 additions & 0 deletions models/burger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Import (require) orm.js into burger.js
var orm = require("../config/orm.js");
// The code that will call the ORM functions using burger specific input for the ORM.
var burger = {
// Display all burgers in the db.
selectAll: function(cb) {
orm.selectAll("burgers", function(res) {
cb(res);
});
},
// Add a new burger to the db.
insertOne: function(cols, vals, cb) {
orm.insertOne("burgers", cols, vals, function(res) {
cb(res);
});
},
// Change the devoured status to true.
updateOne: function(objColVals, condition, cb) {
orm.updateOne("burgers", objColVals, condition, function(res) {
cb(res);
});
},
// Delete a burger from the db.
deleteOne: function(condition, cb) {
orm.deleteOne("burgers", condition, function(res) {
cb(res);
});
}
};

// Export at the end of the burger.js file.
module.exports = burger;
Loading

0 comments on commit 99bc1df

Please sign in to comment.