Skip to content

Commit

Permalink
Bookmarks controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelmalak committed May 23, 2021
1 parent 100f274 commit 27250dc
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 7 deletions.
1 change: 1 addition & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
79 changes: 79 additions & 0 deletions controllers/bookmark.js
Original file line number Diff line number Diff line change
@@ -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: {}
})
})
16 changes: 11 additions & 5 deletions controllers/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions models/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const App = sequelize.define('App', {
type: DataTypes.BOOLEAN,
defaultValue: false
}
}, {
tableName: 'apps'
});

module.exports = App;
2 changes: 1 addition & 1 deletion models/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Config = sequelize.define('Config', {
defaultValue: false
}
}, {
freezeTableName: true
tableName: 'config'
});

module.exports = Config;
2 changes: 1 addition & 1 deletion models/Weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Weather = sequelize.define('Weather', {
conditionText: DataTypes.TEXT,
conditionCode: DataTypes.INTEGER
}, {
freezeTableName: true
tableName: 'weather'
});

module.exports = Weather;
23 changes: 23 additions & 0 deletions routes/bookmark.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 27250dc

Please sign in to comment.