preparing to store new locations

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-20 23:17:54 +02:00
parent 6158fb2630
commit 4e9c8c0f69
5 changed files with 59 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ public class Constants {
private Constants(){}
public static final String BELOW = "below";
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
public static final String ITEM = "item";
@@ -13,6 +14,7 @@ public class Constants {
public static final String LOCATIONS = "locations";
public static final String MOVE_ITEM = "move_item";
public static final String OF_USER = "of_user";
public static final String PARENT = "parent";
public static final String PROPERTY_ID = "prop_id";
public static final String TABLE_ITEMS = "items";
public static final String TABLE_ITEM_PROPERTIES = "item_props";

View File

@@ -99,6 +99,7 @@ public class StockModule extends BaseHandler implements StockService {
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
case LOCATION -> postLocation(user.get(),ex);
case PROPERTY -> postProperty(user.get(),ex);
case null, default -> super.doPost(path,ex);
};
@@ -149,7 +150,7 @@ public class StockModule extends BaseHandler implements StockService {
var result = new ArrayList<Object>();
var userLocations = stockDb.listUserLocations(user);
result.add(Map.of(
OWNER, Map.of(USER, user.id()),
PARENT, Map.of(USER, user.id()),
NAME,user.name(),
LOCATIONS,userLocations.stream().map(DbLocation::toMap).toList()));
@@ -157,7 +158,7 @@ public class StockModule extends BaseHandler implements StockService {
companies.values().stream().sorted(comparing(a -> a.name().toLowerCase())).forEach(company -> {
var locations = stockDb.listCompanyLocations(company);
result.add(Map.of(
COMPANY, company.id(),
PARENT, Map.of(COMPANY, company.id()),
NAME,company.name(),
LOCATIONS,locations.stream().sorted(comparing(a -> a.name().toLowerCase())).map(DbLocation::toMap).toList()));
@@ -199,6 +200,33 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,item);
}
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
if (!(json.get(PARENT) instanceof JSONObject parentData)) throw missingFieldException(PARENT);
var key = parentData.keySet().stream().findFirst().orElseThrow(() -> missingFieldException(PARENT));
if (!(parentData.get(key) instanceof Number id)) throw missingFieldException(key);
Location parent;
Owner owner;
switch (key){
case COMPANY:
owner = companyService().get(id.longValue());
parent = null;
break;
case USER:
owner = userService().loadUser(id.longValue());
parent = null;
break;
case LOCATION:
parent = stockDb.loadLocation(id.longValue());
owner = parent.resolve().owner().resolve();
break;
default: throw unprocessable("Unknown parent object: {0} → {1}",key,id);
};
var loc = new DbLocation(0,owner,parent == null?null:parent.id(),name,null);
return sendContent(ex,stockDb.save(loc));
}
private boolean postProperty(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM);