From 167493f1b8bac1c581f00a41a5243096cac64f43 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Wed, 11 Jan 2023 10:28:37 -0300 Subject: [PATCH 01/17] initial commit: template and connect to db --- package.json | 31 +++++++++++++++++++++++++++++++ scripts/app.js | 10 ++++++++++ scripts/config/db-connect.js | 5 +++++ server.js | 7 +++++++ 4 files changed, 53 insertions(+) create mode 100644 package.json create mode 100644 scripts/app.js create mode 100644 scripts/config/db-connect.js create mode 100644 server.js diff --git a/package.json b/package.json new file mode 100644 index 00000000..6a040d55 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "test-backend-nodejs", + "version": "1.0.0", + "description": "

Backend Analyst Candidate Testing

", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "nodemon server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Lebackrobot/test-backend-nodejs.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Lebackrobot/test-backend-nodejs/issues" + }, + "homepage": "https://github.com/Lebackrobot/test-backend-nodejs#readme", + "dependencies": { + "chai": "^4.3.7", + "express": "^4.18.2", + "mocha": "^10.2.0", + "mongoose": "^6.8.3" + }, + "devDependencies": { + "nodemon": "^2.0.20" + } +} diff --git a/scripts/app.js b/scripts/app.js new file mode 100644 index 00000000..820d33d5 --- /dev/null +++ b/scripts/app.js @@ -0,0 +1,10 @@ +import express from 'express' +import db from './config/db-connect.js' + +db.on('error', err => {console.log(`Connection error ${err}`)}) +db.once('open', () => console.log('Success to connection on db')) + +const app = express() +app.use(express.json()) + +export default app \ No newline at end of file diff --git a/scripts/config/db-connect.js b/scripts/config/db-connect.js new file mode 100644 index 00000000..46b07113 --- /dev/null +++ b/scripts/config/db-connect.js @@ -0,0 +1,5 @@ +import mongoose from 'mongoose' + +mongoose.connect('mongodb+srv://admin:admin123@cluster.ovjsowh.mongodb.net/test-backend-nodejs') + +export default mongoose.connection \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 00000000..fdbfd3c3 --- /dev/null +++ b/server.js @@ -0,0 +1,7 @@ +import app from './scripts/app.js' + +const port = process.env.PORT || 3000 + +app.listen(port, () => { + console.log(`Server listen on port ${port}`) +}) \ No newline at end of file From b64f85d33b774961e45b4b3a3b89e9749d15a2e0 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Wed, 11 Jan 2023 11:37:12 -0300 Subject: [PATCH 02/17] feat: product route --- scripts/app.js | 4 ++++ scripts/controllers/product-controller.js | 19 +++++++++++++++++++ scripts/models/product-model.js | 10 ++++++++++ scripts/routes/index.js | 9 +++++++++ scripts/routes/product-routes.js | 6 ++++++ 5 files changed, 48 insertions(+) create mode 100644 scripts/controllers/product-controller.js create mode 100644 scripts/models/product-model.js create mode 100644 scripts/routes/index.js create mode 100644 scripts/routes/product-routes.js diff --git a/scripts/app.js b/scripts/app.js index 820d33d5..cbee26ba 100644 --- a/scripts/app.js +++ b/scripts/app.js @@ -1,10 +1,14 @@ import express from 'express' import db from './config/db-connect.js' +import { routes } from './routes/index.js' + db.on('error', err => {console.log(`Connection error ${err}`)}) db.once('open', () => console.log('Success to connection on db')) const app = express() app.use(express.json()) +routes(app) + export default app \ No newline at end of file diff --git a/scripts/controllers/product-controller.js b/scripts/controllers/product-controller.js new file mode 100644 index 00000000..cc7f37f1 --- /dev/null +++ b/scripts/controllers/product-controller.js @@ -0,0 +1,19 @@ +import Product from './../models/product-model' + +class ProductController { + static getProducts = (require, response) => { + if(require.query.price) { + Product.find({ price: { $lte: price }}, (err, products) => { + response.status(200).send(products) + }) + } + + else { + Product.find((err, products) => { + response.status(200).send(products) + }) + } + } +} + +export default ProductController \ No newline at end of file diff --git a/scripts/models/product-model.js b/scripts/models/product-model.js new file mode 100644 index 00000000..09e0faae --- /dev/null +++ b/scripts/models/product-model.js @@ -0,0 +1,10 @@ +import mongoose from 'mongoose' + +const productSchema = new mongoose.Schema({ + id: {type: String, required: false}, + title: {type: String, required: true}, + description: {type: String, required: true}, + price: {type: Number, required: true}, +}) + +export default mongoose.model('products', productSchema) \ No newline at end of file diff --git a/scripts/routes/index.js b/scripts/routes/index.js new file mode 100644 index 00000000..d6f459b2 --- /dev/null +++ b/scripts/routes/index.js @@ -0,0 +1,9 @@ +import express from 'express' + +const routes = app => { + app.route('/').get((req, res) => { + res.status(200).send({message: 'Lets bora!'}) + }) +} + +export { routes } \ No newline at end of file diff --git a/scripts/routes/product-routes.js b/scripts/routes/product-routes.js new file mode 100644 index 00000000..700083b2 --- /dev/null +++ b/scripts/routes/product-routes.js @@ -0,0 +1,6 @@ +import express from 'express' +import ProductController from './../controllers/product-controller.js' + +const productRouter = express.Router() + .get('/products', ProductController.getProducts) + From 089900bb27a4cb7d62782d476c57fab51adcb768 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Wed, 11 Jan 2023 13:49:45 -0300 Subject: [PATCH 03/17] feat: product CRUD --- scripts/controllers/product-controller.js | 84 ++++++++++++++++++++--- scripts/routes/index.js | 3 + scripts/routes/product-router.js | 18 +++++ scripts/routes/product-routes.js | 6 -- 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 scripts/routes/product-router.js delete mode 100644 scripts/routes/product-routes.js diff --git a/scripts/controllers/product-controller.js b/scripts/controllers/product-controller.js index cc7f37f1..a6375aea 100644 --- a/scripts/controllers/product-controller.js +++ b/scripts/controllers/product-controller.js @@ -1,18 +1,86 @@ -import Product from './../models/product-model' +import Product from './../models/product-model.js' class ProductController { + + static createProduct = (require, response) => { + const product = new Product(require.body) + + product.save( err => { + if(!err) { + response.status(201).send('Successfully create product') + } + + else { + response.status(500).send({message: err.message}) + } + }) + } + static getProducts = (require, response) => { - if(require.query.price) { - Product.find({ price: { $lte: price }}, (err, products) => { - response.status(200).send(products) + if (require.query.price) { + Product.find({ price: { $lte: price } }, (err, products) => { + if(!err) { + response.status(200).send(products) + } + + else { + response.status(500).send({message: err.message}) + } }) } - - else { - Product.find((err, products) => { + Product.find((err, products) => { + if(!err) { response.status(200).send(products) + } + + else { + response.status(400).send({message: err.message}) + } + }) + } + + static getProduct = (require, response) => { + const id = require.body.id + + Product.findById((err, products)=> { + if(!err) { + response.status(200).send(products) + } + + else { + response.status(400).send({message: err.message}) + } }) - } + + } + + static updateProduct = (require, response) => { + const id = require.body.id + + Product.findByIdAndUpdate(id, {$set: require.body}, err => { + if(!err) { + response.status(200).send({message: 'Successfully update product'}) + } + + else { + response.status(500).send({message: err.message}) + } + }) + } + + static deleteProduct = (require, response) => { + const id = req.params.id + + Product.findByIdAndDelete(id, err => { + + if(!err) { + response.status(500).send({message: err.message}) + } + + else { + response.status(200).send({messagea: 'Successfully delete product'}) + } + }) } } diff --git a/scripts/routes/index.js b/scripts/routes/index.js index d6f459b2..688589d6 100644 --- a/scripts/routes/index.js +++ b/scripts/routes/index.js @@ -1,9 +1,12 @@ import express from 'express' +import productRouter from './product-router.js' const routes = app => { app.route('/').get((req, res) => { res.status(200).send({message: 'Lets bora!'}) }) + + app.use(express.json(), productRouter) } export { routes } \ No newline at end of file diff --git a/scripts/routes/product-router.js b/scripts/routes/product-router.js new file mode 100644 index 00000000..aa22d3d9 --- /dev/null +++ b/scripts/routes/product-router.js @@ -0,0 +1,18 @@ +import express from 'express' +import ProductController from '../controllers/product-controller.js' + +const productRouter = express.Router() + +productRouter + .post('/products', ProductController.createProduct) + + .get('/products', ProductController.getProducts) + .get('/products/:price', ProductController.getProducts) + .get('/products/:id', ProductController.getProduct) + + .put('/products/:id', ProductController.updateProduct) + + .delete('/products/:id', ProductController.deleteProduct) + + +export default productRouter \ No newline at end of file diff --git a/scripts/routes/product-routes.js b/scripts/routes/product-routes.js deleted file mode 100644 index 700083b2..00000000 --- a/scripts/routes/product-routes.js +++ /dev/null @@ -1,6 +0,0 @@ -import express from 'express' -import ProductController from './../controllers/product-controller.js' - -const productRouter = express.Router() - .get('/products', ProductController.getProducts) - From f3049c5b026479719ebbf6836a3ea64cc0909edf Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Wed, 11 Jan 2023 15:34:39 -0300 Subject: [PATCH 04/17] feat: category CRUD --- scripts/controllers/category-controller.js | 76 ++++++++++++++++++++++ scripts/controllers/product-controller.js | 36 +++++----- scripts/models/category-model.js | 8 +++ scripts/models/product-model.js | 3 +- scripts/routes/category-router.js | 13 ++++ scripts/routes/index.js | 6 +- scripts/routes/product-router.js | 5 +- 7 files changed, 126 insertions(+), 21 deletions(-) create mode 100644 scripts/controllers/category-controller.js create mode 100644 scripts/models/category-model.js create mode 100644 scripts/routes/category-router.js diff --git a/scripts/controllers/category-controller.js b/scripts/controllers/category-controller.js new file mode 100644 index 00000000..78ac6ff9 --- /dev/null +++ b/scripts/controllers/category-controller.js @@ -0,0 +1,76 @@ +import Category from './../models/category-model.js' + +class CategoryController { + + static createCategory = (require, response) => { + const category = new Category(require.body) + + category.save(err => { + if (!err) { + response.status(201).send('Successfully create category') + } + + else { + response.status(500).send({ message: err.message }) + } + }) + } + + static getCategories = (require, response) => { + Category.find((err, categories) => { + if (!err) { + response.status(200).send(categories) + } + + else { + response.status(400).send({ message: err.message }) + } + }) + } + + static getCategory = (require, response) => { + const id = require.params.id + + Category.findById(id, (err, category) => { + if (!err) { + response.status(200).send(category) + } + + else { + response.status(400).send({ message: err.message }) + } + }) + + } + + static updateCategory = (require, response) => { + const id = require.params.id + + Category.findByIdAndUpdate(id, { $set: require.body }, err => { + if (!err) { + response.status(200).send({ message: 'Successfully update category' }) + } + + else { + response.status(500).send({ message: err.message }) + } + }) + } + + static deleteCategory = (require, response) => { + const id = require.params.id + + Category.findByIdAndDelete(id, err => { + + if (!err) { + response.status(200).send({ message: 'Successfully delete category' }) + } + + else { + response.status(500).send({ messagea: err.message }) + } + }) + } +} + +export default CategoryController \ No newline at end of file diff --git a/scripts/controllers/product-controller.js b/scripts/controllers/product-controller.js index a6375aea..6adf7d6d 100644 --- a/scripts/controllers/product-controller.js +++ b/scripts/controllers/product-controller.js @@ -17,34 +17,40 @@ class ProductController { } static getProducts = (require, response) => { - if (require.query.price) { - Product.find({ price: { $lte: price } }, (err, products) => { + Product.find() + .populate('category') + .exec((err, products) => { if(!err) { response.status(200).send(products) } else { - response.status(500).send({message: err.message}) + response.status(400).send({message: err.message}) } }) - } - Product.find((err, products) => { - if(!err) { + } + + static searchProductsByCategory = (require, response) => { + const categoryTitle = require.query.categoryTitle + + + Product.find({ 'category.title': categoryTitle}, (err, products) => { + if (!err) { response.status(200).send(products) } else { - response.status(400).send({message: err.message}) + response.status(400).send({ message: err.message }) } - }) + }) } static getProduct = (require, response) => { - const id = require.body.id + const id = require.params.id - Product.findById((err, products)=> { + Product.findById(id, (err, product)=> { if(!err) { - response.status(200).send(products) + response.status(200).send(product) } else { @@ -55,7 +61,7 @@ class ProductController { } static updateProduct = (require, response) => { - const id = require.body.id + const id = require.params.id Product.findByIdAndUpdate(id, {$set: require.body}, err => { if(!err) { @@ -69,16 +75,16 @@ class ProductController { } static deleteProduct = (require, response) => { - const id = req.params.id + const id = require.params.id Product.findByIdAndDelete(id, err => { if(!err) { - response.status(500).send({message: err.message}) + response.status(200).send({ message: 'Successfully delete product' }) } else { - response.status(200).send({messagea: 'Successfully delete product'}) + response.status(500).send({messagea: err.message}) } }) } diff --git a/scripts/models/category-model.js b/scripts/models/category-model.js new file mode 100644 index 00000000..4e2cde0c --- /dev/null +++ b/scripts/models/category-model.js @@ -0,0 +1,8 @@ +import mongoose from 'mongoose' + +const categorySchema = new mongoose.Schema({ + id: {type: String, required: false}, + title: {type: String, require: true} +}) + +export default mongoose.model('category', categorySchema) \ No newline at end of file diff --git a/scripts/models/product-model.js b/scripts/models/product-model.js index 09e0faae..3e6107ff 100644 --- a/scripts/models/product-model.js +++ b/scripts/models/product-model.js @@ -2,9 +2,10 @@ import mongoose from 'mongoose' const productSchema = new mongoose.Schema({ id: {type: String, required: false}, + category: {type: mongoose.Schema.Types.ObjectId, ref: 'category', required: true}, title: {type: String, required: true}, description: {type: String, required: true}, - price: {type: Number, required: true}, + price: {type: Number, required: true} }) export default mongoose.model('products', productSchema) \ No newline at end of file diff --git a/scripts/routes/category-router.js b/scripts/routes/category-router.js new file mode 100644 index 00000000..6c3acbf3 --- /dev/null +++ b/scripts/routes/category-router.js @@ -0,0 +1,13 @@ +import express from 'express' +import CategoryController from '../controllers/category-controller.js' + +const CatregoryController = express.Router() + +CatregoryController + .post('/categories', CategoryController.createCategory) + .get('/categories', CategoryController.getCategories) + .get('/categories/:id', CategoryController.getCategory) + .put('/categories/:id', CategoryController.updateCategory) + .delete('/categories/:id', CategoryController.deleteCategory) + +export default CatregoryController \ No newline at end of file diff --git a/scripts/routes/index.js b/scripts/routes/index.js index 688589d6..f4780b78 100644 --- a/scripts/routes/index.js +++ b/scripts/routes/index.js @@ -1,12 +1,16 @@ import express from 'express' import productRouter from './product-router.js' +import categoryRouter from './category-router.js' const routes = app => { app.route('/').get((req, res) => { res.status(200).send({message: 'Lets bora!'}) }) - app.use(express.json(), productRouter) + app + .use(express.json()) + .use(productRouter) + .use(categoryRouter) } export { routes } \ No newline at end of file diff --git a/scripts/routes/product-router.js b/scripts/routes/product-router.js index aa22d3d9..e6524e39 100644 --- a/scripts/routes/product-router.js +++ b/scripts/routes/product-router.js @@ -5,14 +5,11 @@ const productRouter = express.Router() productRouter .post('/products', ProductController.createProduct) - .get('/products', ProductController.getProducts) - .get('/products/:price', ProductController.getProducts) .get('/products/:id', ProductController.getProduct) - .put('/products/:id', ProductController.updateProduct) - .delete('/products/:id', ProductController.deleteProduct) + .get('/products/search:category', ProductController.searchProductsByCategory) export default productRouter \ No newline at end of file From 400ce5eaf317b2f6c48bbac708114ba69dc8276c Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Wed, 11 Jan 2023 17:57:35 -0300 Subject: [PATCH 05/17] fix: tratamento de consultas por query --- scripts/controllers/product-controller.js | 17 +++++++++++++++-- scripts/routes/product-router.js | 3 ++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/scripts/controllers/product-controller.js b/scripts/controllers/product-controller.js index 6adf7d6d..376c30f6 100644 --- a/scripts/controllers/product-controller.js +++ b/scripts/controllers/product-controller.js @@ -31,10 +31,23 @@ class ProductController { } static searchProductsByCategory = (require, response) => { - const categoryTitle = require.query.categoryTitle + const category = require.params.id + Product.find({ 'category': category }, (err, products) => { + if (!err) { + response.status(200).send(products) + } + + else { + response.status(400).send({ message: err.message }) + } + }) + } + + static searchProductByTitle = (require, response) => { + const title = require.query.title - Product.find({ 'category.title': categoryTitle}, (err, products) => { + Product.find({ 'title': title}, (err, products) => { if (!err) { response.status(200).send(products) } diff --git a/scripts/routes/product-router.js b/scripts/routes/product-router.js index e6524e39..f9f09b16 100644 --- a/scripts/routes/product-router.js +++ b/scripts/routes/product-router.js @@ -4,12 +4,13 @@ import ProductController from '../controllers/product-controller.js' const productRouter = express.Router() productRouter + .get('/products/search', ProductController.searchProductByTitle) + .get('/products/category/:id', ProductController.searchProductsByCategory) .post('/products', ProductController.createProduct) .get('/products', ProductController.getProducts) .get('/products/:id', ProductController.getProduct) .put('/products/:id', ProductController.updateProduct) .delete('/products/:id', ProductController.deleteProduct) - .get('/products/search:category', ProductController.searchProductsByCategory) export default productRouter \ No newline at end of file From b07446babc68af52d6c19b2d29cdc9b5ccca8139 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes <49316490+Lebackrobot@users.noreply.github.com> Date: Wed, 11 Jan 2023 18:45:54 -0300 Subject: [PATCH 06/17] Update README.md --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index f50b9c82..5e843c68 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,47 @@ Your challenge is to develop an API, using Node.JS, for a product catalog manage - Code organization, module separation, readability and comments. - Commit history. - The use of MongoDB is a differentiator + +

