Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
167493f
initial commit: template and connect to db
umpretodev Jan 11, 2023
b64f85d
feat: product route
umpretodev Jan 11, 2023
089900b
feat: product CRUD
umpretodev Jan 11, 2023
2005c9a
merge: merge branch 'routes/product-routes' into main
umpretodev Jan 11, 2023
f3049c5
feat: category CRUD
umpretodev Jan 11, 2023
988f86d
merge: branch 'db-schemas/category' into main
umpretodev Jan 11, 2023
400ce5e
fix: tratamento de consultas por query
umpretodev Jan 11, 2023
8ab0073
merge: branch 'db-schemas/category' into main
umpretodev Jan 11, 2023
b07446b
Update README.md
umpretodev Jan 11, 2023
c401066
WIP: class break
umpretodev Jan 11, 2023
fc84947
Merge remote-tracking branch 'origin/main' into main
umpretodev Jan 11, 2023
b6e7e45
Update README.md
umpretodev Jan 11, 2023
664fe06
Update README.md
umpretodev Jan 11, 2023
8142247
refactor: refatoração de código
umpretodev Jan 13, 2023
2490151
refactor: refatoração de código
umpretodev Jan 18, 2023
a661ba3
merge: branch 'refactoring' into main
umpretodev Jan 18, 2023
1c1d99f
test: product unit test
umpretodev Jan 24, 2023
2fa2ae7
doc: documentação com swagger
umpretodev Jan 26, 2023
858b814
doc: swagger documentation
umpretodev Jan 26, 2023
27051c7
merge branch 'test/unit-testing' into main
umpretodev Jan 26, 2023
a06d6d2
merge branch 'swagger-documentation' into main
umpretodev Jan 26, 2023
0fa72d5
doc: update documentation
umpretodev Jan 26, 2023
25761b3
doc: documentação com o swagger
umpretodev Jan 26, 2023
106bd28
doc: atualizando documentação
umpretodev Jan 26, 2023
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
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,63 @@ 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

<h1>Code Documentation</h1>
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
```

<h2>About API</h2>
Here is some technical information about modeling the problem

<h3>Schemas</h3>
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}
})
```

<h3>Routes</h3>

Product Routes:

Method | EndPoint | Body Params |Returns
:---------: | :------ | :-------: | :--------:
<strong>POST</strong>| /products | product | messag : Object
<strong>PUT<strong> | /products/:id | products | message : Object
<strong>GET</strong> | /products | - |products: Array
<strong>GET</strong> | /product/:id | - |product: Object
<strong>GET</strong> | /products/category/:id | - | products: Array
<strong>GET</strong>| /product/search?title=queryParam | - | message: Object
<strong>DELETE</strong> | /products/:id | - | message: Object

Categorie Routes:

Method | EndPoint | Body Params |Returns
:---------: | :------ | :-------: | :--------:
<strong>POST</strong>| /categories | category | message: Object
<strong>PUT<strong> | /categories/:id | category | message: Object
<strong>GET</strong> | /categories | - |categories: Array
<strong>GET</strong> | /categories/:id | - |category: Object
<strong>DELETE</strong> | /categories/:id | - | message: Object
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "test-backend-nodejs",
"version": "1.0.0",
"description": "<h1>Backend Analyst Candidate Testing</h1>",
"main": "index.js",
"type": "module",
"scripts": {
"test": "mocha scripts/test/product-service.spec.js",
"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",
"chai-http": "^4.3.0",
"chai-json-schema": "^1.5.1",
"express": "^4.18.2",
"mocha": "^10.2.0",
"mongoose": "^6.8.3",
"swagger-ui-express": "^4.6.0"
},
"devDependencies": {
"mocha": "^10.2.0",
"@types/swagger-ui-express": "^4.1.3",
"nodemon": "^2.0.20"
}
}
14 changes: 14 additions & 0 deletions scripts/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +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
5 changes: 5 additions & 0 deletions scripts/config/db-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mongoose from 'mongoose'

