|
|
|
|
@@ -113,7 +113,8 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
var user = userService().loadUser(token);
|
|
|
|
|
if (user.isEmpty()) return unauthorized(ex);
|
|
|
|
|
return switch (path.pop()){
|
|
|
|
|
case MOVE_ITEM -> patchMove(user.get(), path,ex);
|
|
|
|
|
case MOVE_ITEM -> patchMoveItem(user.get(), path,ex);
|
|
|
|
|
case MOVE_LOCATION -> patchMoveLocation(user.get(), path, ex);
|
|
|
|
|
case null -> patchItem(user.get(),ex);
|
|
|
|
|
default -> super.doPatch(path,ex);
|
|
|
|
|
};
|
|
|
|
|
@@ -213,7 +214,7 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
return sendContent(ex,stockDb.save(item));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean patchMove(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
|
|
|
|
|
private boolean patchMoveItem(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
|
|
|
|
|
var json = json(ex);
|
|
|
|
|
if (!(json.get(ITEM) instanceof Number itemId)) throw missingFieldException(ITEM);
|
|
|
|
|
if (!(json.get(TARGET) instanceof Number locationId)) throw missingFieldException(TARGET);
|
|
|
|
|
@@ -232,6 +233,24 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
return sendContent(ex,item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean patchMoveLocation(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
|
|
|
|
|
var json = json(ex);
|
|
|
|
|
if (!(json.get(LOCATION) instanceof Number locationId)) throw missingFieldException(LOCATION);
|
|
|
|
|
if (!(json.get(TARGET) instanceof Number destLocationId)) throw missingFieldException(TARGET);
|
|
|
|
|
var location = stockDb.loadLocation(locationId.longValue());
|
|
|
|
|
|
|
|
|
|
var owner = location.owner().resolve();
|
|
|
|
|
if (!assigned(owner,user)) throw forbidden("You are not allowed to alter the location of \"{0}\"!",location.name());
|
|
|
|
|
|
|
|
|
|
var target = stockDb.loadLocation(locationId.longValue());
|
|
|
|
|
var locOwner = target.resolve().owner().resolve();
|
|
|
|
|
if (!assigned(locOwner,user)) throw forbidden("You are not allowed to modify \"{0}\"!",target.name());
|
|
|
|
|
|
|
|
|
|
if (!locOwner.equals(owner)) throw unprocessable("You may not move locations from one owner ({0}) to another ({1})",owner,locOwner);
|
|
|
|
|
location = stockDb.save(location.parent(target));
|
|
|
|
|
return sendContent(ex,location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
|
|
|
|
|
var json = json(ex);
|
|
|
|
|
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
|
|
|
|
|
|