Skip to content

Commit

Permalink
still working on the routes
Browse files Browse the repository at this point in the history
  • Loading branch information
smendez92 committed Aug 26, 2017
1 parent f7ce64c commit 95afaca
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 72 deletions.
2 changes: 1 addition & 1 deletion config/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (process.env.JAWSDB_URL) {
else {
connection = mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
//port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "heroku_6aafb0838fd5e56"
Expand Down
22 changes: 10 additions & 12 deletions config/orm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var orm = {

// function takes in a table name, a column name, and a value to query the database
// and return the results as a callback function
selectAll: function(tableName, cb) {
var queryString = "SELECT * FROM " + tableName + ";";
connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
cb(result);
});
},

selectAllByKeyValue: function(tableName, column, value, cb) {
var queryString = "SELECT * FROM " + tableName + " WHERE " + column + " = '" + value + "';";
connection.query(queryString, function(err, result) {
Expand All @@ -40,18 +50,6 @@ var orm = {
});
},

// This function takes in a table name, a column name, a value, a condition, and a callback function
// to make an SQL query that updates one row of a table
updateOne: function(tableName, column, value, condition, cb) {
var queryString = "UPDATE " + tableName + " SET " + column + " = " + value + " WHERE " + condition + ";";
connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
cb(result);
});
}

};

// Export the orm object for use by our models (burger.js)
Expand Down
15 changes: 8 additions & 7 deletions controllers/players_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
// =============================================================
// Dependencies
// =============================================================
var express = require("express");

//Import the model to use its database functions
var player = require("../models/player.js");

//create the router for our app
var express = require("express");
var router = express.Router();

// =============================================================
Expand All @@ -24,13 +22,16 @@ var router = express.Router();

// Create all our routes and set up logic within those routes where required.

//player.selectAll(function(data){
// console.log(data)});

