implemented adding/updating of item properties

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-15 09:26:16 +02:00
parent 846ef4a27a
commit 832be23e8c
7 changed files with 49 additions and 23 deletions

View File

@@ -51,17 +51,34 @@ public class SqliteDb extends BaseDb implements StockDb {
}
@Override
public void addProperty(long ownerId, long itemId, long existingPropId, Object value) {
public Property addProperty(long ownerId, long itemId, long existingPropId, Object value) {
try {
insertInto(TABLE_ITEM_PROPERTIES,OWNER,ITEM_ID,PROPERTY_ID,VALUE).values(ownerId,itemId,existingPropId,value).execute(db);
Property prop = null;
var rs = select(ALL).from(TABLE_PROPERTIES).where(ID,equal(existingPropId)).exec(db);
if (rs.next()) prop = Property.of(rs);
rs.close();
if (prop == null) throw databaseException("Failed to add new property to item {0}",itemId);
replaceInto(TABLE_ITEM_PROPERTIES,OWNER,ITEM_ID,PROPERTY_ID,VALUE).values(ownerId,itemId,existingPropId,value).execute(db);
return prop.value(value);
} catch (SQLException e) {
throw databaseException("Failed to add new property to item {0}",itemId);
}
}
@Override
public void addProperty(long ownerId, long itemId, String name, Object value, String unit) {
// TODO
public Property addNewProperty(long ownerId, long itemId, String name, Object value, String unit) {
try {
db.setAutoCommit(false);
var rs = insertInto(TABLE_PROPERTIES,NAME,TYPE,UNIT).values(name,0,unit).execute(db).getGeneratedKeys();
Long propertyId = null;
if (rs.next()) propertyId = rs.getLong(1);
rs.close();
if (propertyId == null || propertyId == 0) throw databaseException("Failed to create new property {0} to DB",name);
insertInto(TABLE_ITEM_PROPERTIES,OWNER,ITEM_ID,PROPERTY_ID,VALUE).values(ownerId,itemId,propertyId,value).execute(db);
return new Property(propertyId,name,value,unit);
} catch (SQLException e) {
throw databaseException("Failed to create new property {0} to DB",name);
}
}
private void createIntermediateItemsTable() throws SQLException { // create intermediate table

View File

@@ -2,12 +2,11 @@
package de.srsoftware.umbrella.stock;
import de.srsoftware.umbrella.core.model.*;
import java.util.Collection;
public interface StockDb {
void addProperty(long ownerId, long itemId, long existingPropId, Object value);
void addProperty(long ownerId, long itemId, String name, Object value, String unit);
Property addProperty(long ownerId, long itemId, long existingPropId, Object value);
Property addNewProperty(long ownerId, long itemId, String name, Object value, String unit);
Collection<Location> listChildLocations(long parentId);
Collection<Location> listCompanyLocations(Company company);
Collection<Item> listItemsAt(long locationId);

View File

@@ -21,10 +21,9 @@ import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.api.StockService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.*;
import org.json.JSONObject;
import java.io.IOException;
import java.util.*;
import org.json.JSONObject;
public class StockModule extends BaseHandler implements StockService {
@@ -133,31 +132,32 @@ public class StockModule extends BaseHandler implements StockService {
private boolean postProperty(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(ID) instanceof Number id)) throw missingFieldException(ID);
if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM);
if (!(itemData.get(OWNER) instanceof JSONObject owner)) throw missingFieldException(OWNER);
if (!(itemData.get(ID) instanceof Number itemId)) throw missingFieldException(ID);
if (!(json.get("add_prop") instanceof JSONObject propData)) throw missingFieldException("add_prop");
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);
var key = new ArrayList<>(keys).getFirst();
var ownerId = switch (key) {
case COMPANY -> -json.getLong(key);
case USER -> json.getLong(key);
case null, default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER));
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));
};
Property property = null;
if (propData.get("existing_prop_id") instanceof Number existingPropId && existingPropId.longValue() != 0L){
stockDb.addProperty(ownerId,id.longValue(),existingPropId.longValue(),value);
property = stockDb.addProperty(ownerId,itemId.longValue(),existingPropId.longValue(),value);
} else {
if (!(propData.get("new_prop") instanceof JSONObject newProp)) throw unprocessable("data must contain either add_prop.existing_prop_id or add_prop.new_prop!");
if (!(newProp.get(NAME) instanceof String name) || name.isBlank()) throw unprocessable("data.add_prop.new_prop does not contain name!");
var unit = newProp.get(UNIT) instanceof String u ? nullIfEmpty(u) : null;
stockDb.addProperty(ownerId,id.longValue(),name,value,unit);
property = stockDb.addNewProperty(ownerId,itemId.longValue(),name,value,unit);
}
return sendEmptyResponse(500,ex);
return sendContent(ex,property);
}
@Override