Browse Source

implemented editing of item base data

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/stock.v2
Stephan Richter 3 weeks ago
parent
commit
a001074783
  1. 60
      core/src/main/java/de/srsoftware/umbrella/core/model/Item.java
  2. 15
      frontend/src/routes/stock/ItemProps.svelte
  3. 32
      stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java

60
core/src/main/java/de/srsoftware/umbrella/core/model/Item.java

@ -4,14 +4,11 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*; import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.tools.Mappable; import de.srsoftware.tools.Mappable;
import org.json.JSONObject; import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.*;
import java.util.HashSet; import org.json.JSONObject;
import java.util.Map;
import java.util.Set;
public class Item implements Mappable { public class Item implements Mappable {
private long id; private long id;
@ -30,6 +27,10 @@ public class Item implements Mappable {
this.properties = new HashSet<>(); this.properties = new HashSet<>();
} }
public String code(){
return code;
}
public boolean isDirty(){ public boolean isDirty(){
return !dirtyFields.isEmpty(); return !dirtyFields.isEmpty();
} }
@ -38,6 +39,14 @@ public class Item implements Mappable {
return id; return id;
} }
public Location location(){
return location;
}
public String name(){
return name;
}
public static Item of(ResultSet rs, Mappable owner, Location location) throws SQLException { public static Item of(ResultSet rs, Mappable owner, Location location) throws SQLException {
var id = rs.getLong(ID); var id = rs.getLong(ID);
var code = rs.getString(CODE); var code = rs.getString(CODE);
@ -55,15 +64,16 @@ public class Item implements Mappable {
@Override @Override
public Map<String, Object> toMap() { public Map<String, Object> toMap() {
var ownerMap = owner instanceof Company comp ? Map.of(COMPANY,comp.id()) : (owner instanceof UmbrellaUser u ? Map.of(USER,u.id()) : Map.of()); var ownerId = ownerId();
return Map.of( var ownerMap = ownerId < 0 ? Map.of(COMPANY,-ownerId) : Map.of(USER,ownerId);
OWNER, ownerMap, var map = new HashMap<String,Object>();
ID, id, map.put(OWNER,ownerMap);
LOCATION, location.toMap(), map.put(ID,id);
CODE, code, if (location != null) map.put(LOCATION,location.toMap());
NAME, name, map.put(CODE,code);
PROPERTIES, properties.stream().map(Property::toMap).toList() map.put(NAME,name);
); if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
return map;
} }
@Override @Override
@ -75,6 +85,9 @@ public class Item implements Mappable {
for (var field : json.keySet()){ for (var field : json.keySet()){
var known = true; var known = true;
switch (field) { switch (field) {
case CODE:
code = json.getString(field);
break;
case NAME: case NAME:
name = json.getString(field); name = json.getString(field);
break; break;
@ -85,4 +98,21 @@ public class Item implements Mappable {
} }
return this; return this;
} }
public static Long ownerId(Mappable owner){
return switch (owner) {
case Company comp -> -comp.id();
case UmbrellaUser user -> user.id();
case null, default -> throw UmbrellaException.unprocessable("Item has owner of unknown class ({0})", owner.getClass().getSimpleName());
};
}
public Long ownerId() {
return ownerId(owner);
}
public Item clear() {
dirtyFields.clear();
return this;
}
} }

15
frontend/src/routes/stock/ItemProps.svelte

@ -42,25 +42,30 @@
} else error(res); } else error(res);
} }
async function updateTitle(newVal){ async function patch(key,newVal){
const url = api('stock'); const url = api('stock');
const data = { const data = {
id : item.id, id : item.id,
owner : item.owner, owner : item.owner,
title : newVal };
} data[key] = newVal;
const res = await fetch(url,{ const res = await fetch(url,{
credentials:'include', credentials:'include',
method:'PATCH', method:'PATCH',
body:JSON.stringify(data) body:JSON.stringify(data)
}); });
if (res.ok){
yikes();
return true;
}
error(res);
return false; return false;
} }
</script> </script>
{#if item} {#if item}
<LineEditor type="h3" editable={true} value={item.name} onSet={updateTitle}/> <LineEditor type="h3" editable={true} value={item.name} onSet={v => patch('name',v)} />
Code: <LineEditor type="span" editable={true} value={item.code} onSet={v => patch('code',v)} />
<table> <table>
<tbody> <tbody>
{#each item.properties.toSorted(byName) as prop} {#each item.properties.toSorted(byName) as prop}

32
stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java

@ -209,9 +209,7 @@ public class SqliteDb extends BaseDb implements StockDb {
rs.close(); rs.close();
for (var item : list){ for (var item : list){
var owner = item.owner(); var ownerId = item.ownerId();
var ownerId = owner instanceof Company comp ? -comp.id() : (owner instanceof UmbrellaUser u ? u.id() : 0);
if (ownerId == 0) throw databaseException("Encountered unknown item owner of type {0}",owner.getClass().getSimpleName());
rs = select(ALL).from(TABLE_ITEM_PROPERTIES).leftJoin(PROPERTY_ID,TABLE_PROPERTIES,ID).where(OWNER,equal(ownerId)).where(ITEM_ID,equal(item.id())).exec(db); rs = select(ALL).from(TABLE_ITEM_PROPERTIES).leftJoin(PROPERTY_ID,TABLE_PROPERTIES,ID).where(OWNER,equal(ownerId)).where(ITEM_ID,equal(item.id())).exec(db);
while (rs.next()) item.properties().add(Property.of(rs)); while (rs.next()) item.properties().add(Property.of(rs));
rs.close(); rs.close();
@ -224,10 +222,8 @@ public class SqliteDb extends BaseDb implements StockDb {
@Override @Override
public Item loadItem(Mappable owner, long itemId) { public Item loadItem(Mappable owner, long itemId) {
var ownerId = owner instanceof Company comp ? -comp.id() : (owner instanceof UmbrellaUser u ? u.id() : 0);
if (ownerId == 0) throw databaseException("Failed to load item: unknown owner type ({0})",owner.getClass().getSimpleName());
try { try {
var rs = select(ALL).from(TABLE_ITEMS).where(OWNER,equal(ownerId)).where(ID,equal(itemId)).exec(db); var rs = select(ALL).from(TABLE_ITEMS).where(OWNER,equal(Item.ownerId(owner))).where(ID,equal(itemId)).exec(db);
Item result = null; Item result = null;
if (rs.next()) result = Item.of(rs,owner,null); if (rs.next()) result = Item.of(rs,owner,null);
rs.close(); rs.close();
@ -293,7 +289,29 @@ public class SqliteDb extends BaseDb implements StockDb {
@Override @Override
public Item save(Item item) { public Item save(Item item) {
LOG.log(ERROR,"StockDb.save(…) not implemented!"); if (item.id() == 0){ // TODO
LOG.log(ERROR,"StockDb.save(…) not implemented!");
} else if (item.isDirty()) {
try {
var location = item.location();
var query = update(TABLE_ITEMS).where(OWNER, equal(item.ownerId())).where(ID, equal(item.id()));
if (location == null) {
query.set(CODE,NAME);
} else {
query.set(CODE,NAME,LOCATION_ID);
}
var pq = query.prepare(db);
if (location == null) {
pq.apply(item.code(),item.name());
} else {
pq.apply(item.code(),item.name(),item.location().id());
}
return item.clear();
} catch (SQLException e){
throw databaseException("Failed to update item {0}",item.name());
}
}
return item; return item;
} }

Loading…
Cancel
Save