Add cash transaction api endpoint

This commit is contained in:
Nikola Petrov 2023-09-25 20:27:40 +02:00
parent 4e113cf8b4
commit 2a67892f59
5 changed files with 66 additions and 0 deletions

View 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"});
});
},
};

View 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);

View File

@ -4,11 +4,13 @@ var router = express.Router();
var photosRouter = require('./photoRouter');
var mediaRouter = require('./mediaRouter');
var gameRouter = require('./gameRouter');
var cashTransactionRouter = require('./cashTransactionRouter');
router.use('/photos', photosRouter);
router.use('/games', gameRouter);
router.use('/movies', mediaRouter);
router.use('/series', mediaRouter);
router.use('/cash', cashTransactionRouter);
router.get('/', function (req, res, next) {
res.status(200).json({message: 'API is working'});

View 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
View File

@ -0,0 +1,4 @@
<h1>Message:</h1>
{{#each messages}}
<div>{{raw}}</div>
{{/each}}