mirror of
https://github.com/pawelmalak/flame.git
synced 2024-12-18 23:41:39 +03:00
Bookmarks controller
This commit is contained in:
parent
100f274d96
commit
27250dc850
1
api.js
1
api.js
@ -15,6 +15,7 @@ api.use('/api/apps', require('./routes/apps'));
|
||||
api.use('/api/config', require('./routes/config'));
|
||||
api.use('/api/weather', require('./routes/weather'));
|
||||
api.use('/api/categories', require('./routes/category'));
|
||||
api.use('/api/bookmarks', require('./routes/bookmark'));
|
||||
|
||||
// Custom error handler
|
||||
api.use(errorHandler);
|
||||
|
79
controllers/bookmark.js
Normal file
79
controllers/bookmark.js
Normal 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: {}
|
||||
})
|
||||
})
|
@ -19,10 +19,12 @@ exports.createCategory = asyncWrapper(async (req, res, next) => {
|
||||
// @route GET /api/categories
|
||||
// @access Public
|
||||
exports.getCategories = asyncWrapper(async (req, res, next) => {
|
||||
// const categories = await Category.findAll({
|
||||
// include: Bookmark
|
||||
// });
|
||||
const categories = await Category.findAll();
|
||||
const categories = await Category.findAll({
|
||||
include: [{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks'
|
||||
}]
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
@ -35,7 +37,11 @@ exports.getCategories = asyncWrapper(async (req, res, next) => {
|
||||
// @access Public
|
||||
exports.getCategory = asyncWrapper(async (req, res, next) => {
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id }
|
||||
where: { id: req.params.id },
|
||||
include: [{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks'
|
||||
}]
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
|
@ -19,6 +19,8 @@ const App = sequelize.define('App', {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'apps'
|
||||
});
|
||||
|
||||
module.exports = App;
|
@ -20,7 +20,7 @@ const Config = sequelize.define('Config', {
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true
|
||||
tableName: 'config'
|
||||
});
|
||||
|
||||
module.exports = Config;
|
@ -9,7 +9,7 @@ const Weather = sequelize.define('Weather', {
|
||||
conditionText: DataTypes.TEXT,
|
||||
conditionCode: DataTypes.INTEGER
|
||||
}, {
|
||||
freezeTableName: true
|
||||
tableName: 'weather'
|
||||
});
|
||||
|
||||
module.exports = Weather;
|
23
routes/bookmark.js
Normal file
23
routes/bookmark.js
Normal 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;
|
Loading…
Reference in New Issue
Block a user