implemented creating new stock items
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
|
||||
@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();
|
||||
|
||||
@@ -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 {
|
||||
DbLocation save(DbLocation location);
|
||||
Item save(Item item);
|
||||
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);
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user