155 lines
4.6 KiB
JavaScript
155 lines
4.6 KiB
JavaScript
const express = require('express');
|
|
const mongoose = require('mongoose');
|
|
const app = express();
|
|
const cors = require('cors');
|
|
const { response } = require('express');
|
|
|
|
app.use(cors(), express.json());
|
|
|
|
const port = process.env.PORT || 5000;
|
|
const MONGO_IP_ADDRESS = '192.168.100.102';
|
|
|
|
mongoose.connect(`mongodb://${MONGO_IP_ADDRESS}/storage`, {useNewUrlParser: true});
|
|
const db = mongoose.connection;
|
|
|
|
db.on('error', console.error.bind(console, 'connection error:'));
|
|
db.once('open', function() {
|
|
console.log(`connected to mongodb: ${MONGO_IP_ADDRESS}`);
|
|
});
|
|
|
|
|
|
|
|
|
|
/*------------MODELS------------*/
|
|
//create schema
|
|
var itemSchema = new mongoose.Schema({
|
|
name: String,
|
|
description: String,
|
|
quantity: Number
|
|
});
|
|
|
|
var boxSchema = new mongoose.Schema({
|
|
name: String,
|
|
description: String,
|
|
item: [itemSchema],
|
|
sort: Number,
|
|
});
|
|
|
|
//compile schema into model
|
|
var Box = mongoose.model('box', boxSchema, 'box');
|
|
var Item = mongoose.model('item', itemSchema, 'item');
|
|
/*------------/MODELS------------*/
|
|
|
|
/*------------CONTROLLER------------*/
|
|
app.listen(port, () => console.log(`Listening on port ${port}`));
|
|
|
|
// get all boxes
|
|
app.get('/box', async (request, response) => {
|
|
const boxes = await getAllBoxes();
|
|
response.send(boxes);
|
|
});
|
|
|
|
// create box
|
|
app.post('/box', async (request, response) => {
|
|
const numBoxes = await Box.countDocuments({});
|
|
const query = Object.assign({sort: numBoxes}, request.body);
|
|
await Box.create(query);
|
|
const updatedBoxes = await getAllBoxes();
|
|
response.send(updatedBoxes);
|
|
});
|
|
//edit box
|
|
app.put('/box/:id', async (request, response) => {
|
|
const id = request.params.id;
|
|
await Box.updateOne({_id: id},{ $set: request.body});
|
|
const updatedBoxes = await getAllBoxes();
|
|
response.send(updatedBoxes);
|
|
});
|
|
//delete box
|
|
app.delete('/box/:id', async (request, response) => {
|
|
await Box.deleteOne({_id: request.params.id});
|
|
const updatedBoxes = await getAllBoxes();
|
|
response.send(updatedBoxes);
|
|
});
|
|
//change box sort
|
|
app.put('/box/:id/sort', async (request, response) => {
|
|
const source = await getBox(request.params.id);
|
|
const target = request.body;
|
|
await Box.updateOne({ _id: source._id }, { $set: { sort: target.sort } });
|
|
await Box.updateOne({ _id: target._id }, { $set: { sort: source.sort } });
|
|
const updatedBoxes = await getAllBoxes();
|
|
response.send(updatedBoxes);
|
|
})
|
|
//create item
|
|
app.post('/box/:id/item', async (request, response) => {
|
|
const id = request.params.id;
|
|
const query = {_id: id};
|
|
await Box.updateOne(query, {
|
|
$push: { item: request.body }
|
|
});
|
|
const updatedBox = await getBox(id);
|
|
response.send(updatedBox);
|
|
});
|
|
//edit item
|
|
app.put('/box/:boxId/item/:itemId', async (request, response) => {
|
|
const boxID = request.params.boxId;
|
|
const itemID = request.params.itemId;
|
|
await Box.updateOne({ '_id': boxID, 'item._id': itemID }, {
|
|
$set: {
|
|
'item.$.name': request.body.name,
|
|
'item.$.description': request.body.description,
|
|
'item.$.quantity': request.body.quantity
|
|
}
|
|
})
|
|
const updatedBox = await getBox(boxID);
|
|
response.send(updatedBox);
|
|
});
|
|
//delete item
|
|
app.delete('/box/:boxId/item/:itemId', async (request, response) => {
|
|
const boxID = request.params.boxId;
|
|
const itemID = request.params.itemId;
|
|
await Box.update({ _id: boxID }, { $pull: { item: { _id: itemID } } });
|
|
const box = await getBox(boxID);
|
|
response.send(box);
|
|
});
|
|
|
|
//move item
|
|
app.post('/box/item/:itemId/:boxIdTarget', async (request, response) => {
|
|
const itemID = request.params.itemId;
|
|
const targetBoxID = request.params.boxIdTarget;
|
|
const query = {_id: targetBoxID};
|
|
//the way I am doing this cannot be the best way...fix me later
|
|
|
|
try {
|
|
//get item in source box
|
|
const box = await Box.find({"item._id": itemID });
|
|
console.log(box);
|
|
const item = await Box.find( {"item._id": itemID}, {item: {$elemMatch: {_id: itemID}}});
|
|
//remove item from source box
|
|
await Box.updateOne({ _id: box[0]._id }, { $pull: { item: { _id: itemID } } });
|
|
//add item to target box
|
|
await Box.updateOne(query, {
|
|
$push: { item: item[0].item[0] }
|
|
});
|
|
|
|
const boxes = await getAllBoxes();
|
|
response.send(boxes);
|
|
}
|
|
catch(error){
|
|
console.log(error);
|
|
response.json(error);
|
|
}
|
|
|
|
/*...*/
|
|
})
|
|
/*------------/CONTROLLER------------*/
|
|
|
|
const getAllBoxes = async (queryParameters) => {
|
|
queryParameters = queryParameters ?? {};
|
|
const data = await Box.find(queryParameters).sort({sort: 1});
|
|
return data;
|
|
}
|
|
|
|
const getBox = async (id) => {
|
|
const data = await Box.findById(id);
|
|
return data;
|
|
} |