add server move item

This commit is contained in:
np.w
2021-02-19 19:54:11 -06:00
parent a0c1c5868e
commit df74644088
2 changed files with 24 additions and 2 deletions
+23 -1
View File
@@ -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------------*/