From 100f274d96608cd6dbfb9c3d373532eb31e5c042 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 22 May 2021 19:04:34 +0200 Subject: [PATCH] Category model and controllers --- api.js | 1 + controllers/category.js | 83 ++++++++++++++++++++++++++++++++++++++ middleware/errorHandler.js | 8 ++-- models/Bookmark.js | 21 ++++++++++ models/Category.js | 17 ++++++++ models/associateModels.js | 13 ++++++ routes/category.js | 23 +++++++++++ server.js | 2 + 8 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 controllers/category.js create mode 100644 models/Bookmark.js create mode 100644 models/Category.js create mode 100644 models/associateModels.js create mode 100644 routes/category.js diff --git a/api.js b/api.js index 31151da..9cd9653 100644 --- a/api.js +++ b/api.js @@ -14,6 +14,7 @@ api.use(express.json()); 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')); // Custom error handler api.use(errorHandler); diff --git a/controllers/category.js b/controllers/category.js new file mode 100644 index 0000000..9af2273 --- /dev/null +++ b/controllers/category.js @@ -0,0 +1,83 @@ +const asyncWrapper = require('../middleware/asyncWrapper'); +const ErrorResponse = require('../utils/ErrorResponse'); +const Category = require('../models/Category'); +const Bookmark = require('../models/Bookmark'); + +// @desc Create new category +// @route POST /api/categories +// @access Public +exports.createCategory = asyncWrapper(async (req, res, next) => { + const category = await Category.create(req.body); + + res.status(201).json({ + success: true, + data: category + }) +}) + +// @desc Get all categories +// @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(); + + res.status(200).json({ + success: true, + data: categories + }) +}) + +// @desc Get single category +// @route GET /api/categories/:id +// @access Public +exports.getCategory = asyncWrapper(async (req, res, next) => { + const category = await Category.findOne({ + where: { id: req.params.id } + }); + + if (!category) { + return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404)) + } + + res.status(200).json({ + success: true, + data: category + }) +}) + +// @desc Update category +// @route PUT /api/categories/:id +// @access Public +exports.updateCategory = asyncWrapper(async (req, res, next) => { + let category = await Category.findOne({ + where: { id: req.params.id } + }); + + if (!category) { + return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404)) + } + + category = await category.update({ ...req.body }); + + res.status(200).json({ + success: true, + data: category + }) +}) + +// @desc Delete category +// @route DELETE /api/categories/:id +// @access Public +exports.deleteCategory = asyncWrapper(async (req, res, next) => { + await Category.destroy({ + where: { id: req.params.id } + }) + + res.status(200).json({ + success: true, + data: {} + }) +}) \ No newline at end of file diff --git a/middleware/errorHandler.js b/middleware/errorHandler.js index b4af510..582dace 100644 --- a/middleware/errorHandler.js +++ b/middleware/errorHandler.js @@ -5,10 +5,10 @@ const errorHandler = (err, req, res, next) => { let error = { ...err }; error.message = err.message; - if (error.errors[0].type === 'unique violation') { - const msg = error.errors[0].message; - error = new ErrorResponse(`Field ${msg}`, 400); - } + // if (error.errors[0].type === 'unique violation') { + // const msg = error.errors[0].message; + // error = new ErrorResponse(`Field ${msg}`, 400); + // } console.log(error); console.log(`${err}`.bgRed); diff --git a/models/Bookmark.js b/models/Bookmark.js new file mode 100644 index 0000000..bc21808 --- /dev/null +++ b/models/Bookmark.js @@ -0,0 +1,21 @@ +const { DataTypes } = require('sequelize'); +const { sequelize } = require('../db'); + +const Bookmark = sequelize.define('Bookmark', { + name: { + type: DataTypes.STRING, + allowNull: false + }, + url: { + type: DataTypes.STRING, + allowNull: false + }, + categoryId: { + type: DataTypes.INTEGER, + allowNull: false + } +}, { + tableName: 'bookmarks' +}); + +module.exports = Bookmark; \ No newline at end of file diff --git a/models/Category.js b/models/Category.js new file mode 100644 index 0000000..5f82633 --- /dev/null +++ b/models/Category.js @@ -0,0 +1,17 @@ +const { DataTypes } = require('sequelize'); +const { sequelize } = require('../db'); + +const Category = sequelize.define('Category', { + name: { + type: DataTypes.STRING, + allowNull: false + }, + isPinned: { + type: DataTypes.BOOLEAN, + defaultValue: false + } +}, { + tableName: 'categories' +}); + +module.exports = Category; \ No newline at end of file diff --git a/models/associateModels.js b/models/associateModels.js new file mode 100644 index 0000000..4ca1fab --- /dev/null +++ b/models/associateModels.js @@ -0,0 +1,13 @@ +const Category = require('./Category'); +const Bookmark = require('./Bookmark'); + +const associateModels = () => { + // Category <> Bookmark + Bookmark.belongsTo(Category, { foreignKey: 'categoryId' }); + Category.hasMany(Bookmark, { + as: 'bookmarks', + foreignKey: 'categoryId' + }); +} + +module.exports = associateModels; \ No newline at end of file diff --git a/routes/category.js b/routes/category.js new file mode 100644 index 0000000..b18b8f6 --- /dev/null +++ b/routes/category.js @@ -0,0 +1,23 @@ +const express = require('express'); +const router = express.Router(); + +const { + createCategory, + getCategories, + getCategory, + updateCategory, + deleteCategory +} = require('../controllers/category'); + +router + .route('/') + .post(createCategory) + .get(getCategories); + +router + .route('/:id') + .get(getCategory) + .put(updateCategory) + .delete(deleteCategory); + +module.exports = router; \ No newline at end of file diff --git a/server.js b/server.js index a502846..6cd01d6 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,13 @@ const api = require('./api'); const jobs = require('./utils/jobs'); const Socket = require('./Socket'); const Sockets = require('./Sockets'); +const associateModels = require('./models/associateModels'); require('dotenv').config(); const PORT = process.env.PORT || 5005; connectDB(); +associateModels(); // Create server for Express API and WebSockets const server = http.createServer();