diff --git a/src/controllers/collectionController.js b/src/controllers/collectionController.js new file mode 100644 index 0000000..8705760 --- /dev/null +++ b/src/controllers/collectionController.js @@ -0,0 +1,30 @@ +const httpStatus = require("http-status"); +const models = require("../models"); +const Collection = models.Collection + +async function getAllCollection(req, res) { + try { + const collections = await Collection.findAll(); + return res.status(httpStatus.OK).json(collections); + } catch (error) { + return res + .status(httpStatus.INTERNAL_SERVER_ERROR) + .json({ error: error.message }); + } +} + +async function createCollection(req, res) { + try { + const collection = await Collection.create(req.body); + return res.status(httpStatus.CREATED).json(collection); + } catch (error) { + return res + .status(httpStatus.INTERNAL_SERVER_ERROR) + .json({ error: error.message }); + } +} + +module.exports = { + getAllCollection, + createCollection, +}; diff --git a/src/migrations/20240628225040-create-collection.js b/src/migrations/20240628225040-create-collection.js new file mode 100644 index 0000000..2684c6e --- /dev/null +++ b/src/migrations/20240628225040-create-collection.js @@ -0,0 +1,22 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable("collections", { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER, + }, + name: { + type: Sequelize.STRING, + }, + }); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.dropTable("collections"); + } +}; diff --git a/src/migrations/20240628230028-update-product.js b/src/migrations/20240628230028-update-product.js new file mode 100644 index 0000000..b6e01de --- /dev/null +++ b/src/migrations/20240628230028-update-product.js @@ -0,0 +1,22 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up (queryInterface, Sequelize) { + /** + * Add altering commands here. + * + * Example: + * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); + */ + }, + + async down (queryInterface, Sequelize) { + /** + * Add reverting commands here. + * + * Example: + * await queryInterface.dropTable('users'); + */ + } +}; diff --git a/src/models/collectionModel.js b/src/models/collectionModel.js new file mode 100644 index 0000000..1b7782f --- /dev/null +++ b/src/models/collectionModel.js @@ -0,0 +1,22 @@ +const { DataTypes } = require("sequelize"); +const database = require("../config/database"); + +const Collection = database.sequelize.define( + "Collection", + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true, + allowNull: false, + }, + name: DataTypes.STRING, + description: DataTypes.STRING, + }, + { + timestamps: false, + tableName: "collections", + } +); + +module.exports = Collection; diff --git a/src/models/index.js b/src/models/index.js index 11a7a29..569772c 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -3,6 +3,7 @@ const Product = require('./productModel') const Customer = require('./customerModel') const Order = require('./orderModel') const OrderItem = require('./orderItemModel') +const Collection = require('./collectionModel') Category.hasMany(Product, { foreignKey: "categoryId", allowNull: true }); Product.belongsTo(Category, { foreignKey: "categoryId", allowNull: true }); @@ -10,6 +11,7 @@ Order.belongsTo(Customer, { foreignKey: "customer_id" }); Order.hasMany(OrderItem, { foreignKey: "order_id" }); OrderItem.belongsTo(Order, { foreignKey: "order_id" }); OrderItem.belongsTo(Product, { foreignKey: "product_id" }); +Product.belongsTo(Collection, { foreignKey: "collectionId", allowNull: true }); module.exports = { Category, @@ -17,4 +19,5 @@ module.exports = { Customer, Order, OrderItem, + Collection, } \ No newline at end of file diff --git a/src/routes/collectionRoutes.js b/src/routes/collectionRoutes.js new file mode 100644 index 0000000..24763eb --- /dev/null +++ b/src/routes/collectionRoutes.js @@ -0,0 +1,10 @@ +const express = require("express"); +const collectionController = require("../controllers/collectionController"); + +const router = express.Router(); + +// Rota para obter todos os itens +router.get("/", collectionController.getAllCollection); +router.post("/", collectionController.createCollection); + +module.exports = router;