implemented creating new stock items
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -90,6 +90,7 @@ public class DbLocation extends Location {
|
|||||||
@Override
|
@Override
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
var map = super.toMap();
|
var map = super.toMap();
|
||||||
|
if (description == null) description = "";
|
||||||
map.put(OWNER,owner.toMap());
|
map.put(OWNER,owner.toMap());
|
||||||
map.put(NAME,name);
|
map.put(NAME,name);
|
||||||
map.put(DESCRIPTION,Map.of(SOURCE,description,RENDERED,markdown(description)));
|
map.put(DESCRIPTION,Map.of(SOURCE,description,RENDERED,markdown(description)));
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class Item implements Mappable {
|
|||||||
private Collection<Property> properties;
|
private Collection<Property> properties;
|
||||||
private Set<String> dirtyFields = new HashSet<>();
|
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.id = id;
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.ownerNumber = ownerNumber;
|
this.ownerNumber = ownerNumber;
|
||||||
@@ -45,6 +45,11 @@ public class Item implements Mappable {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Item id(long newVal) {
|
||||||
|
id = newVal;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Location location(){
|
public Location location(){
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
@@ -73,6 +78,10 @@ public class Item implements Mappable {
|
|||||||
return owner;
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long ownerNumber(){
|
||||||
|
return ownerNumber;
|
||||||
|
}
|
||||||
|
|
||||||
public Item patch(JSONObject json) {
|
public Item patch(JSONObject json) {
|
||||||
for (var field : json.keySet()){
|
for (var field : json.keySet()){
|
||||||
var known = true;
|
var known = true;
|
||||||
@@ -103,6 +112,7 @@ public class Item implements Mappable {
|
|||||||
map.put(LOCATION,location.toMap());
|
map.put(LOCATION,location.toMap());
|
||||||
map.put(CODE,code);
|
map.put(CODE,code);
|
||||||
map.put(NAME,name);
|
map.put(NAME,name);
|
||||||
|
map.put(OWNER_NUMBER,ownerNumber);
|
||||||
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
|
if (properties != null) map.put(PROPERTIES,properties.stream().map(Property::toMap).toList());
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,13 @@
|
|||||||
|
|
||||||
async function saveNewItem(){
|
async function saveNewItem(){
|
||||||
newItem.location = location;
|
newItem.location = location;
|
||||||
console.log(JSON.parse(JSON.stringify(newItem)));
|
|
||||||
const url = api('stock/item');
|
const url = api('stock/item');
|
||||||
const res = post(url,newItem);
|
const res = await post(url,newItem);
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
yikes();
|
yikes();
|
||||||
|
const it = await res.json();
|
||||||
|
console.log(it);
|
||||||
|
items = [...items, it];
|
||||||
} else error(res);
|
} else error(res);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -27,7 +29,7 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{#each items as item}
|
{#each items as item}
|
||||||
<tr onclick={ev => selected = item} ondragstart={e => drag_start(item)} draggable="true">
|
<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.code}</td>
|
||||||
<td>{item.name}</td>
|
<td>{item.name}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import static java.text.MessageFormat.format;
|
|||||||
|
|
||||||
import de.srsoftware.tools.jdbc.Query;
|
import de.srsoftware.tools.jdbc.Query;
|
||||||
import de.srsoftware.umbrella.core.BaseDb;
|
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.*;
|
||||||
import de.srsoftware.umbrella.core.model.Location;
|
import de.srsoftware.umbrella.core.model.Location;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -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 {
|
private void replaceItemsTable() throws SQLException {
|
||||||
db.prepareStatement(format("DROP TABLE {0}",TABLE_ITEMS)).execute();
|
db.prepareStatement(format("DROP TABLE {0}",TABLE_ITEMS)).execute();
|
||||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","items_temp",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 {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item save(Item item) {
|
public Item save(Item item) {
|
||||||
if (item.id() == 0){ // TODO
|
if (item.id() == 0){
|
||||||
LOG.log(ERROR,"StockDb.save(…) not implemented!");
|
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()) {
|
} else if (item.isDirty()) {
|
||||||
try {
|
try {
|
||||||
var location = item.location();
|
var location = item.location();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/* © SRSoftware 2025 */
|
/* © SRSoftware 2025 */
|
||||||
package de.srsoftware.umbrella.stock;
|
package de.srsoftware.umbrella.stock;
|
||||||
|
|
||||||
|
import de.srsoftware.umbrella.core.api.Owner;
|
||||||
import de.srsoftware.umbrella.core.model.*;
|
import de.srsoftware.umbrella.core.model.*;
|
||||||
import de.srsoftware.umbrella.core.model.Location;
|
import de.srsoftware.umbrella.core.model.Location;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -18,4 +19,6 @@ public interface StockDb {
|
|||||||
DbLocation save(DbLocation location);
|
DbLocation save(DbLocation location);
|
||||||
Item save(Item item);
|
Item save(Item item);
|
||||||
Property setProperty(long itemId, long existingPropId, Object value);
|
Property setProperty(long itemId, long existingPropId, Object value);
|
||||||
|
|
||||||
|
long nextItemNumberFor(Owner owner);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,7 +267,13 @@ public class StockModule extends BaseHandler implements StockService {
|
|||||||
var json = json(ex);
|
var json = json(ex);
|
||||||
if (!json.has(NAME) || !(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
|
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(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 {
|
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||||
|
|||||||
Reference in New Issue
Block a user