router.get("/cards", function(request, response) {
player.refreshHand(request.id, function(data) {
// Export routes for use by our server (server.js)

router.get("/api/cards", function(req, res) {
player.selectAll(function(data) {
console.log(data);
res.json(data);
return res.json(data);
});
});

// Export routes for use by our server (server.js)
module.exports = router;
7 changes: 7 additions & 0 deletions db/cards_to_deal_seeds.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

USE `heroku_6aafb0838fd5e56`;
insert into cards_to_deal (id, title, description, room_id, player_id) values(1, "TitL3 1", "D3sCr1pT10n 1 !!", 1, 1);
insert into cards_to_deal (id, title, description, room_id, player_id) values(11, "TitL3 2", "D3sCr1pT10n 2 !!", 1, 11);
insert into cards_to_deal (id, title, description, room_id, player_id) values(21, "TitL3 3", "D3sCr1pT10n 3 !!", 1, 21);
insert into cards_to_deal (id, title, description, room_id, player_id) values(31, "TitL3 4", "D3sCr1pT10n 4 !!", 1, 31);
insert into cards_to_deal (id, title, description, room_id, player_id) values(41, "TitL3 5", "D3sCr1pT10n 5 !!", 1, 41);
31 changes: 31 additions & 0 deletions dist/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/assets/css/style.css" type="text/css" />
<title>MVC With Cats!</title>
</head>
<body>
<button id="draw-button">DRAW NEW CARD</button>
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript">
// Event listener for our cat-button
$("#draw-button").on("click", function() {

var myId = 1

// Perfoming an AJAX GET request to our queryURL
// run an AJAX GET-request for our servers api,
// including the user's character in the url
$.get("/api/cards/")
.done(function(data) {
// log the data we found
console.log(data);
});

});

</script>
</body>

</html>
61 changes: 12 additions & 49 deletions models/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,38 @@
// Import ORM query functions
var orm = require("../config/orm.js");


// =============================================================
// player.js functions
// =============================================================

// Use ORM functions and corresponding inputs to make mySQL queries
var player = {

//This function takes in a values array to insert a new row into the Players table.
//NOTE: the username in the values array must be a string with inner single quotes
// Ex: values = ["'PlayerZ'", 1]
addNewPlayer: function(values, cb){
orm.insertOne("players", ["username", "room_id"], values, function(res){
addNewCard: function(values, cb){
orm.insertOne("cards_test", ["title", "description", "role", ], values, function(res){
cb(res);
});
},

//=========================================
// HOST FUNCTION
//=========================================
//This function takes in a values array to insert a new row into the games table.
//NOTE: the room name in the values array must be a string with inner single quotes
// Ex: values = ["'Test Room'", 3]
addNewRoom: function(values, cb){
orm.insertOne("rooms", ["roomName", "gameLength"], values, function(res){
cb(res);
});
},

//=========================================
// HOST FUNCTION
//=========================================
// This function takes in a single value for the new score and a string that will serve as the condition
// in an SQL query
// Ex: values = 1 ; condition = "id = 31"
increaseScore: function(value, condition, cb){
orm.updateOne("players", "score", value, condition, function(res){
cb(res);
});
},
},

//This function takes in an array of values to make an SQL query to insert a row into the
//cards_played_this_round table
playCard: function(condition, cb){
orm.insertOne("cards_played_this_round", ["id", "room_id", "player_id"], values, function(res){
cb(res);
});
},

//this function takes in a player id
//this function takes in a player id
//and uses ORM function to query (READ) the cards table by that player_id
refreshHand: function(id, cb){
orm.selectAllByKeyValue("cards_to_deal", "player_id", id, function(res){
selectAllByRole: function(role, cb){
orm.selectAllByKeyValue("cards", "role", role, function(res){
cb(res);
});
},

//this function takes in a player id
//and uses ORM function to query (READ) the cards table by that player_id
refreshPlayerStats: function(id, cb){
orm.selectAllByKeyValue("players", "id", id, function(res){
selectAll: function(cb){
orm.selectAll("cards", function(res){
cb(res);
});
}
},

}

/*
// code to test refreshHand. Can be used in router later.
player.refreshHand(1, function(res){
// code to test drawCard. Can be used in router later.
player.drawCard(1, function(res){
console.log(res);
});
*/
Expand Down Expand Up @@ -118,5 +80,6 @@ player.playCard("id = 91", function(res){
});
*/


// Export the burger database functions for use by the controller (burgers_controller.js)
module.exports = player;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"postcss": "postcss -o dist/views/css/main.css src/public/css/style.css"
},
"dependencies": {
"body-parser": "^1.17.2",
"dotenv": "^4.0.0",
"express": "^4.15.4",
"mysql": "^2.14.1"
"mysql": "^2.14.1",
"path": "^0.12.7"
},
"devDependencies": {
"postcss": "^6.0.9",
Expand Down
4 changes: 4 additions & 0 deletions routes/htmlroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = function(app){
res.sendFile(path.join(__dirname + "/../dist/views/playerTrack.html"));
});

app.get('/test', function(req, res){
res.sendFile(path.join(__dirname + "/../dist/test.html"));
});

app.use(function(req, res){
res.sendFile(path.join(__dirname, "/../dist/views/landingpage.html"));
});
Expand Down
24 changes: 22 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
// *********************************************************************************
// Server.js - This file is the initial starting point for the Node/Express server.
// *********************************************************************************

// Dependencies
// =============================================================
var express = require("express");
var bodyParser = require("body-parser");
var path = require("path");

var port = process.env.PORT || 3000;
//access environmental variables for username, password, host
require('dotenv').config({path: 'dotenv.env'});

var port = process.env.PORT || 3006;
var app = express();


app.use(express.static(path.join(__dirname, '/dist')));

// Sets up the Express app to handle data parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: "application/vnd.api+json" }));

// The below points our server to a series of route files.
// ================================================================================
require('./routes/htmlroutes.js')(app);

var routes = require("./controllers/players_controller.js");
app.use("/", routes);


app.listen(port);
app.listen(port, function () {console.log("App listening on PORT " + port);});
Loading

0 comments on commit 95afaca

Please sign in to comment.