Skip to content

Commit

Permalink
Merge pull request #13 from moevm/bogdanov_compoints
Browse files Browse the repository at this point in the history
Server: Components
  • Loading branch information
necitboss authored Nov 10, 2024
2 parents dd49aad + cf94541 commit 62db372
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
76 changes: 76 additions & 0 deletions main/controllers/ComponentsController.js
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: "Не удалось получить комплектующие"
})
}
}
9 changes: 8 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions main/models/Components.js
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);

0 comments on commit 62db372

Please sign in to comment.