Skip to content

Commit

Permalink
Merge pull request #2 from RolandoAndrade/CRUD-transacciones
Browse files Browse the repository at this point in the history
Buscar transacciones
  • Loading branch information
RolandoAndrade authored Dec 22, 2018
2 parents 04d5969 + 8dcc094 commit bbb6910
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ app.use(function(err, req, res, next) {
});
});

/*
//si el puerto está en uso
var server = app.listen(5000, function() {
console.log('Ready on port %d', server.address().port);
});
*/

module.exports = app;
62 changes: 60 additions & 2 deletions queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ function searchUser(req, res, next)
'and LOWER(first_surname) like LOWER('+"'%"+(first_surname||"")+"%'"+')'+
'and LOWER(second_surname) like LOWER('+"'%"+(second_surname||"")+"%'"+')'+
'and LOWER(email) like LOWER('+"'%"+(email||"")+"%'"+')';
console.log(qu);
db.any(qu)
.then(function (data)
{
Expand All @@ -168,6 +167,10 @@ function searchUser(req, res, next)

function getAllTransactions(req, res, next)
{
if(req.query.toString().length>0)
{
return searchTransaction(req,res,next);
}
db.any('select * from transactions')
.then(function (data)
{
Expand Down Expand Up @@ -243,7 +246,7 @@ function updateTransactions(req, res, next)
{
req.body.customer=req.body.customer||data.customer;
req.body.date=new Date(req.body.date)||data.date;
req.body.amount=req.body.first_surname||data.amount;
req.body.amount=req.body.amount||data.amount;
db.none('update transactions set customer=$1, date=$2, amount=$3' +
' where id=$4',
[req.body.customer, req.body.date, req.body.amount, req.body.second_surname, parseInt(req.params.id)])
Expand All @@ -269,6 +272,60 @@ function updateTransactions(req, res, next)

}

function removeTransaction(req, res, next)
{
const userID = parseInt(req.params.id);
db.result('delete from transactions where id = $1', userID)
.then(function (result)
{
res.status(200)
.json({
status: 'success',
message: `Removed ${result.rowCount} transaction`
});
})
.catch(function (err)
{
return next(err);
});
}

function searchTransaction(req, res, next)
{
const customer = req.query.customer;
const date = req.query.date||"";
const amount = req.query.amount||"";
let qu;
if(customer!==undefined)
{
qu='select * from transactions' +
' where customer='+customer+
' and TO_CHAR(date,\'dd.mm.yyyy\') LIKE '+"'%"+date+"%'"+
' and CAST(amount AS VARCHAR(20)) like '+"'%"+amount+"%'";
}
else
{
qu='select * from transactions' +
' where'+
' TO_CHAR(date,\'dd.mm.yyyy\') LIKE '+"'%"+date+"%'"+
' and CAST(amount AS VARCHAR(20)) like '+"'%"+amount+"%'";
}
db.any(qu)
.then(function (data)
{
res.status(200)
.json({
status: 'success',
data: data,
});
})
.catch(function (err)
{
return next(err);
});
}


module.exports = {
getAllUsers: getAllUsers,
getUserByID: getUserByID,
Expand All @@ -279,4 +336,5 @@ module.exports = {
getTransactionByID:getTransactionByID,
createATransaction: createATransaction,
updateTransactions: updateTransactions,
removeTransaction: removeTransaction,
};
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ router.post('/api/transactions', db.createATransaction);

router.put('/api/transactions/:id(\\d+)', db.updateTransactions);

router.delete('/api/transactions/:id', db.removeTransaction);

// application -------------------------------------------------------------
router.get('/', function (req, res) {

Expand Down
2 changes: 2 additions & 0 deletions sqlCreators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ SELECT * FROM transactions

INSERT INTO transactions (customer, date, amount)
VALUES (1, '2018-12-16', 10.3);

select * from transactions where CAST(amount AS VARCHAR(20)) like '%11%'

0 comments on commit bbb6910

Please sign in to comment.