-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added inventoryModel.js and listingModel.js with some database select…
…ion logic
- Loading branch information
Showing
5 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const inventoryModel = require('../models/inventoryModel'); | ||
|
||
|
||
function getUserInventory(req,res,next){ | ||
inventoryModel.getUserInventory(parseInt(req.params.id)) | ||
.then((inventory)=>{ | ||
res.json(inventory); // this was: res.render('inventory', {inventory}); | ||
}) | ||
.catch((err)=>{ | ||
res.status(404); | ||
next(err); | ||
}) | ||
|
||
} | ||
|
||
module.exports = { | ||
getUserInventory | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const listingModel = require('../models/listingModel'); | ||
|
||
function getAllListings(req,res,next){ | ||
listingModel.getAllListings() | ||
.then((listings)=>{ | ||
res.json(listings); | ||
}) | ||
.catch((err)=>{ | ||
res.status(404); | ||
next(err); | ||
}) | ||
} | ||
|
||
function getCTListings(req,res,next){ | ||
listingModel.getListingByTeam(CT) | ||
.then((listing)=>{ | ||
res.json(listing); | ||
}) | ||
.catch((err)=>{ | ||
res.status(404); | ||
next(err); | ||
}) | ||
} | ||
|
||
function getTListings(req,res,next){ | ||
listingModel.getListingByTeam(T) | ||
.then((listing)=>{ | ||
res.json(listing); | ||
}) | ||
.catch((err)=>{ | ||
res.status(404); | ||
next(err); | ||
}) | ||
} | ||
|
||
function addListing(req,res,next){ | ||
listingModel.addListing(req.body) // req.body is the data from the form, is then parsed into "listingData" in the model | ||
.then((listing)=>{ | ||
res.json(listing); // redirect to /listings?? | ||
}) | ||
.catch((err)=>{ | ||
res.status(404); | ||
next(err); | ||
}) | ||
} | ||
|
||
|
||
module.exports = { | ||
getAllListings, | ||
getCTListings, | ||
getTListings, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//database access | ||
|
||
const db = require('../config/database').config; | ||
|
||
let getUserInventory = (id) => new Promise((resolve,reject)=>{ // the ID is the user ID | ||
db.query(`SELECT CCL_inventory.userWeaponID, CCL_weapons.id, CCL_weapons.image, CCL_weapons.rarity, CCL_weapons.name FROM CCL_inventory | ||
INNER JOIN CCL_weapons | ||
ON CCL_inventory.weaponID = CCL_weapons.id | ||
WHERE CCL_inventory.userID =${id};`, function (err, inventory, fields){ | ||
if(err){ | ||
reject(err); | ||
} | ||
resolve(inventory); | ||
}); | ||
}); | ||
|
||
|
||
module.exports= { | ||
getUserInventory | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const db = require('../config/database').config; | ||
|
||
let getAllListings = () => new Promise(async (resolve,reject)=>{ | ||
let sql = `SELECT * FROM CCL_listings`; | ||
db.query(sql, function (err, listing, fields){ | ||
if(err){ | ||
reject(err); | ||
} | ||
resolve(listing[0]); | ||
}); | ||
}); | ||
|
||
// get all info by a specific listing: | ||
// SELECT * FROM CCL_listings INNER JOIN CCL_inventory ON CCL_listings.sellerWeaponID = CCL_inventory.userWeaponID INNER JOIN CCL_weapons ON CCL_inventory.weaponID = CCL_weapons.id WHERE [key] = [value] | ||
let getListingByTeam = (Team) => new Promise(async (resolve,reject)=>{ | ||
|
||
let sql = "SELECT * FROM CCL_listings INNER JOIN CCL_inventory ON CCL_listings.sellerWeaponID = CCL_inventory.userWeaponID INNER JOIN CCL_weapons ON CCL_inventory.weaponID = CCL_weapons.id WHERE CCL_weapons.team = " + db.escape(Team); | ||
console.log(sql); | ||
|
||
db.query(sql, function (err, listing, fields){ | ||
if(err){ | ||
reject(err); | ||
} | ||
resolve(listing); | ||
}); | ||
|
||
}); | ||
let addListing = (listingData) => new Promise( async (resolve,reject)=> { | ||
|
||
let sql = "INSERT INTO CCL_listings (sellerID, sellerWeaponID, price) VALUES (" + | ||
db.escape(listingData.team) + ", " + | ||
db.escape(listingData.price)+ ", " + | ||
db.escape(listingData.weaponID)+ ")" ; | ||
console.log(sql); | ||
|
||
db.query(sql, function (err, result, fields){ | ||
if(err) { | ||
reject(err) | ||
} | ||
resolve(listingData) | ||
}); | ||
|
||
}); | ||
|
||
module.exports = { | ||
getAllListings, | ||
getListingByTeam, | ||
addListing, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters