Skip to content

Commit

Permalink
Merge pull request #18 from FACK1/queries
Browse files Browse the repository at this point in the history
added getMovies  query #3
  • Loading branch information
shaima96 authored Dec 5, 2018
2 parents 9c6648c + a75352f commit eda2769
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon ./src/server/server.js",
"start": "node ./src/server/server.js",
"dev": "nodemon ./src/server/server.js",
"test": "tape ./src/test/test.js | tap-spec ",
"lint": "./node_modules/.bin/eslint ./src/server/**/*.js",
"lint-fix": "./node_modules/.bin/eslint --fix ./src/**/*.js"
Expand All @@ -22,8 +23,8 @@
"dependencies": {
"env2": "^2.2.2",
"istanbul": "^0.4.5",
"nodemon": "^1.18.7",
"pg": "^7.7.1",
"request": "^2.88.0",
"tape": "^4.9.1"
},
"devDependencies": {
Expand All @@ -33,6 +34,7 @@
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"nodemon": "^1.18.7",
"tap-spec": "^5.0.0"
}
}
18 changes: 18 additions & 0 deletions src/handlerfunction/searchMoive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require('env2')('.env')
const request = require('request');
const handlers = require('../server/handlers');
const getMoives = require('../queries/getMovies.js');

const search = (req, res, value) => {
getMoives(value,(err,result)=>{
if(err){
console.log(err);
}else {
res.writeHead(200,{ 'content-type': 'application/json '});
const output=JSON.stringify(result);
res.end(output);
}
})
};

module.exports = { search };
11 changes: 11 additions & 0 deletions src/queries/getMovies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const dbConnection = require('../database/db_connection.js');

const getMoives = (title,cb) => {
const query = 'SELECT * FROM movies where title = $1';
dbConnection.query(query ,[title], (err, res) => {
if (err) return cb(err);
cb(null, res.rows);
});
};

module.exports =getMoives;
66 changes: 39 additions & 27 deletions src/server/handlers.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,64 @@
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
const fs = require('fs')
const path = require('path')
const querystring=require('querystring');
const searchMoive=require('../handlerfunction/searchMoive.js')

const errorHandler = (request, response) => {
response.writeHead(404, { 'content-type': 'text/html' });
response.end('<h1> Page Not Found </h1>');
};
response.writeHead(404, { 'content-type': 'text/html' })
response.end('<h1> Page Not Found </h1>')
}

const homeHandler = (request, response) => {
const htmlPath = path.join(__dirname, '..', '..', 'public', 'index.html');
const htmlPath = path.join(__dirname, '..', '..', 'public', 'index.html')
fs.readFile(htmlPath, (error, htmlFile) => {
if (error) {
response.writeHead(500, { 'content-Type': 'text/html' });
response.end('<h1> Server error! sorry</h1>');
return;
response.writeHead(500, { 'content-Type': 'text/html' })
response.end('<h1> Server error! sorry</h1>')
return
}
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(htmlFile);
response.end();
});
};
response.writeHead(200, { 'Content-Type': 'text/html' })
response.write(htmlFile)
response.end()
})
}

const publicHandler = (request, response) => {
const extension = request.url.split('.')[1];
const extension = request.url.split('.')[1]
const contentTypeMapping = {
html: 'text/html',
css: 'text/css',
js: 'application/js',
};
js: 'application/js'
}
if (!contentTypeMapping[extension]) {
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end('<h1> Not Found </h1>');
response.writeHead(404, { 'Content-Type': 'text/html' })
response.end('<h1> Not Found </h1>')
} else {
const filePath = path.join(__dirname, '..', '..', 'public', request.url);
const filePath = path.join(__dirname, '..', '..', 'public', request.url)
fs.readFile(filePath, (error, file) => {
if (error) {
errorHandler(request, response);
errorHandler(request, response)
}
response.writeHead(200, { 'content-type': contentTypeMapping[extension] });
response.end(file);
});
response.writeHead(200, { 'content-type': contentTypeMapping[extension] })
response.end(file)
})
}
}

const searchMoviesHandler = (request, response) => {
if (!request.url.includes('/search?value=')) {
errorHandler(request, response);
}else {
const parsedQuery = querystring.parse(request.url.split('?')[1]);
searchMoive.search(request, response, parsedQuery.value);
}
};



module.exports = {
homeHandler,
publicHandler,
errorHandler
errorHandler,
searchMoviesHandler,

};
}

0 comments on commit eda2769

Please sign in to comment.