|
|
|
|
@ -8,6 +8,8 @@ import static de.srsoftware.umbrella.core.Constants.*;
@@ -8,6 +8,8 @@ import static de.srsoftware.umbrella.core.Constants.*;
|
|
|
|
|
import static de.srsoftware.umbrella.core.Field.ITEM; |
|
|
|
|
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService; |
|
|
|
|
import static de.srsoftware.umbrella.core.ModuleRegistry.userService; |
|
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
|
import static de.srsoftware.umbrella.core.Util.mapValues; |
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; |
|
|
|
|
import static de.srsoftware.umbrella.stock.Constants.*; |
|
|
|
|
import static java.lang.System.Logger.Level.WARNING; |
|
|
|
|
@ -33,6 +35,7 @@ import org.json.JSONObject;
@@ -33,6 +35,7 @@ import org.json.JSONObject;
|
|
|
|
|
public class StockModule extends BaseHandler implements StockService { |
|
|
|
|
|
|
|
|
|
private final StockDb stockDb; |
|
|
|
|
private Comparator<Item> byName = (a,b) -> a.name().compareToIgnoreCase(b.name()); |
|
|
|
|
|
|
|
|
|
public StockModule(Configuration config) throws UmbrellaException { |
|
|
|
|
super(); |
|
|
|
|
@ -160,6 +163,7 @@ public class StockModule extends BaseHandler implements StockService {
@@ -160,6 +163,7 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
var head = path.pop(); |
|
|
|
|
return switch (head) { |
|
|
|
|
case ITEM -> postItem(user.get(), ex); |
|
|
|
|
case LIST -> postItemList(user.get(), path, ex); |
|
|
|
|
case LOCATION -> postLocation(user.get(),ex); |
|
|
|
|
case PROPERTY -> postProperty(user.get(),ex); |
|
|
|
|
case null, default -> super.doPost(path,ex); |
|
|
|
|
@ -316,6 +320,15 @@ public class StockModule extends BaseHandler implements StockService {
@@ -316,6 +320,15 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
return sendContent(ex,stockDb.save(newItem)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean postItemList(UmbrellaUser user, Path path, HttpExchange ex) throws IOException { |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!json.has(COMPANY_ID) || !(json.get(COMPANY_ID) instanceof Number company_id)) throw missingFieldException(COMPANY_ID); |
|
|
|
|
var company = companyService().get(company_id.longValue()); |
|
|
|
|
if (!companyService().membership(company_id.longValue(),user.id())) throw forbidden("You are not a member of {0}!", company.name()); |
|
|
|
|
var items = stockDb.listItemsOf(company); |
|
|
|
|
return sendContent(ex,items.stream().sorted(byName).map(Item::toMap)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME); |
|
|
|
|
|