Add cash transaction api endpoint
This commit is contained in:
parent
4e113cf8b4
commit
2a67892f59
41
controllers/cashTransactionController.js
Normal file
41
controllers/cashTransactionController.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
var cashTransactionModel = require('../models/cashTransactionModel.js');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
|
||||||
|
list: function (req, res) {
|
||||||
|
cashTransactionModel.find()
|
||||||
|
.then(transactions => {
|
||||||
|
var data = {
|
||||||
|
messages: transactions
|
||||||
|
};
|
||||||
|
return res.render('cash', data);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
return res.status(500).json({
|
||||||
|
message: 'Error when getting transactions.',
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
create: async function (req, res) {
|
||||||
|
var transaction = new cashTransactionModel({
|
||||||
|
raw: req.body.messageBody
|
||||||
|
});
|
||||||
|
|
||||||
|
const trans = await transaction.save()
|
||||||
|
if(trans){
|
||||||
|
return res.status(201).json({ message: 'Welcome to the API POST' });
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return res.status(400).json({message: "something went wrong"});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
delete: function (req, res) {
|
||||||
|
cashTransactionModel.deleteMany({})
|
||||||
|
.then(data =>{
|
||||||
|
res.status(201).json({message:"OK"});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
8
models/cashTransactionModel.js
Normal file
8
models/cashTransactionModel.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
var mongoose = require('mongoose');
|
||||||
|
var Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
var cashTransaction = new Schema({
|
||||||
|
'raw': String,
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model('cashTransaction', cashTransaction);
|
@ -4,11 +4,13 @@ var router = express.Router();
|
|||||||
var photosRouter = require('./photoRouter');
|
var photosRouter = require('./photoRouter');
|
||||||
var mediaRouter = require('./mediaRouter');
|
var mediaRouter = require('./mediaRouter');
|
||||||
var gameRouter = require('./gameRouter');
|
var gameRouter = require('./gameRouter');
|
||||||
|
var cashTransactionRouter = require('./cashTransactionRouter');
|
||||||
|
|
||||||
router.use('/photos', photosRouter);
|
router.use('/photos', photosRouter);
|
||||||
router.use('/games', gameRouter);
|
router.use('/games', gameRouter);
|
||||||
router.use('/movies', mediaRouter);
|
router.use('/movies', mediaRouter);
|
||||||
router.use('/series', mediaRouter);
|
router.use('/series', mediaRouter);
|
||||||
|
router.use('/cash', cashTransactionRouter);
|
||||||
|
|
||||||
router.get('/', function (req, res, next) {
|
router.get('/', function (req, res, next) {
|
||||||
res.status(200).json({message: 'API is working'});
|
res.status(200).json({message: 'API is working'});
|
||||||
|
11
routes/api/cashTransactionRouter.js
Normal file
11
routes/api/cashTransactionRouter.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
const cashTransaction = require('../../controllers/cashTransactionController.js')
|
||||||
|
|
||||||
|
router.get('/', cashTransaction.list);
|
||||||
|
|
||||||
|
router.post('/', cashTransaction.create);
|
||||||
|
|
||||||
|
router.delete('/', cashTransaction.delete);
|
||||||
|
|
||||||
|
module.exports = router;
|
4
views/cash.hbs
Normal file
4
views/cash.hbs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<h1>Message:</h1>
|
||||||
|
{{#each messages}}
|
||||||
|
<div>{{raw}}</div>
|
||||||
|
{{/each}}
|
Loading…
x
Reference in New Issue
Block a user