implemented editing of item base data
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,14 +4,11 @@ package de.srsoftware.umbrella.core.model;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Item implements Mappable {
|
||||
private long id;
|
||||
@@ -30,6 +27,10 @@ public class Item implements Mappable {
|
||||
this.properties = new HashSet<>();
|
||||
}
|
||||
|
||||
public String code(){
|
||||
return code;
|
||||
}
|
||||
|
||||
public boolean isDirty(){
|
||||
return !dirtyFields.isEmpty();
|
||||
}
|
||||
@@ -38,6 +39,14 @@ public class Item implements Mappable {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Location location(){
|
||||
return location;
|
||||
}
|
||||
|
||||
public String name(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public static Item of(ResultSet rs, Mappable owner, Location location) throws SQLException {
|
||||
var id = rs.getLong(ID);
|
||||
var code = rs.getString(CODE);
|
||||
@@ -55,15 +64,16 @@ public class Item implements Mappable {
|
||||
|
||||
@Override
|
||||
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());
|
||||
return Map.of(
|
||||
OWNER, ownerMap,
|
||||
ID, id,
|
||||
LOCATION, location.toMap(),
|
||||
CODE, code,
|
||||
NAME, name,
|
||||
PROPERTIES, properties.stream().map(Property::toMap).toList()
|
||||
);
|
||||
var ownerId = ownerId();
|
||||
var ownerMap = ownerId < 0 ? Map.of(COMPANY,-ownerId) : Map.of(USER,ownerId);
|
||||
var map = new HashMap<String,Object>();
|
||||
map.put(OWNER,ownerMap);
|
||||
map.put(ID,id);
|
||||
if (location != null) map.put(LOCATION,location.toMap());
|
||||
map.put(CODE,code);
|
||||
map.put(NAME,name);
|
||||
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,6 +85,9 @@ public class Item implements Mappable {
|
||||
for (var field : json.keySet()){
|
||||
var known = true;
|
||||
switch (field) {
|
||||
case CODE:
|
||||
code = json.getString(field);
|
||||
break;
|
||||
case NAME:
|
||||
name = json.getString(field);
|
||||
break;
|
||||
@@ -85,4 +98,21 @@ public class Item implements Mappable {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,25 +42,30 @@
|
||||
} else error(res);
|
||||
}
|
||||
|
||||
async function updateTitle(newVal){
|
||||
async function patch(key,newVal){
|
||||
const url = api('stock');
|
||||
const data = {
|
||||
id : item.id,
|
||||
owner : item.owner,
|
||||
title : newVal
|
||||
}
|
||||
};
|
||||
data[key] = newVal;
|
||||
const res = await fetch(url,{
|
||||
credentials:'include',
|
||||
method:'PATCH',
|
||||
body:JSON.stringify(data)
|
||||
});
|
||||
if (res.ok){
|
||||
yikes();
|
||||
return true;
|
||||
}
|
||||
error(res);
|
||||
return false;
|
||||
}
|
||||
</script>
|
||||
|
||||
{#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>
|
||||
<tbody>
|
||||
{#each item.properties.toSorted(byName) as prop}
|
||||
|
||||
@@ -209,9 +209,7 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
rs.close();
|
||||
|
||||
for (var item : list){
|
||||
var owner = item.owner();
|
||||
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());
|
||||
var ownerId = item.ownerId();
|
||||
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));
|
||||
rs.close();
|
||||
@@ -224,10 +222,8 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
|
||||
@Override
|
||||
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 {
|
||||
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;
|
||||
if (rs.next()) result = Item.of(rs,owner,null);
|
||||
rs.close();
|
||||
@@ -293,7 +289,29 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user