diff --git a/server/server.js b/server/server.js index d1f918c..bcc6ef5 100644 --- a/server/server.js +++ b/server/server.js @@ -9,11 +9,11 @@ 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}); +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() { +db.once('open', function () { console.log(`connected to mongodb: ${MONGO_IP_ADDRESS}`); }); @@ -52,7 +52,7 @@ app.get('/box', async (request, response) => { // create box app.post('/box', async (request, response) => { const numBoxes = await Box.countDocuments({}); - const query = Object.assign({sort: numBoxes}, request.body); + const query = Object.assign({ sort: numBoxes }, request.body); await Box.create(query); const updatedBoxes = await getAllBoxes(); response.send(updatedBoxes); @@ -60,13 +60,13 @@ app.post('/box', async (request, response) => { //edit box app.put('/box/:id', async (request, response) => { const id = request.params.id; - await Box.updateOne({_id: id},{ $set: request.body}); + 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}); + await Box.deleteOne({ _id: request.params.id }); const updatedBoxes = await getAllBoxes(); response.send(updatedBoxes); }); @@ -82,7 +82,7 @@ app.put('/box/:id/sort', async (request, response) => { //create item app.post('/box/:id/item', async (request, response) => { const id = request.params.id; - const query = {_id: id}; + const query = { _id: id }; await Box.updateOne(query, { $push: { item: request.body } }); @@ -116,36 +116,32 @@ app.delete('/box/:boxId/item/:itemId', async (request, response) => { app.post('/box/item/:itemId/:boxIdTarget', async (request, response) => { const itemID = request.params.itemId; const targetBoxID = request.params.boxIdTarget; - const query = {_id: targetBoxID}; + 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}}}); + const box = await Box.find({ "item._id": itemID }); + 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] } - }); + await Box.updateOne(query, { $push: { item: item[0].item[0] } }); const boxes = await getAllBoxes(); response.send(boxes); } - catch(error){ - console.log(error); + catch (error) { response.json(error); } - + /*...*/ }) /*------------/CONTROLLER------------*/ const getAllBoxes = async (queryParameters) => { queryParameters = queryParameters ?? {}; - const data = await Box.find(queryParameters).sort({sort: 1}); + const data = await Box.find(queryParameters).sort({ sort: 1 }); return data; }