-
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.
Merge pull request #20 from nickcrisci/CleanUp
Clean up
- Loading branch information
Showing
26 changed files
with
4,124 additions
and
1,621 deletions.
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,17 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
commonjs: true, | ||
es2021: true, | ||
}, | ||
extends: 'airbnb-base', | ||
overrides: [ | ||
], | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
}, | ||
rules: { | ||
'no-console': 0, | ||
'linebreak-style': 0, | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
[ | ||
{ | ||
"name": "Nick", | ||
"cocktailPreferences": [ | ||
1, | ||
2, | ||
3 | ||
], | ||
"id": 1681903556356 | ||
} | ||
] |
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 |
---|---|---|
@@ -1,34 +1,29 @@ | ||
"use strict"; | ||
const bodyParser = require('body-parser'); | ||
const express = require('express'); | ||
|
||
const bodyParser = require("body-parser"), | ||
express = require("express"), | ||
path = require("path"); | ||
|
||
const routes = require(path.join(__dirname, "routes")); | ||
|
||
const middleWarePath = path.join(__dirname, "middleware"); | ||
const routes = require('./routes'); | ||
|
||
// Middleware for User Paths | ||
const userParamCheck = require(path.join(middleWarePath, "userParamCheck")), | ||
getUserMidd = require(path.join(middleWarePath, "getUserMidd")), | ||
limitUser = require(path.join(middleWarePath, "limitUser")), | ||
paginateUser = require(path.join(middleWarePath, "paginateUser")); | ||
const userParamCheck = require('./middleware/userParamCheck'); | ||
const getUserMidd = require('./middleware/getUserMidd'); | ||
const limitUser = require('./middleware/limitUser'); | ||
const paginateUser = require('./middleware/paginateUser'); | ||
|
||
const getCombination = require(path.join(middleWarePath, "getCombination")), | ||
cocktailFilter = require(path.join(middleWarePath, "cocktailFilter")); | ||
const getCombination = require('./middleware/getCombination'); | ||
const cocktailFilter = require('./middleware/cocktailFilter'); | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
// User Routes | ||
app.get("/users/:id?", getUserMidd, paginateUser, limitUser, routes.getUser); | ||
app.delete("/users/:id", routes.deleteUser); | ||
app.patch("/users/:id", routes.patchUser); | ||
app.post("/users", userParamCheck, routes.postUser); | ||
app.get('/users/:id?', getUserMidd, paginateUser, limitUser, routes.getUser); | ||
app.delete('/users/:id', routes.deleteUser); | ||
app.patch('/users/:id', routes.patchUser); | ||
app.post('/users', userParamCheck, routes.postUser); | ||
|
||
// Combination Routes | ||
app.get("/combination/track/:userId?", getCombination, cocktailFilter, routes.getTrackCombination); | ||
app.get("/combination/playlist/:userId?", getCombination, cocktailFilter, routes.getTrackCombination); | ||
app.get('/combination/track/:userId?', getCombination, cocktailFilter, routes.getTrackCombination); | ||
app.get('/combination/playlist/:userId?', getCombination, cocktailFilter, routes.getTrackCombination); | ||
|
||
module.exports = app; | ||
module.exports = app; |
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 |
---|---|---|
@@ -1,60 +1,93 @@ | ||
const filterCocktail = function(req, res, next) { | ||
const filterCocktail = (req, res, next) => { | ||
const cocktails = req.cocktails || []; | ||
const user = req.user || {}; | ||
const cocktailPreferences = user.cocktailPreferences || null; | ||
|
||
const cocktails = req.cocktails || [], | ||
user = req.user || {}, | ||
cocktailPreferences = user.cocktailPreferences || null; | ||
|
||
let results = []; | ||
let results = []; | ||
|
||
const cocktailListSize = 4; | ||
const cocktailListSize = 4; | ||
|
||
let include, exclude; | ||
let include; | ||
let exclude; | ||
|
||
if(cocktailPreferences) { | ||
include = cocktailPreferences.include || null; | ||
exclude = cocktailPreferences.exclude || null; | ||
} else { | ||
include, exclude = null, null; | ||
if (cocktailPreferences) { | ||
include = cocktailPreferences.include || null; | ||
exclude = cocktailPreferences.exclude || null; | ||
} else { | ||
include = null; | ||
exclude = null; | ||
} | ||
|
||
if (include && include.length < 1) include = null; | ||
if (exclude && exclude.length < 1) exclude = null; | ||
|
||
// Throws an error if both include and exclude are null | ||
if (!include && !exclude) { | ||
if (cocktails.length > cocktailListSize) { | ||
req.cocktails = req.cocktails.slice(0, cocktailListSize); | ||
} | ||
return next(); | ||
} | ||
|
||
cocktails.array.forEach((cocktail) => { | ||
let includeBool = false; | ||
let excludeBool = false; | ||
cocktail.ingredients.forEach((ingredient) => { | ||
if (include == null) { | ||
includeBool = true; | ||
} else if (include.some((element) => element === ingredient.name)) { | ||
includeBool = true; | ||
} | ||
|
||
if (exclude.some((element) => element === ingredient.name)) { | ||
excludeBool = true; | ||
} | ||
}); | ||
|
||
if(include && include.length < 1) include = null; | ||
if(exclude && exclude.length < 1) exclude = null; | ||
/* for (const ingredient of cocktail.ingredients) { | ||
if (include == null) { | ||
includeBool = true; | ||
} else if (include.some((element) => element == ingredient.name)) { | ||
includeBool = true; | ||
} | ||
// Throws an error if both include and exclude are null | ||
if (!include && !exclude) { | ||
if (cocktails.length > cocktailListSize) { | ||
req.cocktails = req.cocktails.slice(0, cocktailListSize); | ||
} | ||
return next(); | ||
if (exclude.some((element) => element == ingredient.name)) { | ||
excludeBool = true; | ||
break; | ||
} | ||
} */ | ||
if (includeBool && !excludeBool) { | ||
results.push(cocktail); | ||
} | ||
}); | ||
|
||
for(const cocktail of cocktails) { | ||
var includeBool = false; | ||
var excludeBool = false; | ||
for (const ingredient of cocktail["ingredients"]) { | ||
if(include == null) { | ||
includeBool = true; | ||
} else if (include.some(element => element == ingredient["name"])) { | ||
includeBool = true; | ||
} | ||
|
||
if(exclude.some(element => element == ingredient["name"])) { | ||
excludeBool = true; | ||
break; | ||
} | ||
} | ||
if(includeBool && !excludeBool) { | ||
results.push(cocktail); | ||
} | ||
/* for (const cocktail of cocktails) { | ||
let includeBool = false; | ||
let excludeBool = false; | ||
for (const ingredient of cocktail.ingredients) { | ||
if (include == null) { | ||
includeBool = true; | ||
} else if (include.some((element) => element == ingredient.name)) { | ||
includeBool = true; | ||
} | ||
if (exclude.some((element) => element == ingredient.name)) { | ||
excludeBool = true; | ||
break; | ||
} | ||
} | ||
if (includeBool && !excludeBool) { | ||
results.push(cocktail); | ||
} | ||
|
||
if (results.length > cocktailListSize) { | ||
results = results.slice(0, cocktailListSize); | ||
} | ||
} */ | ||
|
||
req.cocktails = results; | ||
if (results.length > cocktailListSize) { | ||
results = results.slice(0, cocktailListSize); | ||
} | ||
|
||
return next(); | ||
} | ||
req.cocktails = results; | ||
|
||
return next(); | ||
}; | ||
|
||
module.exports = filterCocktail; | ||
module.exports = filterCocktail; |
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 |
---|---|---|
@@ -1,83 +1,78 @@ | ||
"use strict"; | ||
const createErrorObject = require('../modules/createErrorObject'); | ||
|
||
const path = require('path'), | ||
createErrorObject = require(path.join(__dirname, "..", "modules", "createErrorObject")); | ||
const getUserById = require('../modules/getUserById'); | ||
const getCocktail = require('../modules/getCocktail'); | ||
const getSeedGenres = require('../modules/getSeedGenres'); | ||
const { spotifyRecommendationWithGenre } = require('../modules/spotify'); | ||
|
||
const modulePath = path.join(__dirname, "..", "modules"); | ||
const getCombination = async (req, res, next) => { | ||
// Has to be an object because "in" checks for properties in Objects, | ||
// not in list (in a list it checks for an index) | ||
const allowedQueries = { mood: true, limit: true, market: true }; | ||
|
||
const getUserById = require(path.join(modulePath, "getUserById")), | ||
getCocktail = require(path.join(modulePath, "getCocktail")), | ||
getSeedGenres = require(path.join(modulePath, "getSeedGenres")), | ||
getPlaylist = require(path.join(modulePath, "getSpotifyRecommendation")); | ||
req.query.forEach((query) => { | ||
if (!(query in allowedQueries)) { | ||
const error = createErrorObject({ status: 400, title: 'Bad Request', source: { parameter: query } }); | ||
return res.status(400).send(error); | ||
} | ||
}); | ||
|
||
const getCombination = async function(req, res, next) { | ||
// This allows the middleware to be used for both the track & playlist combination | ||
// If the track path is invoked the limit will be set to 1, | ||
// else it will be set to the given limit in the query, or null if not specified | ||
let limit; | ||
|
||
// Has to be an object because "in" checks for properties in Objects, not in list (in a list it checks for an index) | ||
const allowedQueries = {"mood": true, "limit": true, "market": true}; | ||
if (req.originalUrl.includes('/combination/track')) { | ||
limit = 1; | ||
} else { | ||
limit = req.query.limit || null; | ||
} | ||
|
||
// Checks for illegal query | ||
for (const query in req.query) { | ||
if( ! (query in allowedQueries) ) { | ||
const error = createErrorObject({ status: 400, title: "Bad Request", source: { parameter: query } }); | ||
return res.status(400).send(error); | ||
} | ||
} | ||
const userId = req.params.userId || null; | ||
const mood = req.query.mood || 'Froh'; | ||
const market = req.query.market || null; | ||
|
||
// This allows the middleware to be used for both the track & playlist combination | ||
// If the track path is invoked the limit will be set to 1, else it will be set to the given limit in the query, or null if not specified | ||
let limit; | ||
getCocktail(mood, (err, result) => { | ||
req.cocktails = result; | ||
}); | ||
|
||
if (req.originalUrl.includes("/combination/track")) { | ||
limit = 1; | ||
} else { | ||
limit = req.query.limit || null; | ||
} | ||
const genreSeeds = getSeedGenres(mood); | ||
|
||
const userId = req.params.userId || null; | ||
const mood = req.query.mood || 'Froh'; | ||
const market = req.query.market || null ; | ||
|
||
getCocktail(mood,(err,result) => { | ||
req.cocktails = result; | ||
}); | ||
if (genreSeeds === null) { | ||
const error = createErrorObject({ status: 404, title: 'Mood not found', source: { parameter: 'mood' } }); | ||
return res.status(error.status).send(error); | ||
} | ||
|
||
const genre_seeds = getSeedGenres(mood); | ||
|
||
if (genre_seeds.status) { | ||
return res.status(genre_seeds.status).send(genre_seeds); | ||
} | ||
|
||
// Spotify Request - to eliminate duplicate code | ||
const getTracks = preferences => { | ||
getPlaylist.spotify_RecommendationWithGenre( | ||
new getPlaylist.AuthForToken(), | ||
preferences, | ||
{limit, market, genre_seeds}, | ||
(err, tracks) => { | ||
if (err) { | ||
const error = createErrorObject({ status: 503, title: "Bad Gateway" }); | ||
return res.status(503).send(error); | ||
} | ||
req.tracks = tracks; | ||
return next(); | ||
} | ||
); | ||
} | ||
// Spotify Request - to eliminate duplicate code | ||
const getTracks = (preferences) => { | ||
spotifyRecommendationWithGenre( | ||
preferences, | ||
{ limit, market, genreSeeds }, | ||
(err, tracks) => { | ||
if (err) { | ||
const error = createErrorObject({ status: 503, title: 'Bad Gateway' }); | ||
return res.status(503).send(error); | ||
} | ||
req.tracks = tracks; | ||
return next(); | ||
}, | ||
); | ||
}; | ||
|
||
if (userId) { | ||
await getUserById(userId, (err, userObject) => { | ||
if (err) { | ||
const error = createErrorObject({ status: 404, title: "Not found" }); | ||
return res.status(404).send(error); | ||
} | ||
const musicPreferences = userObject.musicPreferences || []; | ||
req.user = userObject; | ||
if (userId) { | ||
await getUserById(userId, (err, userObject) => { | ||
if (err) { | ||
const error = createErrorObject({ status: 404, title: 'Not found' }); | ||
return res.status(404).send(error); | ||
} | ||
const musicPreferences = userObject.musicPreferences || []; | ||
req.user = userObject; | ||
|
||
getTracks(musicPreferences); | ||
}); | ||
} else { | ||
getTracks([]); | ||
} | ||
} | ||
getTracks(musicPreferences); | ||
}); | ||
} else { | ||
getTracks([]); | ||
} | ||
}; | ||
|
||
module.exports = getCombination; |
Oops, something went wrong.