diff --git a/get.rest b/get.rest
index 43e17d29d132c4b5a35bb5165f1c1b58e2386f48..035a74694f189d67ab9e05bfe748ca6c3a929503 100644
--- a/get.rest
+++ b/get.rest
@@ -19,7 +19,7 @@ GET {{host}}/fruit?limit=2&offset=2 HTTP/1.1
GET {{host}}/fruit?limit=3&offset=1&name=b HTTP/1.1
###
-GET {{host}}/fruit?offset=7 HTTP/1.1
+GET {{host}}/fruit?offset=-7 HTTP/1.1
###
GET {{host}}/fruit?ahag=1&name=b HTTP/1.1
diff --git a/index.js b/index.js
index 6871227d09538bb146b72f60a5ab27fdd7ddf591..1d31c9d7c5a53a1b06d8d3393172899051fb99de 100644
--- a/index.js
+++ b/index.js
@@ -10,23 +10,24 @@ app.listen( PORT, ( err )=>{
console.log(`Server running on ${PORT}`)
})
-// import API specific modules
-
-const query_sanitizer = require('./js/query_sanitizer')
-const parse_query_into_results = require('./js/parse_query_into_results')
-
// import DB
const fruit_db = require('./db/fruit_db')
+// import API specific modules
+
+const query_parser = require('./js/query_parser')
+const db_filter = require('./js/db_filter')
+
// API
app.get('/fruit', (req,res)=>{
- let { params, response } = query_sanitizer( req.query )
- if( response.code === 400 ){
- res.status(response.code).send({ error: response.string })
+ let { error, db_options } = query_parser( req.query )
+
+ if ( error ){
+ res.status(400).send({ error: error })
} else {
- res.json( parse_query_into_results( params, fruit_db ) )
+ res.send( db_filter( db_options, fruit_db ))
}
})
\ No newline at end of file
diff --git a/js/db_filter.js b/js/db_filter.js
new file mode 100644
index 0000000000000000000000000000000000000000..f137d841ee6a30dce602bfc1d90305c73ca131b1
--- /dev/null
+++ b/js/db_filter.js
@@ -0,0 +1,48 @@
+module.exports = db_filter = ( db_options, fruit_db ) => {
+
+ // create a copy of fruit_db rather than a binding
+
+ let results = [ ...fruit_db ]
+
+ // 5 parameters: offset, limit, inSeason, color, name
+
+ if ( db_options.name ){
+
+ let name = db_options.name.toLowerCase()
+
+ results = results.filter(
+ fruit => fruit.name.toLowerCase().slice(0,name.length) === name
+ )
+ }
+
+ if( db_options.colors ){
+
+ results = results.filter(
+ fruit => fruit.colors.some(
+ color => db_options.colors.toLowerCase().includes( color.toLowerCase() )
+ )
+ )
+ }
+
+ if( db_options.inSeason === "true" ){
+
+ results = results.filter(
+ fruit => fruit.inSeason
+ )
+
+ } else if ( db_options.inSeason === "false" ){
+
+ results = results.filter(
+ fruit => !fruit.inSeason
+ )
+ }
+
+ if( db_options.offset ){
+
+ results = results.slice( db_options.offset )
+ }
+
+ results = results.slice( 0, db_options.limit )
+
+ return results
+}
\ No newline at end of file
diff --git a/js/parse_query_into_results.js b/js/parse_query_into_results.js
deleted file mode 100644
index 33b0656c83fe1664781e42b6bdda9246e5ea6cc4..0000000000000000000000000000000000000000
--- a/js/parse_query_into_results.js
+++ /dev/null
@@ -1,49 +0,0 @@
-module.exports = parse_query_into_results = ( query_params, db ) => {
-
- let fruit_db = db
- let params = { ...query_params }
-
- //5 parameters: offset, limit, inSeason, color, name
-
- let results = [ ...fruit_db ]
-
- if ( params.name ){
-
- let name = params.name.toLowerCase()
-
- results = results.filter(
- fruit => fruit.name.toLowerCase().slice(0,name.length) === name
- )
- }
-
- if( params.colors ){
-
- results = results.filter(
- fruit => fruit.colors.some(
- color => params.colors.includes( color )
- )
- )
- }
-
- if( params.inSeason === "true" ){ // inSeason set to "true"
-
- results = results.filter(
- fruit => fruit.inSeason
- )
-
- } else if ( params.inSeason === "false" ){// inSeason set to "false"
-
- results = results.filter(
- fruit => !fruit.inSeason
- )
- }
-
- if( params.offset ){
-
- results = results.slice( params.offset )
- }
-
- results = results.slice( 0, params.limit )
-
- return results
-}
diff --git a/js/query_parser.js b/js/query_parser.js
new file mode 100644
index 0000000000000000000000000000000000000000..529331073e9962265af274d992748329b4a3e17f
--- /dev/null
+++ b/js/query_parser.js
@@ -0,0 +1,46 @@
+module.exports = query_parser = ( query_params ) => {
+
+ const base_params = {
+ limit: 5,
+ }
+
+ if( query_params.offset ){
+
+ let offset = Number( query_params.offset )
+ if( !Number.isInteger( offset ) || offset < 0 || offset > ( fruit_db.length - 1 ) ){
+
+ return { error: `Offset must be an integer value between 0 and ${ fruit_db.length - 1 }.` }
+ }
+ }
+
+ if( query_params.limit ){
+
+ let limit = Number( query_params.limit )
+ if( !Number.isInteger( limit ) || limit < 1 || limit > ( fruit_db.length ) ){
+
+ return { error: `Limit must be an integer value between 1 and ${ fruit_db.length }.` }
+ }
+ }
+
+ if( query_params.inSeason ){
+
+ let inSeason = query_params.inSeason
+ if(inSeason !== "true" && inSeason !== "false"){
+
+ return { error: `inSeason must be set to either true or false.` }
+ }
+ }
+
+ if( query_params.name ){
+
+ let name = query_params.name
+ if( typeof name !== "string" ){
+
+ return { error: `name must be a string.` }
+ }
+ }
+
+ let db_options = Object.assign( base_params, query_params )
+
+ return { db_options }
+}
\ No newline at end of file
diff --git a/js/query_sanitizer.js b/js/query_sanitizer.js
deleted file mode 100644
index e6a42cffb92219ff3c12d9a00492f472cab56bb2..0000000000000000000000000000000000000000
--- a/js/query_sanitizer.js
+++ /dev/null
@@ -1,57 +0,0 @@
-module.exports = query_sanitizer = ( req_query ) => {
-
- const base_params = {
- limit: 5,
- }
-
- const response = {
- code: '',
- string: ''
- }
-
- let query_params = { ...req_query }
-
- if( query_params.offset ){
-
- let offset = Number( query_params.offset )
- if( !Number.isInteger( offset ) || offset < 0 || offset > ( fruit_db.length - 1 ) ){
- response.code = 400
- response.string =
- `Offset must be an integer value between 0 and ${ fruit_db.length - 1 }.`
- }
- }
-
- if( query_params.limit ){
-
- let limit = Number( query_params.limit )
- if( !Number.isInteger( limit ) || limit < 1 || limit > ( fruit_db.length ) ){
- response.code = 400
- response.string =
- `Limit must be an integer value between 1 and ${ fruit_db.length }.`
- }
- }
-
- if( query_params.inSeason ){
-
- let inSeason = query_params.inSeason
- if(inSeason !== "true" && inSeason !== "false"){
- response.code = 400
- response.string =
- `inSeason must be set to either true or false`
- }
- }
-
- if( query_params.name ){
-
- let name = query_params.name
- if( typeof name !== "string" ){
- response.code = 400
- response.string =
- `name must be a string`
- }
- }
-
- let params = Object.assign( base_params, query_params )
-
- return { params, response }
-}
\ No newline at end of file