Code Documentation

+Hello, dear. Below is the usage documentation. + +After downloading the project,it is necessary to install all project dependencies with npm + +```console +anotaai@pc:~$ npm install +``` +Now it is possible to run up the server with the command + +```console +anotaai@pc:~$ npm run dev +``` + +

About API

+Here is some technical information about modeling the problem + +

Schemas

+two schemes were models: + +- Products: mproducts has categories reference +```javascript + id: {type: String, required: false}, + category: {type: mongoose.Schema.Types.ObjectId, ref: 'category', required: true}, + title: {type: String, required: true}, + description: {type: String, required: true}, + price: {type: Number, required: true} +``` +- Categories: +```javascript +const categorySchema = new mongoose.Schema({ + id: {type: String, required: false}, + title: {type: String, require: true} +}) +``` + +

Routes

+ +Method | EndPoint | Returns +:---------: | :------ | :-------: +GET | /products | products: Array +GET | /products/category/:id | product: Object +POST| /product/category/ | message: Object From c401066b1841e26a0e91ac4749f3b833c45b0e00 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes <49316490+Lebackrobot@users.noreply.github.com> Date: Wed, 11 Jan 2023 18:45:54 -0300 Subject: [PATCH 07/17] WIP: class break --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index f50b9c82..5e843c68 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,47 @@ Your challenge is to develop an API, using Node.JS, for a product catalog manage - Code organization, module separation, readability and comments. - Commit history. - The use of MongoDB is a differentiator + +

