This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTechController.js
More file actions
58 lines (43 loc) · 1.28 KB
/
Copy pathTechController.js
File metadata and controls
58 lines (43 loc) · 1.28 KB
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
const Tech = require('../models/Tech');
const User = require('../models/User');
module.exports = {
async index(req, res) {
const { user_id } = req.params;
const user = await User.findByPk(user_id, {
include: {
association: 'techs',
attributes: ['name'],
through: {
attributes: []
}
}
})
return res.json(user.techs);
},
async store(req, res) {
const { user_id } = req.params;
const { name } = req.body;
const user = await User.findByPk(user_id);
if (!user) {
return res.status(400).json({ error: 'User not found' });
}
const [tech] = await Tech.findOrCreate({
where: { name }
});
await user.addTech(tech);
return res.json(tech);
},
async delete(req, res) {
const { user_id } = req.params;
const { name } = req.body;
const user = await User.findByPk(user_id);
if (!user) {
return res.status(400).json({ error: 'User not found' });
}
const tech = await Tech.findOne({
where: { name }
});
await user.removeTech(tech);
return res.json();
}
}