add server move item
This commit is contained in:
+1
-1
@@ -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);
|
||||
|
||||
+23
-1
@@ -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------------*/
|
||||
|
||||
Reference in New Issue
Block a user