Code Documentation

+Hello, dear. Below is the usage documentation. + +After downloading the project,it is necessary to install all project dependencies with npm + +```console +anotaai@pc:~$ npm install +``` +Now it is possible to run up the server with the command + +```console +anotaai@pc:~$ npm run dev +``` + +

About API

+Here is some technical information about modeling the problem + +

Schemas

+two schemes were models: + +- Products: mproducts has categories reference +```javascript + id: {type: String, required: false}, + category: {type: mongoose.Schema.Types.ObjectId, ref: 'category', required: true}, + title: {type: String, required: true}, + description: {type: String, required: true}, + price: {type: Number, required: true} +``` +- Categories: +```javascript +const categorySchema = new mongoose.Schema({ + id: {type: String, required: false}, + title: {type: String, require: true} +}) +``` + +

Routes

+ +Method | EndPoint | Returns +:---------: | :------ | :-------: +GET | /products | products: Array +GET | /products/category/:id | product: Object +POST| /product/category/ | message: Object From b6e7e4526ea56cfe07141024f5b79e8b3859696d Mon Sep 17 00:00:00 2001 From: Pedro Fernandes <49316490+Lebackrobot@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:46:18 -0300 Subject: [PATCH 08/17] Update README.md --- README.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5e843c68..a7d9621f 100644 --- a/README.md +++ b/README.md @@ -77,8 +77,24 @@ const categorySchema = new mongoose.Schema({

Routes

-Method | EndPoint | Returns -:---------: | :------ | :-------: -GET | /products | products: Array -GET | /products/category/:id | product: Object -POST| /product/category/ | message: Object +Product Routes: + +Method | EndPoint | Body Params |Returns +:---------: | :------ | :-------: | :--------: +POST| /products | product | messag : Object +PUT | /products/:id | products | message : Object +GET | /products | - |products: Array +GET | /product/:id | - |product: Object +GET | /products/category/:id | - | products: Array +GET| /product/search?title=queryParam | - | message: Object +DELETE | /products/:id | - | message: Object + +Categorie Routes: + +Method | EndPoint | Body Params |Returns +:---------: | :------ | :-------: | :--------: +POST| /categories | category | message: Object +PUT | /categories/:id | category | message: Object +GET | /categories | - |categories: Array +GET | /categories/:id | - |categorie: Object +DELETE | /categories/:id | - | message: Object From 664fe06731d9d766c4c9bd941bec45b6a9b56f19 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes <49316490+Lebackrobot@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:51:18 -0300 Subject: [PATCH 09/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a7d9621f..961f37f8 100644 --- a/README.md +++ b/README.md @@ -96,5 +96,5 @@ Method | EndPoint | Body Params |Returns POST| /categories | category | message: Object PUT | /categories/:id | category | message: Object GET | /categories | - |categories: Array -GET | /categories/:id | - |categorie: Object +GET | /categories/:id | - |category: Object DELETE | /categories/:id | - | message: Object From 81422475a816fd1a2e6641662f6a4a2953c41744 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Fri, 13 Jan 2023 12:45:56 -0300 Subject: [PATCH 10/17] =?UTF-8?q?refactor:=20refatora=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/controllers/category-controller.js | 77 ++++++++++------------ scripts/services/category-services.js | 25 +++++++ 2 files changed, 59 insertions(+), 43 deletions(-) create mode 100644 scripts/services/category-services.js diff --git a/scripts/controllers/category-controller.js b/scripts/controllers/category-controller.js index 78ac6ff9..8cd06c37 100644 --- a/scripts/controllers/category-controller.js +++ b/scripts/controllers/category-controller.js @@ -1,74 +1,65 @@ import Category from './../models/category-model.js' +import CategoryService from './../services/category-services.js' class CategoryController { static createCategory = (require, response) => { - const category = new Category(require.body) - category.save(err => { - if (!err) { - response.status(201).send('Successfully create category') - } + CategoryService.createCategory(require) + .then(success => { + response.status(201).send({message:'Successfully create category'}) + }) - else { - response.status(500).send({ message: err.message }) - } + .catch(err => { + response.status(500).send({ message: err.message}) }) } static getCategories = (require, response) => { - Category.find((err, categories) => { - if (!err) { - response.status(200).send(categories) - } - - else { - response.status(400).send({ message: err.message }) - } - }) + + CategoryService.getCategories() + .then(categories => { + response.status(200).send(categories) + }) + + .catch(err => { + response.status(400).send({ message: err.message }) + }) } static getCategory = (require, response) => { - const id = require.params.id - Category.findById(id, (err, category) => { - if (!err) { - response.status(200).send(category) - } + CategoryService.getCategory(require) + .then(category => { + response.status(200).send(category) + }) - else { - response.status(400).send({ message: err.message }) - } + .catch(err => { + response.status(400).send({ message: err.message }) }) } static updateCategory = (require, response) => { - const id = require.params.id - Category.findByIdAndUpdate(id, { $set: require.body }, err => { - if (!err) { - response.status(200).send({ message: 'Successfully update category' }) - } + CategoryService.updateCategory(require) + .then(success => { + response.status(200).send({ message: "Successfully delete category" }) + }) - else { - response.status(500).send({ message: err.message }) - } + .catch(err => { + response.status(500).send({message: err.message}) }) } static deleteCategory = (require, response) => { - const id = require.params.id - - Category.findByIdAndDelete(id, err => { - - if (!err) { - response.status(200).send({ message: 'Successfully delete category' }) - } + CategoryService.deleteCategory(require) + .then(success => { + response.status(200).send({message: "Successfully delete category"}) + }) - else { - response.status(500).send({ messagea: err.message }) - } + .catch(err => { + response.status(500).send({message: err.message}) }) } } diff --git a/scripts/services/category-services.js b/scripts/services/category-services.js new file mode 100644 index 00000000..d00451b7 --- /dev/null +++ b/scripts/services/category-services.js @@ -0,0 +1,25 @@ +import Category from './../models/category-model.js' + +class CategoryService { + static createCategory = require => { + return new Category(require.body).save() + } + + static getCategories = () => { + return Category.find().lean() + } + + static getCategory = require => { + return Category.findById(require.params.id).lean() + } + + static updateCategory = require => { + return Category.findByIdAndUpdate(require.params.id, {$set: require.body}) + } + + static deleteCategory = require => { + return Category.findByIdAndDelete(require.params.id) + } +} + +export default CategoryService \ No newline at end of file From 24901514dbc17ff37848fd921722061cbf1c9a32 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Wed, 18 Jan 2023 11:19:08 -0300 Subject: [PATCH 11/17] =?UTF-8?q?refactor:=20refatora=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/controllers/category-controller.js | 1 - scripts/controllers/product-controller.js | 105 +++++++++------------ scripts/routes/category-router.js | 6 +- scripts/routes/index.js | 3 +- scripts/services/product-service.js | 29 ++++++ 5 files changed, 76 insertions(+), 68 deletions(-) create mode 100644 scripts/services/product-service.js diff --git a/scripts/controllers/category-controller.js b/scripts/controllers/category-controller.js index 8cd06c37..b9def8c2 100644 --- a/scripts/controllers/category-controller.js +++ b/scripts/controllers/category-controller.js @@ -1,4 +1,3 @@ -import Category from './../models/category-model.js' import CategoryService from './../services/category-services.js' class CategoryController { diff --git a/scripts/controllers/product-controller.js b/scripts/controllers/product-controller.js index 376c30f6..208058ae 100644 --- a/scripts/controllers/product-controller.js +++ b/scripts/controllers/product-controller.js @@ -1,76 +1,61 @@ -import Product from './../models/product-model.js' +import ProductService from './../services/product-service.js' class ProductController { static createProduct = (require, response) => { - const product = new Product(require.body) - - product.save( err => { - if(!err) { - response.status(201).send('Successfully create product') - } + ProductService.createProduct(require) + .then(() => { + response.status(201).send({message: 'Successfully create product'}) + }) - else { - response.status(500).send({message: err.message}) - } - }) + .catch(err => { + response.status(500).send({message: err.message}) + }) } static getProducts = (require, response) => { - Product.find() - .populate('category') - .exec((err, products) => { - if(!err) { - response.status(200).send(products) - } - - else { - response.status(400).send({message: err.message}) - } - }) + ProductService.getProducts(require) + .then(products => { + response.status(200).send(products) + }) + + .catch(err => { + response.status(400).send({message: err.message}) + }) } static searchProductsByCategory = (require, response) => { - const category = require.params.id - - Product.find({ 'category': category }, (err, products) => { - if (!err) { - response.status(200).send(products) - } + ProductService.searchProductsByCategory(require) + .then(products => { + response.status(200).send(products) + }) - else { - response.status(400).send({ message: err.message }) - } + .catch(err => { + response.status(400).send({message: err.message}) }) } static searchProductByTitle = (require, response) => { - const title = require.query.title + ProductService.searchProductByTitle(require) + .then(product => { + response.status(200).send(product) + }) - Product.find({ 'title': title}, (err, products) => { - if (!err) { - response.status(200).send(products) - } - - else { - response.status(400).send({ message: err.message }) - } + .catch(err => { + response.status(400).send({message: err.message}) }) } static getProduct = (require, response) => { - const id = require.params.id - - Product.findById(id, (err, product)=> { - if(!err) { - response.status(200).send(product) - } - - else { - response.status(400).send({message: err.message}) - } - }) + ProductService.getProduct(require) + .then(product => { + response.status(200).send(product) + }) + + .catch(err => { + response.status(400).send({message: err.message}) + }) } static updateProduct = (require, response) => { @@ -88,18 +73,14 @@ class ProductController { } static deleteProduct = (require, response) => { - const id = require.params.id - - Product.findByIdAndDelete(id, err => { - - if(!err) { - response.status(200).send({ message: 'Successfully delete product' }) - } - - else { - response.status(500).send({messagea: err.message}) - } + ProductService.deleteProduct(require) + .then(() => { + response.status(200).send({message: 'Successfully delete product'}) }) + + .catch(err => { + response.status(500).send({message: err.message}) + }) } } diff --git a/scripts/routes/category-router.js b/scripts/routes/category-router.js index 6c3acbf3..3db1bfaf 100644 --- a/scripts/routes/category-router.js +++ b/scripts/routes/category-router.js @@ -1,13 +1,13 @@ import express from 'express' import CategoryController from '../controllers/category-controller.js' -const CatregoryController = express.Router() +const CategoryRouter = express.Router() -CatregoryController +CategoryRouter .post('/categories', CategoryController.createCategory) .get('/categories', CategoryController.getCategories) .get('/categories/:id', CategoryController.getCategory) .put('/categories/:id', CategoryController.updateCategory) .delete('/categories/:id', CategoryController.deleteCategory) -export default CatregoryController \ No newline at end of file +export default CategoryRouter \ No newline at end of file diff --git a/scripts/routes/index.js b/scripts/routes/index.js index f4780b78..e1d91362 100644 --- a/scripts/routes/index.js +++ b/scripts/routes/index.js @@ -9,8 +9,7 @@ const routes = app => { app .use(express.json()) - .use(productRouter) - .use(categoryRouter) + .use(productRouter, categoryRouter) } export { routes } \ No newline at end of file diff --git a/scripts/services/product-service.js b/scripts/services/product-service.js new file mode 100644 index 00000000..54e20f2b --- /dev/null +++ b/scripts/services/product-service.js @@ -0,0 +1,29 @@ +import Product from './../models/product-model.js' + +class ProductService { + static createProduct = require => { + return new Product(require.body).save() + } + + static getProducts = require => { + return Product.find().populate('category') + } + + static getProduct = require => { + return Product.findById(require.params.id).lean() + } + + static searchProductsByCategory = require => { + return Product.find({category: require.params.id}) + } + + static searchProductByTitle = require => { + return Product.find({title: require.query.title}).lean() + } + + static deleteProduct = require => { + return Product.findByIdAndDelete(require.params.id) + } +} + +export default ProductService \ No newline at end of file From 1c1d99f95820f36bb591ee130722fd91b4f5d1c0 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Tue, 24 Jan 2023 11:10:35 -0300 Subject: [PATCH 12/17] test: product unit test --- package.json | 6 +- scripts/controllers/category-controller.js | 2 +- scripts/controllers/product-controller.js | 14 ++-- scripts/models/category-model.js | 2 +- scripts/services/product-service.js | 5 ++ scripts/test/category-service.spec.js | 66 +++++++++++++++ scripts/test/product-service.spec.js | 97 ++++++++++++++++++++++ 7 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 scripts/test/category-service.spec.js create mode 100644 scripts/test/product-service.spec.js diff --git a/package.json b/package.json index 6a040d55..9fcb3e01 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "mocha scripts/test/product-service.spec.js", "dev": "nodemon server.js" }, "repository": { @@ -21,11 +21,13 @@ "homepage": "https://github.com/Lebackrobot/test-backend-nodejs#readme", "dependencies": { "chai": "^4.3.7", + "chai-http": "^4.3.0", + "chai-json-schema": "^1.5.1", "express": "^4.18.2", - "mocha": "^10.2.0", "mongoose": "^6.8.3" }, "devDependencies": { + "mocha": "^10.2.0", "nodemon": "^2.0.20" } } diff --git a/scripts/controllers/category-controller.js b/scripts/controllers/category-controller.js index b9def8c2..088da85e 100644 --- a/scripts/controllers/category-controller.js +++ b/scripts/controllers/category-controller.js @@ -43,7 +43,7 @@ class CategoryController { CategoryService.updateCategory(require) .then(success => { - response.status(200).send({ message: "Successfully delete category" }) + response.status(200).send({ message: "Successfully update category" }) }) .catch(err => { diff --git a/scripts/controllers/product-controller.js b/scripts/controllers/product-controller.js index 208058ae..7e9ff3af 100644 --- a/scripts/controllers/product-controller.js +++ b/scripts/controllers/product-controller.js @@ -59,16 +59,14 @@ class ProductController { } static updateProduct = (require, response) => { - const id = require.params.id - Product.findByIdAndUpdate(id, {$set: require.body}, err => { - if(!err) { - response.status(200).send({message: 'Successfully update product'}) - } + ProductService.updateProduct(require) + .then( success => { + response.status(200).send({message: 'Successfully update product'}) + }) - else { - response.status(500).send({message: err.message}) - } + .catch(err => { + response.status(500).send({message: err.message}) }) } diff --git a/scripts/models/category-model.js b/scripts/models/category-model.js index 4e2cde0c..5288e816 100644 --- a/scripts/models/category-model.js +++ b/scripts/models/category-model.js @@ -2,7 +2,7 @@ import mongoose from 'mongoose' const categorySchema = new mongoose.Schema({ id: {type: String, required: false}, - title: {type: String, require: true} + title: {type: String, required: true} }) export default mongoose.model('category', categorySchema) \ No newline at end of file diff --git a/scripts/services/product-service.js b/scripts/services/product-service.js index 54e20f2b..55d53dc0 100644 --- a/scripts/services/product-service.js +++ b/scripts/services/product-service.js @@ -20,6 +20,11 @@ class ProductService { static searchProductByTitle = require => { return Product.find({title: require.query.title}).lean() } + + + static updateProduct = require => { + return Product.findByIdAndUpdate(require.params.id, {$set: require.body}) + } static deleteProduct = require => { return Product.findByIdAndDelete(require.params.id) diff --git a/scripts/test/category-service.spec.js b/scripts/test/category-service.spec.js new file mode 100644 index 00000000..0f3e2983 --- /dev/null +++ b/scripts/test/category-service.spec.js @@ -0,0 +1,66 @@ +import chai from 'chai' +import chaiHttp from 'chai-http' +import chaiJsonSchema from 'chai-json-schema' +import Category from '../models/category-model.js' + +chai.use(chaiHttp) +chai.use(chaiJsonSchema) + +const urlBase = 'http://localhost:3000' + +describe('Category Router', () => { + it('POST /categories', done => { + const category = {title: 'Doces'} + + chai.request(urlBase).post('/categories').send(category).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(201) + chai.expect(response.body).to.include({ message: 'Successfully create category'}) + }) + + done() + }) + + it('GET /categories', done => { + chai.request(urlBase).get('/categories').end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response.body).to.be.jsonSchema([Category]) + }) + + done() + }) + + it('GET /categories/:id', done => { + const category = '63c179fef0992a4b3378dd8c' + + chai.request(urlBase).get(`/categories/${category}`).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response.body).to.be.jsonSchema(Category) + }) + done() + }) + + it('UPDATE /categories/:id', done => { + const category = '63c179fef0992a4b3378dd8c' + + chai.request(urlBase).put(`/categories/${category}`).send({title: "Salgadinhos"}).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response.body).to.include({message: 'Successfully update category'}) + }) + done() + }) + + it('REMOVE /categories:id', done => { + const category = '63caac82a5ea9dfa1387524a' + + chai.request(urlBase).delete(`/categories/${category}`).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response.body).to.include({message: 'Successfully delete category'}) + }) + done() + }) +}) diff --git a/scripts/test/product-service.spec.js b/scripts/test/product-service.spec.js new file mode 100644 index 00000000..2cf622cc --- /dev/null +++ b/scripts/test/product-service.spec.js @@ -0,0 +1,97 @@ +import chai from 'chai' +import chaiHttp from 'chai-http' +import chaiJsonSchema from 'chai-json-schema' +import Product from '../models/product-model.js' + +chai.use(chaiHttp) +chai.use(chaiJsonSchema) + +const urlBase = 'http://localhost:3000' + +describe('Product Router', () => { + it('POST /products', done => { + const product = { category: '63c179fef0992a4b3378dd8c', title: 'Fofura', description: 'Sabor pimenta', price: 4.00} + + chai.request(urlBase).post('/products').send(product).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(201) + chai.expect(response.body).to.include({ message: 'Successfully create product'}) + }) + + done() + }) + + it('GET /products', done => { + chai.request(urlBase).get('/products').end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response.body).to.be.jsonSchema([Product]) + }) + + done() + }) + + it('GET /products/:id', done => { + const id = '63cfddd5c9ebca69f3fe1242' + + chai.request(urlBase).get(`/products/${id}`).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response).to.be.jsonSchema(Product) + }) + + done() + }) + + it('GET /products/search', done => { + chai.request(urlBase).get('/products/search').query({title: 'Coxinha'}).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response).to.be.jsonSchema(Product) + }) + + done() + }) + + if('GET /product/category/:id', done => { + const id = '63c179fef0992a4b3378dd8c' + + chai.request(urlBase).get(`products/category/${id}`).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response).to.be.jsonSchema(Product) + }) + + done() + }) + + + it('UPDATE /products/:id', done => { + + const id = '63cfddd5c9ebca69f3fe1242' + const product = { category: '63c179fef0992a4b3378dd8c', title: 'Doritos', description: 'sabor primenta', price: 4.00} + + chai.request(urlBase).put(`/products/${id}`).send(product).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response).to.include({ message: 'Successfully update product' }) + + }) + + done() + }) + + it('DELETE /products/:id', done => { + const id = '63cfd8955ca541cc45e67a64' + + chai.request(urlBase).delete(`/products/${id}`).end((err, response) => { + chai.expect(err).to.be.null + chai.expect(response).to.have.status(200) + chai.expect(response).to.include({ message: 'Successfully delete product' }) + }) + + done() + }) + + +}) From 2fa2ae79ef7eb2d25339c92cd7ac3c4524b36be2 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Thu, 26 Jan 2023 11:36:23 -0300 Subject: [PATCH 13/17] =?UTF-8?q?doc:=20documenta=C3=A7=C3=A3o=20com=20swa?= =?UTF-8?q?gger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6a040d55..fb7b896a 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,11 @@ "chai": "^4.3.7", "express": "^4.18.2", "mocha": "^10.2.0", - "mongoose": "^6.8.3" + "mongoose": "^6.8.3", + "swagger-ui-express": "^4.6.0" }, "devDependencies": { + "@types/swagger-ui-express": "^4.1.3", "nodemon": "^2.0.20" } } From 858b8147320cf60461db95db6a7392ed6f7e52c6 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Thu, 26 Jan 2023 11:37:36 -0300 Subject: [PATCH 14/17] doc: swagger documentation --- scripts/routes/index.js | 5 +++ scripts/swagger.json | 79 +++++++++++++++++++++++++++++++++++++++++ server.js | 3 ++ 3 files changed, 87 insertions(+) create mode 100644 scripts/swagger.json diff --git a/scripts/routes/index.js b/scripts/routes/index.js index e1d91362..4875975d 100644 --- a/scripts/routes/index.js +++ b/scripts/routes/index.js @@ -2,12 +2,17 @@ import express from 'express' import productRouter from './product-router.js' import categoryRouter from './category-router.js' +import swaggerUI from 'swagger-ui-express' +import swaggerFile from './../swagger.json' assert { type: "json" } + const routes = app => { app.route('/').get((req, res) => { res.status(200).send({message: 'Lets bora!'}) }) app + .use(express.json()) + .use('/docs', swaggerUI.serve, swaggerUI.setup(swaggerFile)) .use(express.json()) .use(productRouter, categoryRouter) } diff --git a/scripts/swagger.json b/scripts/swagger.json new file mode 100644 index 00000000..3427adee --- /dev/null +++ b/scripts/swagger.json @@ -0,0 +1,79 @@ +{ + "openapi": "3.0.0", + "paths": { + "/products": { + "post": { + "tags": ["products"], + "summary": "Create Product", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "title": {"type": "string"} + }, + "example": { + "title": "Doces" + } + } + } + } + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "types": "string" + } + }, + "example": { + "message": "Successfully create product" + } + } + } + } + } + } + }, + "get": { + "tags": ["Products"], + "summary": "List all products", + "responses": { + "200": { + "description": "List all products", + "content": { + "application/json": { + "schema": { + "type": "Array", + "properties": { + "title": { + "type": "string" + }, + "_id": { + "type": "ID" + } + } + }, + "example": { + "categories": [ + {"_id": 123, "title": "Doces"}, + {"_id": 456, "title": "Salgados"}, + {"_id": 567, "title": "Sashimi"} + ] + } + + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/server.js b/server.js index fdbfd3c3..143f8d3c 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,10 @@ import app from './scripts/app.js' +import express from 'express' + const port = process.env.PORT || 3000 + app.listen(port, () => { console.log(`Server listen on port ${port}`) }) \ No newline at end of file From 0fa72d547a82741e438b399155cfc19e397942d4 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Thu, 26 Jan 2023 11:51:11 -0300 Subject: [PATCH 15/17] doc: update documentation --- scripts/swagger.json | 72 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/scripts/swagger.json b/scripts/swagger.json index 3427adee..f106246c 100644 --- a/scripts/swagger.json +++ b/scripts/swagger.json @@ -43,7 +43,7 @@ } }, "get": { - "tags": ["Products"], + "tags": ["products"], "summary": "List all products", "responses": { "200": { @@ -73,6 +73,76 @@ } } } + }, + "put": { + "tags": [ + "products" + ], + "summary": "Update Product", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "title": { + "type": "string" + } + }, + "example": { + "title": "Doces" + } + } + } + } + }, + "responses": { + "200": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "types": "string" + } + }, + "example": { + "message": "Successfully update product" + } + } + } + } + } + } + }, + "delete": { + "tags": [ + "products" + ], + "summary": "Update Product", + "responses": { + "200": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "types": "string" + } + }, + "example": { + "message": "Successfully delete product" + } + } + } + } + } + } } } } From 25761b3810dd2e68e7cf8249aaf5ff24e87ec7c2 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Thu, 26 Jan 2023 12:12:09 -0300 Subject: [PATCH 16/17] =?UTF-8?q?doc:=20documenta=C3=A7=C3=A3o=20com=20o?= =?UTF-8?q?=20swagger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/routes/index.js | 5 +- scripts/swagger.json | 183 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 178 insertions(+), 10 deletions(-) diff --git a/scripts/routes/index.js b/scripts/routes/index.js index 4875975d..adeac773 100644 --- a/scripts/routes/index.js +++ b/scripts/routes/index.js @@ -3,7 +3,7 @@ import productRouter from './product-router.js' import categoryRouter from './category-router.js' import swaggerUI from 'swagger-ui-express' -import swaggerFile from './../swagger.json' assert { type: "json" } +import swaggerJson from './../swagger.json' assert { type: "json" } const routes = app => { app.route('/').get((req, res) => { @@ -12,8 +12,7 @@ const routes = app => { app .use(express.json()) - .use('/docs', swaggerUI.serve, swaggerUI.setup(swaggerFile)) - .use(express.json()) + .use('/docs', swaggerUI.serve, swaggerUI.setup(swaggerJson)) .use(productRouter, categoryRouter) } diff --git a/scripts/swagger.json b/scripts/swagger.json index f106246c..154b993e 100644 --- a/scripts/swagger.json +++ b/scripts/swagger.json @@ -12,7 +12,10 @@ "schema": { "type": "object", "properties": { - "title": {"type": "string"} + "title": {"type": "string"}, + "description": {"type": "string"}, + "price": {"type": "number"}, + "category": {"type": "_id"} }, "example": { "title": "Doces" @@ -56,16 +59,21 @@ "title": { "type": "string" }, - "_id": { - "type": "ID" + "description": { + "type": "string" + }, + "price": { + "type": "number" + }, + "category": { + "type": "_id" } } }, "example": { "categories": [ - {"_id": 123, "title": "Doces"}, - {"_id": 456, "title": "Salgados"}, - {"_id": 567, "title": "Sashimi"} + {"_id": "123", "title": "Doritos", "description": "Salgadinho caro", "price": 15.99, "category": "567" }, + {"_id": "123", "title": "Coxinha", "description": "frango e queijo", "price": 5.00, "category": "789" } ] } @@ -144,6 +152,167 @@ } } } + }, + + "/categories": { + "post": { + "tags": [ + "categories" + ], + "summary": "Create Category", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "title": { + "type": "string" + } + }, + "example": { + "title": "Doces" + } + } + } + } + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "types": "string" + } + }, + "example": { + "message": "Successfully create product" + } + } + } + } + } + } + }, + "get": { + "tags": [ + "categories" + ], + "summary": "List all categories", + "responses": { + "200": { + "description": "List all categories", + "content": { + "application/json": { + "schema": { + "type": "Array", + "properties": { + "title": { + "type": "string" + }, + "_id": { + "type": "_id" + } + } + }, + "example": { + "categories": [ + { + "_id": 123, + "title": "Doces" + }, + { + "_id": 456, + "title": "Salgados" + }, + { + "_id": 567, + "title": "Sashimi" + } + ] + } + } + } + } + } + }, + "put": { + "tags": [ + "categories" + ], + "summary": "Update Category", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "title": { + "type": "string" + } + }, + "example": { + "title": "Doces" + } + } + } + } + }, + "responses": { + "200": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "types": "string" + } + }, + "example": { + "message": "Successfully update product" + } + } + } + } + } + } + }, + "delete": { + "tags": [ + "categories" + ], + "summary": "Update Category", + "responses": { + "200": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "types": "string" + } + }, + "example": { + "message": "Successfully delete product" + } + } + } + } + } + } + } } } -} \ No newline at end of file +} + + From 106bd28ec397e8c2a2abfca16d683c76f45e9f73 Mon Sep 17 00:00:00 2001 From: Pedro Fernandes Date: Thu, 26 Jan 2023 12:30:26 -0300 Subject: [PATCH 17/17] =?UTF-8?q?doc:=20atualizando=20documenta=C3=A7?= =?UTF-8?q?=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/swagger.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/swagger.json b/scripts/swagger.json index 154b993e..998aa7c0 100644 --- a/scripts/swagger.json +++ b/scripts/swagger.json @@ -18,7 +18,11 @@ "category": {"type": "_id"} }, "example": { - "title": "Doces" + "title": "Doritos", + "description": "Salgadinho caro", + "price": 15.99, + "category": 567 + } } }