-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcosmetic.controller.js
117 lines (106 loc) · 3.53 KB
/
cosmetic.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const express = require('express');
const { body, validationResult } = require('express-validator');
const router = express.Router();
const CosmeticDto = require('./cosmetic.dto')
const CosmeticRepository = require('./cosmetic.repository')
// GET /cosmetics
router.get('/', async (req, res) => {
try {
const cosmetics = await CosmeticRepository.getAllCosmetics();
const cosmeticDTOs = cosmetics.map((cosmetic) => {
return new CosmeticDto(
cosmetic.id,
cosmetic.name,
cosmetic.type
);
});
res.json(cosmeticDTOs);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// GET /cosmetics/:id
router.get('/:id', async (req, res) => {
const { id } = req.params;
try {
const cosmetic = CosmeticRepository.getCosmeticById(id);
if (!cosmetic) {
return res.status(404).json({ error: 'Cosmetic not found!' });
}
const cosmeticDTO= new CosmeticDto(
cosmetic.id,
cosmetic.name,
cosmetic.type
);
res.json(cosmeticDTO);
} catch (error) {
console.error(error);
res.status(500).json( { error: 'Internal Server Error' });
}
});
// POST /cosmetics
router.post(
'/',
[
body('name').notEmpty().withMessage('Name is required'),
body('type').notEmpty().withMessage('Type is required'),
],
async (req, res) => {
const { name, type } = req.body;
// Check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
const cosmetic = await CosmeticRepository.createCosmetic({ name, type });
const cosmeticDTO = new CosmeticDto(cosmetic.id, cosmetic.name, cosmetic.type);
res.status(201).json(cosmeticDTO);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
}
);
// PUT /cosmetics/:id
router.put(
'/:id',
[
body('name').notEmpty().withMessage('Name is required'),
body('type').notEmpty().withMessage('Type is required'),
],
async (req, res) => {
const {id} = req.params;
const {name, type} = req.body;
// Check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({errors: errors.array()});
}
try {
const existingCosmetic = await CosmeticRepository.getCosmeticById(id);
if (!existingCosmetic) {
return res.status(404).json({error: 'Cosmetic not found'});
}
const updatedCosmetic = await CosmeticRepository.updateCosmetic({id, name, type});
const cosmeticDTO = new CosmeticDto(updatedCosmetic.id, updatedCosmetic.name, updatedCosmetic.type);
res.json(cosmeticDTO);
} catch (error) {
console.error(error);
res.status(500).json({error: 'Internal Server Error'});
}
}
);
// DELETE /cosmetics/:id
router.delete('/:id', async (req, res) => {
const { id } = req.params;
try {
await CosmeticRepository.deleteCosmetic(id);
res.sendStatus(204);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
module.exports = { router };