Browse Source

implemented creating new stock items

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 1 week ago
parent
commit
3caf51f52d
  1. 1
      core/src/main/java/de/srsoftware/umbrella/core/model/DbLocation.java
  2. 12
      core/src/main/java/de/srsoftware/umbrella/core/model/Item.java
  3. 8
      frontend/src/routes/stock/ItemList.svelte
  4. 26
      stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java
  5. 3
      stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java
  6. 8
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

1
core/src/main/java/de/srsoftware/umbrella/core/model/DbLocation.java

@ -90,6 +90,7 @@ public class DbLocation extends Location { @@ -90,6 +90,7 @@ public class DbLocation extends Location {
@Override
public Map<String, Object> toMap() {
var map = super.toMap();
if (description == null) description = "";
map.put(OWNER,owner.toMap());
map.put(NAME,name);
map.put(DESCRIPTION,Map.of(SOURCE,description,RENDERED,markdown(description)));

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

@ -18,7 +18,7 @@ public class Item implements Mappable { @@ -18,7 +18,7 @@ public class Item implements Mappable {
private Collection<Property> properties;
private Set<String> dirtyFields = new HashSet<>();
private Item(long id, Owner owner, long ownerNumber, Location location, String code, String name) {
public Item(long id, Owner owner, long ownerNumber, Location location, String code, String name) {
this.id = id;
this.owner = owner;
this.ownerNumber = ownerNumber;
@ -45,6 +45,11 @@ public class Item implements Mappable { @@ -45,6 +45,11 @@ public class Item implements Mappable {
return id;
}
public Item id(long newVal) {
id = newVal;
return this;
}
public Location location(){
return location;
}
@ -73,6 +78,10 @@ public class Item implements Mappable { @@ -73,6 +78,10 @@ public class Item implements Mappable {
return owner;
}
public long ownerNumber(){
return ownerNumber;
}
public Item patch(JSONObject json) {
for (var field : json.keySet()){
var known = true;
@ -103,6 +112,7 @@ public class Item implements Mappable { @@ -103,6 +112,7 @@ public class Item implements Mappable {
map.put(LOCATION,location.toMap());
map.put(CODE,code);
map.put(NAME,name);
map.put(OWNER_NUMBER,ownerNumber);
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
return map;
}

8
frontend/src/routes/stock/ItemList.svelte

@ -8,11 +8,13 @@ @@ -8,11 +8,13 @@
async function saveNewItem(){
newItem.location = location;
console.log(JSON.parse(JSON.stringify(newItem)));
const url = api('stock/item');
const res = post(url,newItem);
const res = await post(url,newItem);
if (res.ok) {
yikes();
const it = await res.json();
console.log(it);
items = [...items, it];
} else error(res);
}
</script>
@ -27,7 +29,7 @@ @@ -27,7 +29,7 @@
<tbody>
{#each items as item}
<tr onclick={ev => selected = item} ondragstart={e => drag_start(item)} draggable="true">
<td>{item.id}</td>
<td title={`db id: ${item.id}`}>{item.owner_number}</td>
<td>{item.code}</td>
<td>{item.name}</td>
</tr>

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

@ -16,6 +16,7 @@ import static java.text.MessageFormat.format; @@ -16,6 +16,7 @@ import static java.text.MessageFormat.format;
import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.model.*;
import de.srsoftware.umbrella.core.model.Location;
import java.sql.Connection;
@ -272,6 +273,18 @@ public class SqliteDb extends BaseDb implements StockDb { @@ -272,6 +273,18 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
public long nextItemNumberFor(Owner owner) {
try {
var rs = select("max(owner_number)").from(TABLE_ITEMS).where(OWNER,equal(owner.dbCode())).exec(db);
long number = rs.next() ? rs.getLong(1) : 0;
rs.close();
return number +1L;
} catch (SQLException e) {
throw databaseException("Failed to read last item number for {0}",owner);
}
}
private void replaceItemsTable() throws SQLException {
db.prepareStatement(format("DROP TABLE {0}",TABLE_ITEMS)).execute();
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","items_temp",TABLE_ITEMS)).execute();
@ -320,8 +333,17 @@ public class SqliteDb extends BaseDb implements StockDb { @@ -320,8 +333,17 @@ public class SqliteDb extends BaseDb implements StockDb {
@Override
public Item save(Item item) {
if (item.id() == 0){ // TODO
LOG.log(ERROR,"StockDb.save(…) not implemented!");
if (item.id() == 0){
try {
var rs = insertInto(TABLE_ITEMS, OWNER, OWNER_NUMBER, CODE, NAME, LOCATION_ID)
.values(item.owner().dbCode(), item.ownerNumber(), item.code(), item.name(), item.location().id())
.execute(db).getGeneratedKeys();
if (rs.next()) item.id(rs.getLong(1));
rs.close();
return item;
} catch (SQLException e) {
throw databaseException("Failed to save new item to database!");
}
} else if (item.isDirty()) {
try {
var location = item.location();

3
stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.stock;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.model.*;
import de.srsoftware.umbrella.core.model.Location;
import java.util.Collection;
@ -18,4 +19,6 @@ public interface StockDb { @@ -18,4 +19,6 @@ public interface StockDb {
DbLocation save(DbLocation location);
Item save(Item item);
Property setProperty(long itemId, long existingPropId, Object value);
long nextItemNumberFor(Owner owner);
}

8
stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

@ -267,7 +267,13 @@ public class StockModule extends BaseHandler implements StockService { @@ -267,7 +267,13 @@ public class StockModule extends BaseHandler implements StockService {
var json = json(ex);
if (!json.has(NAME) || !(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
if (!json.has(CODE) || !(json.get(CODE) instanceof String code)) throw missingFieldException(CODE);
if (!json.has(LOCATION) || !(json.get(LOCATION) instanceof JSONObject locationData)) throw missingFieldException(LOCATION);
var location = stockDb.loadLocation(locationData.getLong(ID));
var owner = location.owner().resolve();
if (!assigned(owner,user)) throw forbidden("You are not allowed to add items to {0}!",location);
var number = stockDb.nextItemNumberFor(owner);
var newItem = new Item(0,owner,number,location,code,name);
return sendContent(ex,stockDb.save(newItem));
}
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {

Loading…
Cancel
Save