|
|
|
@ -114,7 +114,15 @@ public class StockModule extends BaseHandler implements StockService { |
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
return switch (path.pop()){ |
|
|
|
return switch (path.pop()){ |
|
|
|
case MOVE_ITEM -> patchMoveItem(user.get(), path,ex); |
|
|
|
case MOVE_ITEM -> patchMoveItem(user.get(), path,ex); |
|
|
|
case MOVE_LOCATION -> patchMoveLocation(user.get(), path, ex); |
|
|
|
case LOCATION -> { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
var id = Long.parseLong(path.pop()); |
|
|
|
|
|
|
|
yield patchLocation(id, user.get(), ex); |
|
|
|
|
|
|
|
} catch (NumberFormatException nfe){ |
|
|
|
|
|
|
|
yield super.doPatch(path,ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
case null -> patchItem(user.get(),ex); |
|
|
|
case null -> patchItem(user.get(),ex); |
|
|
|
default -> super.doPatch(path,ex); |
|
|
|
default -> super.doPatch(path,ex); |
|
|
|
}; |
|
|
|
}; |
|
|
|
@ -233,21 +241,20 @@ public class StockModule extends BaseHandler implements StockService { |
|
|
|
return sendContent(ex,item); |
|
|
|
return sendContent(ex,item); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean patchMoveLocation(UmbrellaUser user, Path path, HttpExchange ex) throws IOException { |
|
|
|
private boolean patchLocation(long locationId, UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
if (!(json.get(LOCATION) instanceof Number locationId)) throw missingFieldException(LOCATION); |
|
|
|
var location = stockDb.loadLocation(locationId); |
|
|
|
if (!(json.get(TARGET) instanceof Number destLocationId)) throw missingFieldException(TARGET); |
|
|
|
|
|
|
|
var location = stockDb.loadLocation(locationId.longValue()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var owner = location.owner().resolve(); |
|
|
|
var owner = location.owner().resolve(); |
|
|
|
if (!assigned(owner,user)) throw forbidden("You are not allowed to alter the location of \"{0}\"!",location.name()); |
|
|
|
if (!assigned(owner,user)) throw forbidden("You are not allowed to edit \"{0}\"!",location.name()); |
|
|
|
|
|
|
|
|
|
|
|
var target = stockDb.loadLocation(destLocationId.longValue()); |
|
|
|
if (json.has(PARENT_LOCATION_ID) && json.get(PARENT_LOCATION_ID) instanceof Number parentId){ |
|
|
|
var locOwner = target.resolve().owner().resolve(); |
|
|
|
var target = stockDb.loadLocation(parentId.longValue()); |
|
|
|
if (!assigned(locOwner,user)) throw forbidden("You are not allowed to modify \"{0}\"!",target.name()); |
|
|
|
var targetOwner = target.owner().resolve(); |
|
|
|
|
|
|
|
if (!assigned(targetOwner,user)) throw forbidden("You are not allowed to edit \"{0}\"!",target.name()); |
|
|
|
|
|
|
|
if (!targetOwner.equals(owner)) throw unprocessable("You may not move locations from one owner ({0}) to another ({1})",owner,targetOwner); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!locOwner.equals(owner)) throw unprocessable("You may not move locations from one owner ({0}) to another ({1})",owner,locOwner); |
|
|
|
location = stockDb.save(location.patch(json)); |
|
|
|
location = stockDb.save(location.parent(target)); |
|
|
|
|
|
|
|
return sendContent(ex,location); |
|
|
|
return sendContent(ex,location); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|