|
|
|
|
@ -99,6 +99,7 @@ public class StockModule extends BaseHandler implements StockService {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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); |
|
|
|
|
|