|
|
|
|
@ -14,6 +14,7 @@ import static java.util.Comparator.comparing;
@@ -14,6 +14,7 @@ import static java.util.Comparator.comparing;
|
|
|
|
|
|
|
|
|
|
import com.sun.net.httpserver.HttpExchange; |
|
|
|
|
import de.srsoftware.configuration.Configuration; |
|
|
|
|
import de.srsoftware.tools.Mappable; |
|
|
|
|
import de.srsoftware.tools.Path; |
|
|
|
|
import de.srsoftware.tools.SessionToken; |
|
|
|
|
import de.srsoftware.umbrella.core.BaseHandler; |
|
|
|
|
@ -62,6 +63,19 @@ public class StockModule extends BaseHandler implements StockService {
@@ -62,6 +63,19 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean doPatch(Path path, HttpExchange ex) throws IOException { |
|
|
|
|
addCors(ex); |
|
|
|
|
try { |
|
|
|
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
|
|
|
|
var user = userService().loadUser(token); |
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
return patchItem(user.get(),ex); |
|
|
|
|
} catch (UmbrellaException e){ |
|
|
|
|
return send(ex,e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean doPost(Path path, HttpExchange ex) throws IOException { |
|
|
|
|
addCors(ex); |
|
|
|
|
@ -130,6 +144,31 @@ public class StockModule extends BaseHandler implements StockService {
@@ -130,6 +144,31 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
return sendContent(ex, result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean patchItem(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.get(ID) instanceof Number id)) throw missingFieldException(ID); |
|
|
|
|
if (!(json.get(OWNER) instanceof JSONObject ownerRef)) throw missingFieldException(OWNER); |
|
|
|
|
json.remove(ID); |
|
|
|
|
json.remove(OWNER); |
|
|
|
|
var owner = toOwner(ownerRef); |
|
|
|
|
|
|
|
|
|
var item = stockDb.loadItem(owner,id.longValue()); |
|
|
|
|
item.patch(json); |
|
|
|
|
return sendContent(ex,stockDb.save(item)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Mappable toOwner(JSONObject owner) { |
|
|
|
|
var keys = owner.keySet(); |
|
|
|
|
if (keys.size() != 1) throw unprocessable("{0} expected to have only one child!",OWNER); |
|
|
|
|
String key = new ArrayList<>(keys).getFirst(); |
|
|
|
|
return switch (key) { |
|
|
|
|
case COMPANY -> companyService().get(owner.getLong(key)); |
|
|
|
|
case USER -> userService().loadUser(owner.getLong(key)); |
|
|
|
|
default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER)); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
@ -139,14 +178,7 @@ public class StockModule extends BaseHandler implements StockService {
@@ -139,14 +178,7 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
if (!propData.has(VALUE)) throw missingFieldException(VALUE); |
|
|
|
|
var value = propData.get(VALUE); |
|
|
|
|
if (value == null) throw missingFieldException(VALUE); |
|
|
|
|
var keys = owner.keySet(); |
|
|
|
|
if (keys.size() != 1) throw unprocessable("{0} expected to have only one child!",OWNER); |
|
|
|
|
String key = new ArrayList<>(keys).getFirst(); |
|
|
|
|
long ownerId = switch (key) { |
|
|
|
|
case COMPANY -> -owner.getLong(key); |
|
|
|
|
case USER -> owner.getLong(key); |
|
|
|
|
default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER)); |
|
|
|
|
}; |
|
|
|
|
var ownerId = toOwnerId(owner); |
|
|
|
|
|
|
|
|
|
Property property = null; |
|
|
|
|
if (propData.get("existing_prop_id") instanceof Number existingPropId && existingPropId.longValue() != 0L){ |
|
|
|
|
@ -160,6 +192,18 @@ public class StockModule extends BaseHandler implements StockService {
@@ -160,6 +192,18 @@ public class StockModule extends BaseHandler implements StockService {
|
|
|
|
|
return sendContent(ex,property); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private long toOwnerId(JSONObject owner) { |
|
|
|
|
var keys = owner.keySet(); |
|
|
|
|
if (keys.size() != 1) throw unprocessable("{0} expected to have only one child!",OWNER); |
|
|
|
|
String key = new ArrayList<>(keys).getFirst(); |
|
|
|
|
return switch (key) { |
|
|
|
|
case COMPANY -> -owner.getLong(key); |
|
|
|
|
case USER -> owner.getLong(key); |
|
|
|
|
default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER)); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Collection<Object> redefineMe(long company_id) { |
|
|
|
|
return List.of(); |
|
|
|
|
|