remove logs
This commit is contained in:
+14
-18
@@ -9,11 +9,11 @@ app.use(cors(), express.json());
|
|||||||
const port = process.env.PORT || 5000;
|
const port = process.env.PORT || 5000;
|
||||||
const MONGO_IP_ADDRESS = '192.168.100.102';
|
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;
|
const db = mongoose.connection;
|
||||||
|
|
||||||
db.on('error', console.error.bind(console, 'connection error:'));
|
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}`);
|
console.log(`connected to mongodb: ${MONGO_IP_ADDRESS}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ app.get('/box', async (request, response) => {
|
|||||||
// create box
|
// create box
|
||||||
app.post('/box', async (request, response) => {
|
app.post('/box', async (request, response) => {
|
||||||
const numBoxes = await Box.countDocuments({});
|
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);
|
await Box.create(query);
|
||||||
const updatedBoxes = await getAllBoxes();
|
const updatedBoxes = await getAllBoxes();
|
||||||
response.send(updatedBoxes);
|
response.send(updatedBoxes);
|
||||||
@@ -60,13 +60,13 @@ app.post('/box', async (request, response) => {
|
|||||||
//edit box
|
//edit box
|
||||||
app.put('/box/:id', async (request, response) => {
|
app.put('/box/:id', async (request, response) => {
|
||||||
const id = request.params.id;
|
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();
|
const updatedBoxes = await getAllBoxes();
|
||||||
response.send(updatedBoxes);
|
response.send(updatedBoxes);
|
||||||
});
|
});
|
||||||
//delete box
|
//delete box
|
||||||
app.delete('/box/:id', async (request, response) => {
|
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();
|
const updatedBoxes = await getAllBoxes();
|
||||||
response.send(updatedBoxes);
|
response.send(updatedBoxes);
|
||||||
});
|
});
|
||||||
@@ -82,7 +82,7 @@ app.put('/box/:id/sort', async (request, response) => {
|
|||||||
//create item
|
//create item
|
||||||
app.post('/box/:id/item', async (request, response) => {
|
app.post('/box/:id/item', async (request, response) => {
|
||||||
const id = request.params.id;
|
const id = request.params.id;
|
||||||
const query = {_id: id};
|
const query = { _id: id };
|
||||||
await Box.updateOne(query, {
|
await Box.updateOne(query, {
|
||||||
$push: { item: request.body }
|
$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) => {
|
app.post('/box/item/:itemId/:boxIdTarget', async (request, response) => {
|
||||||
const itemID = request.params.itemId;
|
const itemID = request.params.itemId;
|
||||||
const targetBoxID = request.params.boxIdTarget;
|
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
|
//the way I am doing this cannot be the best way...fix me later
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//get item in source box
|
//get item in source box
|
||||||
const box = await Box.find({"item._id": itemID });
|
const box = await Box.find({ "item._id": itemID });
|
||||||
console.log(box);
|
const item = await Box.find({ "item._id": itemID }, { item: { $elemMatch: { _id: itemID } } });
|
||||||
const item = await Box.find( {"item._id": itemID}, {item: {$elemMatch: {_id: itemID}}});
|
|
||||||
//remove item from source box
|
//remove item from source box
|
||||||
await Box.updateOne({ _id: box[0]._id }, { $pull: { item: { _id: itemID } } });
|
await Box.updateOne({ _id: box[0]._id }, { $pull: { item: { _id: itemID } } });
|
||||||
//add item to target box
|
//add item to target box
|
||||||
await Box.updateOne(query, {
|
await Box.updateOne(query, { $push: { item: item[0].item[0] } });
|
||||||
$push: { item: item[0].item[0] }
|
|
||||||
});
|
|
||||||
|
|
||||||
const boxes = await getAllBoxes();
|
const boxes = await getAllBoxes();
|
||||||
response.send(boxes);
|
response.send(boxes);
|
||||||
}
|
}
|
||||||
catch(error){
|
catch (error) {
|
||||||
console.log(error);
|
|
||||||
response.json(error);
|
response.json(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*...*/
|
/*...*/
|
||||||
})
|
})
|
||||||
/*------------/CONTROLLER------------*/
|
/*------------/CONTROLLER------------*/
|
||||||
|
|
||||||
const getAllBoxes = async (queryParameters) => {
|
const getAllBoxes = async (queryParameters) => {
|
||||||
queryParameters = queryParameters ?? {};
|
queryParameters = queryParameters ?? {};
|
||||||
const data = await Box.find(queryParameters).sort({sort: 1});
|
const data = await Box.find(queryParameters).sort({ sort: 1 });
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user