From 2a67892f599df291480ba750d405492852886130 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Mon, 25 Sep 2023 20:27:40 +0200 Subject: [PATCH] Add cash transaction api endpoint --- controllers/cashTransactionController.js | 41 ++++++++++++++++++++++++ models/cashTransactionModel.js | 8 +++++ routes/api/apiRouter.js | 2 ++ routes/api/cashTransactionRouter.js | 11 +++++++ views/cash.hbs | 4 +++ 5 files changed, 66 insertions(+) create mode 100644 controllers/cashTransactionController.js create mode 100644 models/cashTransactionModel.js create mode 100644 routes/api/cashTransactionRouter.js create mode 100644 views/cash.hbs diff --git a/controllers/cashTransactionController.js b/controllers/cashTransactionController.js new file mode 100644 index 0000000..40caa77 --- /dev/null +++ b/controllers/cashTransactionController.js @@ -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"}); + }); + }, +}; \ No newline at end of file diff --git a/models/cashTransactionModel.js b/models/cashTransactionModel.js new file mode 100644 index 0000000..5f84297 --- /dev/null +++ b/models/cashTransactionModel.js @@ -0,0 +1,8 @@ +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +var cashTransaction = new Schema({ + 'raw': String, +}); + +module.exports = mongoose.model('cashTransaction', cashTransaction); diff --git a/routes/api/apiRouter.js b/routes/api/apiRouter.js index 23c1284..52b2c26 100644 --- a/routes/api/apiRouter.js +++ b/routes/api/apiRouter.js @@ -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'}); diff --git a/routes/api/cashTransactionRouter.js b/routes/api/cashTransactionRouter.js new file mode 100644 index 0000000..e1778b8 --- /dev/null +++ b/routes/api/cashTransactionRouter.js @@ -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; \ No newline at end of file diff --git a/views/cash.hbs b/views/cash.hbs new file mode 100644 index 0000000..e3425ea --- /dev/null +++ b/views/cash.hbs @@ -0,0 +1,4 @@ +

Message:

+{{#each messages}} +
{{raw}}
+{{/each}} \ No newline at end of file