drag drop sort swap
This commit is contained in:
+14
-5
@@ -22,17 +22,16 @@ 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: [itemSchema],
|
||||
sort: Number,
|
||||
});
|
||||
|
||||
//compile schema into model
|
||||
@@ -51,7 +50,9 @@ app.get('/box', async (request, response) => {
|
||||
|
||||
// create box
|
||||
app.post('/box', async (request, response) => {
|
||||
await Box.create(request.body);
|
||||
const numBoxes = await Box.countDocuments({});
|
||||
const query = Object.assign({sort: numBoxes}, request.body);
|
||||
await Box.create(query);
|
||||
const updatedBoxes = await getAllBoxes();
|
||||
response.send(updatedBoxes);
|
||||
});
|
||||
@@ -68,7 +69,15 @@ app.delete('/box/:id', async (request, response) => {
|
||||
const updatedBoxes = await getAllBoxes();
|
||||
response.send(updatedBoxes);
|
||||
});
|
||||
|
||||
//change box sort
|
||||
app.put('/box/:id/sort', async (request, response) => {
|
||||
const source = await getBox(request.params.id);
|
||||
const target = request.body;
|
||||
await Box.updateOne({ _id: source._id }, { $set: { sort: target.sort } });
|
||||
await Box.updateOne({ _id: target._id }, { $set: { sort: source.sort } });
|
||||
const updatedBoxes = await getAllBoxes();
|
||||
response.send(updatedBoxes);
|
||||
})
|
||||
//create item
|
||||
app.post('/box/:id/item', async (request, response) => {
|
||||
const id = request.params.id;
|
||||
@@ -106,7 +115,7 @@ app.delete('/box/:boxId/item/:itemId', async (request, response) => {
|
||||
|
||||
const getAllBoxes = async (queryParameters) => {
|
||||
queryParameters = queryParameters ?? {};
|
||||
const data = await Box.find(queryParameters);
|
||||
const data = await Box.find(queryParameters).sort({sort: 1});
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user