moving more methods around
This commit is contained in:
+22
-29
@@ -21,15 +21,23 @@ db.once('open', function() {
|
||||
|
||||
/*------------MODELS------------*/
|
||||
//create schema
|
||||
var itemSchema = new mongoose.Schema({
|
||||
id: String,
|
||||
name: String,
|
||||
description: String,
|
||||
quantity: Number
|
||||
});
|
||||
|
||||
var boxSchema = new mongoose.Schema({
|
||||
id: String,
|
||||
name: String,
|
||||
description: String,
|
||||
item: [mongoose.Schema.Types.Mixed],
|
||||
item: [itemSchema],
|
||||
});
|
||||
|
||||
//compile schema into model
|
||||
var Box = mongoose.model('box', boxSchema, 'box');
|
||||
var Item = mongoose.model('item', itemSchema, 'item');
|
||||
/*------------/MODELS------------*/
|
||||
|
||||
/*------------CONTROLLER------------*/
|
||||
@@ -43,20 +51,14 @@ app.get('/box', async (request, response) => {
|
||||
|
||||
// create box
|
||||
app.post('/box', async (request, response) => {
|
||||
Box.create(request.body, (error, document) => {
|
||||
if(error) return request.next(error);
|
||||
return document;
|
||||
});
|
||||
await Box.create(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}, (error, document) => {
|
||||
if(error)return null;
|
||||
return document;
|
||||
});
|
||||
await Box.deleteOne({_id: request.params.id});
|
||||
const updatedBoxes = await getAllBoxes();
|
||||
response.send(updatedBoxes);
|
||||
});
|
||||
@@ -65,24 +67,21 @@ app.delete('/box/:id', async (request, response) => {
|
||||
app.post('/box/:id/item', async (request, response) => {
|
||||
const id = request.params.id;
|
||||
const query = {_id: id};
|
||||
const box = Box.updateOne(query,{
|
||||
const box = await Box.updateOne(query, {
|
||||
$push: { item: request.body }
|
||||
}, (error, document) => {
|
||||
if(error)return null;
|
||||
return document;
|
||||
});
|
||||
const updatedBox = await getBox(id);
|
||||
response.send(updatedBox);
|
||||
});
|
||||
|
||||
//delete item //THIS IS NOT WORKING
|
||||
app.delete('/item/:id', async (request, response) => {
|
||||
Item.deleteOne({_id: request.params.id}, (error, document) => {
|
||||
if(error)return null;
|
||||
return document;
|
||||
});
|
||||
const updatedBoxes = await getAllBoxes();
|
||||
response.send(updatedBoxes);
|
||||
//delete item
|
||||
app.delete('/box/:boxId/item/:itemId', async (request, response) => {
|
||||
const boxID = request.params.boxId;
|
||||
const itemID = request.params.itemId;
|
||||
await Box.update({ _id: boxID }, { $pull: { item: { _id: itemID } } });
|
||||
const box = await getBox(boxID);
|
||||
console.log(box.item);
|
||||
response.send(box.item);
|
||||
});
|
||||
|
||||
|
||||
@@ -90,17 +89,11 @@ app.delete('/item/:id', async (request, response) => {
|
||||
|
||||
const getAllBoxes = async (queryParameters) => {
|
||||
queryParameters = queryParameters ?? {};
|
||||
const data = await Box.find(queryParameters, (error, document) => {
|
||||
if(error) return null;
|
||||
return document;
|
||||
});
|
||||
const data = await Box.find(queryParameters);
|
||||
return data;
|
||||
}
|
||||
|
||||
const getBox = async (id) => {
|
||||
const data = await Box.findById(id, (error, document) => {
|
||||
if(error) return null;
|
||||
return document;
|
||||
});
|
||||
const data = await Box.findById(id);
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user