diff --git a/api.js b/api.js index 9cd9653..6068aaf 100644 --- a/api.js +++ b/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); diff --git a/controllers/bookmark.js b/controllers/bookmark.js new file mode 100644 index 0000000..5a76c40 --- /dev/null +++ b/controllers/bookmark.js @@ -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: {} + }) +}) \ No newline at end of file diff --git a/controllers/category.js b/controllers/category.js index 9af2273..0b47120 100644 --- a/controllers/category.js +++ b/controllers/category.js @@ -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) { diff --git a/models/App.js b/models/App.js index 41c4605..8b7a5a3 100644 --- a/models/App.js +++ b/models/App.js @@ -19,6 +19,8 @@ const App = sequelize.define('App', { type: DataTypes.BOOLEAN, defaultValue: false } +}, { + tableName: 'apps' }); module.exports = App; \ No newline at end of file diff --git a/models/Config.js b/models/Config.js index 4578b86..3919f3f 100644 --- a/models/Config.js +++ b/models/Config.js @@ -20,7 +20,7 @@ const Config = sequelize.define('Config', { defaultValue: false } }, { - freezeTableName: true + tableName: 'config' }); module.exports = Config; \ No newline at end of file diff --git a/models/Weather.js b/models/Weather.js index 5611109..d79add5 100644 --- a/models/Weather.js +++ b/models/Weather.js @@ -9,7 +9,7 @@ const Weather = sequelize.define('Weather', { conditionText: DataTypes.TEXT, conditionCode: DataTypes.INTEGER }, { - freezeTableName: true + tableName: 'weather' }); module.exports = Weather; \ No newline at end of file diff --git a/routes/bookmark.js b/routes/bookmark.js new file mode 100644 index 0000000..f0d62f4 --- /dev/null +++ b/routes/bookmark.js @@ -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; \ No newline at end of file