implemented adding/updating of item properties
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.nullable;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
@@ -45,4 +44,9 @@ public class Property implements Mappable {
|
||||
if (unit != null) map.put(UNIT,unit);
|
||||
return map;
|
||||
}
|
||||
|
||||
public Property value(Object newVal){
|
||||
value = newVal;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
var json = await res.json();
|
||||
var dict = {}
|
||||
for (var entry of json.sort((a,b) => b.id - a.id)) dict[entry.name+'.'+entry.unit] = entry;
|
||||
properties = null;
|
||||
properties = Object.values(dict).sort((a,b) => a.name.localeCompare(b.name));
|
||||
yikes();
|
||||
} else error(res);
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
}
|
||||
});
|
||||
|
||||
function byName(a,b){
|
||||
return a.name.localeCompare(b.name);
|
||||
}
|
||||
|
||||
async function onclick(){
|
||||
const url = api('stock/property');
|
||||
const data = {
|
||||
@@ -29,6 +33,10 @@
|
||||
body:JSON.stringify(data)
|
||||
});
|
||||
if (res.ok){
|
||||
const prop = await res.json();
|
||||
const id = prop.id;
|
||||
item.properties = item.properties.filter(p => p.id != id);
|
||||
item.properties.push(prop);
|
||||
yikes();
|
||||
} else error(res);
|
||||
}
|
||||
@@ -39,7 +47,7 @@
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
{#each item.properties as prop}
|
||||
{#each item.properties.toSorted(byName) as prop}
|
||||
<tr>
|
||||
<td>
|
||||
{prop.name}
|
||||
|
||||
@@ -6,15 +6,12 @@ import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.Util;
|
||||
import de.srsoftware.umbrella.core.api.MarkdownService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MarkdownApi extends BaseHandler implements MarkdownService {
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user