Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/controllers/collectionController.js
Original file line number Diff line number Diff line change
@@ -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,
};
22 changes: 22 additions & 0 deletions src/migrations/20240628225040-create-collection.js
Original file line number Diff line number Diff line change
@@ -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");
}
};
22 changes: 22 additions & 0 deletions src/migrations/20240628230028-update-product.js
Original file line number Diff line number Diff line change
@@ -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');
*/
}
};
22 changes: 22 additions & 0 deletions src/models/collectionModel.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ 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 });
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,
Product,
Customer,
Order,
OrderItem,
Collection,
}
10 changes: 10 additions & 0 deletions src/routes/collectionRoutes.js
Original file line number Diff line number Diff line change
@@ -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;