Bookmarks controller

This commit is contained in:
unknown 2021-05-23 17:18:04 +02:00
parent 100f274d96
commit 27250dc850
7 changed files with 118 additions and 7 deletions

1
api.js
View File

@ -15,6 +15,7 @@ api.use('/api/apps', require('./routes/apps'));
api.use('/api/config', require('./routes/config')); api.use('/api/config', require('./routes/config'));
api.use('/api/weather', require('./routes/weather')); api.use('/api/weather', require('./routes/weather'));
api.use('/api/categories', require('./routes/category')); api.use('/api/categories', require('./routes/category'));
api.use('/api/bookmarks', require('./routes/bookmark'));
// Custom error handler // Custom error handler
api.use(errorHandler); api.use(errorHandler);

79
controllers/bookmark.js Normal file
View File

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

View File

@ -19,10 +19,12 @@ exports.createCategory = asyncWrapper(async (req, res, next) => {
// @route GET /api/categories // @route GET /api/categories
// @access Public // @access Public
exports.getCategories = asyncWrapper(async (req, res, next) => { exports.getCategories = asyncWrapper(async (req, res, next) => {
// const categories = await Category.findAll({ const categories = await Category.findAll({
// include: Bookmark include: [{
// }); model: Bookmark,
const categories = await Category.findAll(); as: 'bookmarks'
}]
});
res.status(200).json({ res.status(200).json({
success: true, success: true,
@ -35,7 +37,11 @@ exports.getCategories = asyncWrapper(async (req, res, next) => {
// @access Public // @access Public
exports.getCategory = asyncWrapper(async (req, res, next) => { exports.getCategory = asyncWrapper(async (req, res, next) => {
const category = await Category.findOne({ const category = await Category.findOne({
where: { id: req.params.id } where: { id: req.params.id },
include: [{
model: Bookmark,
as: 'bookmarks'
}]
}); });
if (!category) { if (!category) {

View File

@ -19,6 +19,8 @@ const App = sequelize.define('App', {
type: DataTypes.BOOLEAN, type: DataTypes.BOOLEAN,
defaultValue: false defaultValue: false
} }
}, {
tableName: 'apps'
}); });
module.exports = App; module.exports = App;

View File

@ -20,7 +20,7 @@ const Config = sequelize.define('Config', {
defaultValue: false defaultValue: false
} }
}, { }, {
freezeTableName: true tableName: 'config'
}); });
module.exports = Config; module.exports = Config;

View File

@ -9,7 +9,7 @@ const Weather = sequelize.define('Weather', {
conditionText: DataTypes.TEXT, conditionText: DataTypes.TEXT,
conditionCode: DataTypes.INTEGER conditionCode: DataTypes.INTEGER
}, { }, {
freezeTableName: true tableName: 'weather'
}); });
module.exports = Weather; module.exports = Weather;

23
routes/bookmark.js Normal file
View File

@ -0,0 +1,23 @@
const express = require('express');
const router = express.Router();
const {
createBookmark,
getBookmarks,
getBookmark,
updateBookmark,
deleteBookmark
} = require('../controllers/bookmark');
router
.route('/')
.post(createBookmark)
.get(getBookmarks);
router
.route('/:id')
.get(getBookmark)
.put(updateBookmark)
.delete(deleteBookmark);
module.exports = router;