implemented cloning of stock items. NEXT: update of GUI via message bus
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -27,6 +27,7 @@ import de.srsoftware.umbrella.core.api.Owner;
|
||||
import de.srsoftware.umbrella.core.api.StockService;
|
||||
import de.srsoftware.umbrella.core.constants.Field;
|
||||
import de.srsoftware.umbrella.core.constants.Path;
|
||||
import de.srsoftware.umbrella.core.constants.Text;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
@@ -190,6 +191,7 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case Path.CLONE -> postClone(user.get(),ex);
|
||||
case Path.ITEM -> postItem(user.get(), ex);
|
||||
case LIST -> postItemList(user.get(), path, ex);
|
||||
case Path.LOCATION -> postLocation(user.get(),ex);
|
||||
@@ -285,7 +287,7 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
var json = json(ex);
|
||||
if (!(json.get(ID) instanceof Number id)) throw missingField(ID);
|
||||
json.remove(ID);
|
||||
|
||||
LOG.log(WARNING,"Missing permission check in StockModule.patchItem()!");
|
||||
var item = stockDb.loadItem(id.longValue());
|
||||
item.patch(json);
|
||||
return sendContent(ex,stockDb.save(item));
|
||||
@@ -336,6 +338,21 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
return sendContent(ex,location);
|
||||
}
|
||||
|
||||
private boolean postClone(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!json.has(ID))throw missingField(ID);
|
||||
if (!(json.get(ID) instanceof Number num)) throw invalidField(ID,Text.NUMBER);
|
||||
long itemId = num.longValue();
|
||||
var item = stockDb.loadItem(itemId);
|
||||
stockDb.loadProperties(item);
|
||||
var location = item.location().resolve();
|
||||
var owner = location.owner().resolve();
|
||||
if (!assigned(owner,user)) throw forbidden("You are not allowed to add items to \"{location}\"!", Text.LOCATION,location.name());
|
||||
var newItem = new Item(0,owner,0,location,item.code(),item.name(),item.description());
|
||||
for (var property : item.properties()) newItem.properties().add(property);
|
||||
return sendContent(ex,stockDb.save(newItem));
|
||||
}
|
||||
|
||||
private boolean postItem(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!json.has(NAME) || !(json.get(NAME) instanceof String name)) throw missingField(NAME);
|
||||
@@ -395,6 +412,7 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
if (!(itemData.get(ID) instanceof Number itemId)) throw missingField(ID);
|
||||
if (!(json.get("add_prop") instanceof JSONObject propData)) throw missingField("add_prop");
|
||||
if (!propData.has(VALUE)) throw missingField(VALUE);
|
||||
LOG.log(WARNING,"Missing permission check in StockModule.postProperty()!");
|
||||
var value = propData.get(VALUE);
|
||||
if (value == null) throw missingField(VALUE);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user