Skip to content

Commit

Permalink
added getMOives query #3
Browse files Browse the repository at this point in the history
  • Loading branch information
shaima96 committed Dec 5, 2018
1 parent fa9041f commit 69457f2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
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 69457f2

Please sign in to comment.