1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-24 13:39:35 +02:00

SQLite database. App model and controller

This commit is contained in:
unknown 2021-05-10 17:47:26 +02:00
parent 8392c2422a
commit 2acc3b72ec
11 changed files with 1185 additions and 32 deletions

79
controllers/apps.js Normal file
View file

@ -0,0 +1,79 @@
const asyncWrapper = require('../middleware/asyncWrapper');
const ErrorResponse = require('../utils/ErrorResponse');
const App = require('../models/App');
// @desc Create new app
// @route POST /api/apps
// @access Public
exports.createApp = asyncWrapper(async (req, res, next) => {
const app = await App.create(req.body);
res.status(201).json({
success: true,
data: app
})
})
// @desc Get all apps
// @route GET /api/apps
// @access Public
exports.getApps = asyncWrapper(async (req, res, next) => {
const apps = await App.findAll();
res.status(200).json({
success: true,
data: apps
})
})
// @desc Get single app
// @route GET /api/apps/:id
// @access Public
exports.getApp = asyncWrapper(async (req, res, next) => {
const app = await App.findOne({
where: { id: req.params.id }
});
if (!app) {
return next(new ErrorResponse(`App with id of ${req.params.id} was not found`, 404));
}
res.status(200).json({
success: true,
data: app
})
})
// @desc Update app
// @route PUT /api/apps/:id
// @access Public
exports.updateApp = asyncWrapper(async (req, res, next) => {
let app = await App.findOne({
where: { id: req.params.id }
});
if (!app) {
return next(new ErrorResponse(`App with id of ${req.params.id} was not found`, 404));
}
app = await app.update({ ...req.body });
res.status(200).json({
success: true,
data: app
})
})
// @desc Delete app
// @route DELETE /api/apps/:id
// @access Public
exports.deleteApp = asyncWrapper(async (req, res, next) => {
await App.destroy({
where: { id: req.params.id }
})
res.status(200).json({
success: true,
data: {}
})
})