diff --git a/client/src/app.js b/client/src/app.js index 4880a79..6a64466 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -205,9 +205,9 @@ function App() { // ITEM FUNCTIONS /////////////////////////////////////////////////////////////// const moveItem = async (itemID, boxIDTarget) => { //post req - console.log(`move item: ${itemID} to box ${boxIDTarget}`); try { const response = await axios.post(`${api.baseApiUrl}/item/${itemID}/${boxIDTarget}`); + setData(response.data); } catch(error) { console.log(error); diff --git a/server/server.js b/server/server.js index 37d3680..d1f918c 100644 --- a/server/server.js +++ b/server/server.js @@ -116,7 +116,29 @@ 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; - response.send({itemID,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}}}); + //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------------*/