Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Answer for the Node-challage-master #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,27 @@ The command above will run the following test suites sequentially:


Happy hacking 😁!

##First Task

The first thing i did is downloading all the required modules and softwares needed for the challange. I used Node.js, Express.js,Jest.js,Supertest,body-parser,Posgres and dotenv files.

##Second Task

Then i created a database in my posgres called "challenge" and import dump.sql file to that database. And in my node application i created a local server and connect it to port 8080 and also connect my node application to database.
##Third Task

I used REST api to access the data in the database. So i use GET method to fetch the all the data. One thing i added here even though the challenge only ask to fetch the data, i used all the CRUD application so that the application allow to add,update,delete expenses from the database.Not only that i added a method that will allow to get a specific data using its ID. I used express,body-parser and dotenv in the application.

##Forth Task

After that i wrote a code that will filter expenses based on "amount_in_cents" and "status". I gave a specific query to select data that are greater than 6000 and status pending to test it. Also added a sorting function to sort by "amount_in_cents". Also a pagination function will put data in pages.

##Final Task

The final task is to test the application. I used a "Jest.js" and "supertest" for testing the project.

##Additional

Additional thing is that i also done the same application using Node.js and MongoDB. so request me if you are interested to see it.

16 changes: 16 additions & 0 deletions __test__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

const server = require('./api');
const supertest = require('supertest');
const app = require('./server')
describe('GET /expenses', function(){
it('respond with json', function(done){
request(app)
.get('/expenses')
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res){
if (err) return done(err);
done()
});
})
});
100 changes: 100 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const clients = require('./connection');
const app = require('./server')
clients.connect();

app.get('/expenses',(req,res)=>{
//============SORTING===========
var sort = (function(err){
if(err)
throw err;
con.query("SELECT * from expenses by amount_in_cents",function(err,result,fields){
if(err)throw err;
console.log(result)
})
})
//=============FILTERING===============
var filter = (function(db_error) {
if (db_error) throw db_error;
console.log("Connected!");
var sql = "select * from expenses where amount_in_cents >='6000' and status='pending'";
DB_Conn.query(sql, function (db_error, result) {
if (db_error) throw db_error;
console.log(result);
});

});
//============PAGINATION===================
var numRows;
var queryPagination;
var numPerPage = parseInt(req.query.npp, 10) || 1;
var page = parseInt(req.query.page, 10) || 0;
var numPages;
var skip = page * numPerPage;
// Here we compute the LIMIT parameter
var limit = skip + ',' + numPerPage;
queryAsync('SELECT count(*) as numRows FROM expenses')
.then(function(results) {
numRows = results[0].numRows;
numPages = Math.ceil(numRows / numPerPage);
console.log('number of pages:', numPages);

clients.query((err,result)=>{
if(!err){
res.send(result.rows)
}
})
clients.end;
})
app.get('/expenses/:id',(req,res)=>{
clients.query(`Select * from users where id=${req.params.id}`)
if(!err){
res.send(result.rows);
}
clients.end;
})
app.post('/expenses',(req,res)=>{
const user = req.body;
let insertQuery = `Insert into expenses(id,merchant_name,amount_in_cents,currency,user_id,date_created,status)
values${expenses.id},${expenses.merchant_name},${expenses.amount_in_cents},${expenses.currency},${expenses.user_id},${expenses.date_created},${expenses.status}`
clients.query(insertQuery,(err,result)=>{
if(!err){
res.send(`Insertion is Sucessful!!!`)
}
else{
console.log(err.message)
}
})
clients.end;
})
app.put('/expenses/:id',(req,res)=>{
let user = req.body;
let UpdateQuery = `update expenses
set merchant_name ='${expenses.merchant_name}',
amount_in_cents='${amount_in_cents},
currency = '${expenses.currency}',
user_id = '${expenses.user_id}',
date_created ='${expenses.date_created}',
status = '${expenses.status}',
where id = '${expences.id}'`
clients.query(UpdateQuery, (err,result)=>{
if(!err){
res.send('Update was sucessful!!!')
}else{
console.log(err.message)
}
})
clients.end;
})
app.delete('./expenses/:id',(req,res)=>{
let deleteQuery = `delete form expenses where id=${req.params.id}`
clients.query(deleteQuery, (err,result)=>{
if(!err){
res.send('Deletion was Sucessful!!!')
}else{
console.log(err.message)
}
})
clients.end;
})
})
module.exports = app;
9 changes: 9 additions & 0 deletions connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {Client} = require('pg')
const client = new Client({
host:"localhost",
user: "postgres",
port: 5432,
password:"1234",
database: "challange"
})
module.exports = client;