This repository was archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproduct_controller.js
More file actions
104 lines (97 loc) · 2.94 KB
/
Copy pathproduct_controller.js
File metadata and controls
104 lines (97 loc) · 2.94 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
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
const Product = require('../models').Product
const Tag = require('../models').Tag
const License = require('../models').License
const Usage = require('../models').Usage
module.exports = {
index: (req, res) => {
Product
.findAll({
include: [
{ model: Tag, as: 'tags' },
{ model: License, as: 'licenses' },
{ model: Usage, as: 'usage' }
]
})
.then(products => {
return res.status(200).send(products)
})
.catch(error => res.status(500).send("Server error"))
},
show: (req, res) => {
Product
.findById(req.params.id, {
include: [
{ model: Tag, as: 'tags' },
{ model: License, as: 'licenses' },
{ model: Usage, as: 'usage' }
]
})
.then(product => {
if (!product)
return res.status(404).send({ message: "Product not found" })
return res.status(200).send(product)
})
.catch(error => res.status(500).send("Server error"))
},
create: (req, res) => {
const tagList = req.body.tags || []
const tagObjects = tagList.map(t => {
return { name: t }
})
Product
.create({
name: req.body.name,
repository: req.body.repository,
description: req.body.description,
laborHours: req.body.laborHours,
tags: tagObjects,
licenses: req.body.licenses,
usageKey: req.body.usageKey
}, {
include: [
{ model: Tag, as: 'tags' },
{ model: License, as: 'licenses' }
]
})
.then(product => res.status(201).send(product))
.catch(error => res.status(500).send("Server error"))
},
update: (req, res) => {
Product
.findById(req.params.id, {
include: [
{ model: Tag, as: 'tags' },
{ model: License, as: 'licenses' },
{ model: Usage, as: 'usage' }
]
})
.then(product => {
if (!product)
return res.status(404).send({ message: "Product not found" })
product
.update({
name: req.body.name || product.name,
repository: req.body.repository || product.repository,
description: req.body.description || product.description,
laborHours: req.body.laborHours || product.laborHours,
usageKey: req.body.usageKey || product.usageKey
})
.then(() => res.status(200).send(product))
.catch(error => res.status(500).send("Server error"))
})
.catch(error => res.status(500).send("Server error"))
},
destroy: (req, res) => {
Product
.findById(req.params.id)
.then(product => {
if (!product)
return res.status(404).send({ message: "Product not found" })
product
.destroy()
.then(() => res.status(204).send())
.catch(error => res.status(500).send("Server error"))
})
.catch(error => res.status(500).send("Server error"))
}
}