diff --git a/main/controllers/ComponentsController.js b/main/controllers/ComponentsController.js new file mode 100644 index 0000000..335501f --- /dev/null +++ b/main/controllers/ComponentsController.js @@ -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: "Не удалось получить комплектующие" + }) + } +} \ No newline at end of file diff --git a/main/index.js b/main/index.js index 234ed03..33d5d71 100644 --- a/main/index.js +++ b/main/index.js @@ -3,6 +3,7 @@ import mongoose from "mongoose"; import cors from "cors"; import {login, register} from "./controllers/AdminController.js"; import checkAuth from "./utils/checkAuth.js"; +import {addComponent, getAll, getOne} from "./controllers/ComponentsController.js"; const app = express(); const PORT = 4444; @@ -22,10 +23,16 @@ app.get('/', (req, res) => { }); app.post('/auth/register', register); - app.post('/auth/login', login); + + +app.post('/components', checkAuth, addComponent) +app.get('/components', getAll) +app.get('/components/:id', getOne); + + app.listen(PORT, (err) => { if (err) { console.warn(err) diff --git a/main/models/Components.js b/main/models/Components.js new file mode 100644 index 0000000..9f80921 --- /dev/null +++ b/main/models/Components.js @@ -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); \ No newline at end of file