generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from moevm/bogdanov_compoints
Server: Components
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import ComponentsModel from "../models/Components.js"; | ||
|
||
export const addComponent = async (req, res) => { | ||
try { | ||
const components = await ComponentsModel.find({name: req.body.name}); | ||
// console.log(components); | ||
if (components[0]) { | ||
res.json({ | ||
message: "Такой компонент уже есть" | ||
}) | ||
return; | ||
} | ||
const doc = new ComponentsModel({ | ||
name: req.body.name, | ||
type: req.body.type, | ||
price: req.body.price, | ||
count: req.body.count, | ||
main_properties: req.body.main_properties, | ||
other_properties: req.body.other_properties | ||
}) | ||
const post = await doc.save(); | ||
res.json(post); | ||
} catch (err) { | ||
console.warn(err); | ||
res.status(500).json({ | ||
message: "Не удалось создать компонент" | ||
}) | ||
} | ||
} | ||
|
||
export const getAll = async (req, res) => { | ||
try { | ||
const components = await ComponentsModel.find().exec(); | ||
res.json(components); | ||
}catch (err){ | ||
console.warn(err); | ||
res.status(500).json({ | ||
message: "Не удалось получить комплектующие" | ||
}) | ||
} | ||
} | ||
|
||
export const getOne = async (req, res) => { | ||
try { | ||
const componentId = String(req.params.id); | ||
|
||
ComponentsModel.findOne({ | ||
_id: componentId | ||
}).then( | ||
(doc, err) => { | ||
if (err) { | ||
console.warn(err); | ||
return res.status(500).json({ | ||
message: "Не удалось получить комплектующие" | ||
}) | ||
} | ||
if (!doc){ | ||
return res.status(404).json({ | ||
message: "Компонент не найден" | ||
}) | ||
} | ||
res.json(doc); | ||
} | ||
).catch(err => { | ||
console.warn(err); | ||
res.status(500).json({ | ||
message: "Не удалось получить комплектующие" | ||
}) | ||
}) | ||
}catch (err){ | ||
console.warn(err); | ||
res.status(500).json({ | ||
message: "Не удалось получить комплектующие" | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const ComponentsSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
type: { | ||
type: String, | ||
required: true, | ||
}, | ||
price: { | ||
type: Number, | ||
required: true, | ||
}, | ||
count: { | ||
type: Number, | ||
required: true, | ||
}, | ||
main_properties: { | ||
type: Array, | ||
required: true, | ||
}, | ||
other_properties: { | ||
type: Array, | ||
required: true, | ||
} | ||
}, { | ||
timestamps: true | ||
}); | ||
|
||
export default mongoose.model('Components', ComponentsSchema); |