Skip to content

Commit fa1443c

Browse files
committed
2 parents f53d0fd + d6042a1 commit fa1443c

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import Product from "../models/Product";
2+
import User from "../models/User";
3+
4+
class ProductController {
5+
async index(request, response) {
6+
const products = await Product.find();
7+
8+
return response.json(products);
9+
}
10+
11+
async find(request, response) {
12+
const { id } = request.params;
13+
const result = await Product.find({ where: { id } });
14+
const product = result[0];
15+
16+
if (!product)
17+
return response.status(400).json({ erro: 'Product not found!' });
18+
19+
return response.json(product);
20+
}
21+
22+
async create(request, response) {
23+
const { description, user_id } = request.body;
24+
25+
if (!description)
26+
return response.status(400).json({ error: 'Product description can\'t be null' });
27+
28+
if (user_id === null)
29+
return response.status(400).json({ error: 'User ID can\'t be null' });
30+
31+
const result = await User.find({ where: { id: user_id } });
32+
const user = result[0];
33+
34+
if (!user)
35+
return response.status(400).json({ erro: 'User not found!' });
36+
37+
const product = await Product.create({ description, user_id });
38+
39+
return response.status(201).json(product);
40+
}
41+
42+
async update(request, response) {
43+
const { id } = request.params;
44+
const { description } = request.body;
45+
const result = await Product.find({ where: { id } });
46+
const product = result[0];
47+
48+
if (!product)
49+
return response.status(400).json({ error: 'Product not found!' });
50+
51+
if (!description)
52+
return response.status(400).json({ error: 'Product description can\'t be null' });
53+
54+
await Product.update({ description }, { where: { id } });
55+
56+
return response.status(204).send();
57+
}
58+
59+
async delete(request, response) {
60+
const { id } = request.params;
61+
const result = await Product.find({ where: { id } });
62+
const product = result[0];
63+
64+
if (!product)
65+
return response.status(400).json({ error: 'Product not found!' });
66+
67+
await Product.delete({ where: { id } });
68+
return response.status(204).send();
69+
}
70+
}
71+
72+
export default new ProductController();

src/routes/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Router } from 'express';
22
import { router as usersRouter } from './usersRoutes';
3+
import { router as productsRouter } from './productsRoutes';
34

45
const router = Router();
56

67
router.use('/users', usersRouter);
8+
router.use('/products', productsRouter);
79

810
export { router };

src/routes/productsRoutes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Router } from 'express';
2+
import ProductController from '../controllers/ProductController';
3+
4+
const router = Router();
5+
6+
router.get('/', ProductController.index);
7+
router.post('/', ProductController.create);
8+
router.get('/:id', ProductController.find);
9+
router.put('/:id', ProductController.update);
10+
router.delete('/:id', ProductController.delete);
11+
12+
export { router };

0 commit comments

Comments
 (0)