mongoose.connect('mongodb+srv://admin:[email protected]/test-backend-nodejs')

export default mongoose.connection
66 changes: 66 additions & 0 deletions scripts/controllers/category-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import CategoryService from './../services/category-services.js'

class CategoryController {

static createCategory = (require, response) => {

CategoryService.createCategory(require)
.then(success => {
response.status(201).send({message:'Successfully create category'})
})

.catch(err => {
response.status(500).send({ message: err.message})
})
}

static getCategories = (require, response) => {

CategoryService.getCategories()
.then(categories => {
response.status(200).send(categories)
})

.catch(err => {
response.status(400).send({ message: err.message })
})
}

static getCategory = (require, response) => {

CategoryService.getCategory(require)
.then(category => {
response.status(200).send(category)
})

.catch(err => {
response.status(400).send({ message: err.message })
})

}

static updateCategory = (require, response) => {

CategoryService.updateCategory(require)
.then(success => {
response.status(200).send({ message: "Successfully update category" })
})

.catch(err => {
response.status(500).send({message: err.message})
})
}

static deleteCategory = (require, response) => {
CategoryService.deleteCategory(require)
.then(success => {
response.status(200).send({message: "Successfully delete category"})
})

.catch(err => {
response.status(500).send({message: err.message})
})
}
}

export default CategoryController
85 changes: 85 additions & 0 deletions scripts/controllers/product-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import ProductService from './../services/product-service.js'

class ProductController {

static createProduct = (require, response) => {
ProductService.createProduct(require)
.then(() => {
response.status(201).send({message: 'Successfully create product'})
})

.catch(err => {
response.status(500).send({message: err.message})
})
}

static getProducts = (require, response) => {
ProductService.getProducts(require)
.then(products => {
response.status(200).send(products)
})

.catch(err => {
response.status(400).send({message: err.message})
})
}

static searchProductsByCategory = (require, response) => {
ProductService.searchProductsByCategory(require)
.then(products => {
response.status(200).send(products)
})

.catch(err => {
response.status(400).send({message: err.message})
})
}

static searchProductByTitle = (require, response) => {
ProductService.searchProductByTitle(require)
.then(product => {
response.status(200).send(product)
})

.catch(err => {
response.status(400).send({message: err.message})
})
}

static getProduct = (require, response) => {

ProductService.getProduct(require)
.then(product => {
response.status(200).send(product)
})

.catch(err => {
response.status(400).send({message: err.message})
})
}

static updateProduct = (require, response) => {

ProductService.updateProduct(require)
.then( success => {
response.status(200).send({message: 'Successfully update product'})
})

.catch(err => {
response.status(500).send({message: err.message})
})
}

static deleteProduct = (require, response) => {
ProductService.deleteProduct(require)
.then(() => {
response.status(200).send({message: 'Successfully delete product'})
})

.catch(err => {
response.status(500).send({message: err.message})
})
}
}

export default ProductController
8 changes: 8 additions & 0 deletions scripts/models/category-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import mongoose from 'mongoose'

const categorySchema = new mongoose.Schema({
id: {type: String, required: false},
title: {type: String, required: true}
})

export default mongoose.model('category', categorySchema)
11 changes: 11 additions & 0 deletions scripts/models/product-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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}
})

export default mongoose.model('products', productSchema)
13 changes: 13 additions & 0 deletions scripts/routes/category-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import express from 'express'
import CategoryController from '../controllers/category-controller.js'

const CategoryRouter = express.Router()

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 CategoryRouter
19 changes: 19 additions & 0 deletions scripts/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import express from 'express'
import productRouter from './product-router.js'
import categoryRouter from './category-router.js'

import swaggerUI from 'swagger-ui-express'
import swaggerJson 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(swaggerJson))
.use(productRouter, categoryRouter)
}

export { routes }
16 changes: 16 additions & 0 deletions scripts/routes/product-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express'
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)


export default productRouter
25 changes: 25 additions & 0 deletions scripts/services/category-services.js
Original file line number Diff line number Diff line change
@@ -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
Loading