preparing direct loading of items

current state:
- loading of item implemented on backend
- need to post path information
- could be resolved re-using _getLocationEntities_, but that would include the full data of all items at that location, which is probably not required

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-11-17 00:37:45 +01:00
parent a4488a2fae
commit 0f7fb99eaa
4 changed files with 75 additions and 7 deletions

View File

@@ -211,11 +211,7 @@ public class SqliteDb extends BaseDb implements StockDb {
while (rs.next()) list.add(Item.of(rs));
rs.close();
for (var item : list){
rs = select(ALL).from(TABLE_ITEM_PROPERTIES).leftJoin(PROPERTY_ID,TABLE_PROPERTIES,ID).where(ITEM_ID,equal(item.id())).exec(db);
while (rs.next()) item.properties().add(Property.of(rs));
rs.close();
}
for (var item : list) loadProperties(item);
return list;
} catch (SQLException e){
throw databaseException("Failed to load items at {0}",location);
@@ -223,9 +219,32 @@ public class SqliteDb extends BaseDb implements StockDb {
}
@Override
public Item loadItem(long id) {
public Item loadProperties(Item item){
try {
var rs = select(ALL).from(TABLE_ITEMS).where(ID,equal(id)).exec(db);
var rs = select(ALL).from(TABLE_ITEM_PROPERTIES).leftJoin(PROPERTY_ID, TABLE_PROPERTIES, ID).where(ITEM_ID, equal(item.id())).exec(db);
while (rs.next()) item.properties().add(Property.of(rs));
rs.close();
return item;
} catch (SQLException e){
throw databaseException("Failed to load properties of {0}",item.name());
}
}
@Override
public Item loadItem(long id) {
var query = select(ALL).from(TABLE_ITEMS).where(ID,equal(id));
return loadItem(query);
}
@Override
public Item loadItem(String owner, long itemNumber) {
var query = select(ALL).from(TABLE_ITEMS).where(OWNER,equal(owner)).where(OWNER_NUMBER,equal(itemNumber));
return loadItem(query);
}
private Item loadItem(SelectQuery query){
try {
var rs = query.exec(db);
Item result = null;
if (rs.next()) result = Item.of(rs);
rs.close();

View File

@@ -16,7 +16,9 @@ public interface StockDb {
Collection<Property> listProperties();
Collection<DbLocation> listUserLocations(UmbrellaUser userId);
Item loadItem(long id);
Item loadItem(String owner, long itemNumber);
DbLocation loadLocation(long locationId);
Item loadProperties(Item item);
long nextItemNumberFor(Owner owner);
Map<String,Object> pathToLocation(Location location);
DbLocation save(DbLocation location);

View File

@@ -90,6 +90,14 @@ public class StockModule extends BaseHandler implements StockService {
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
case COMPANY -> {
try {
var company = companyService().get(Long.parseLong(path.pop()));
yield getItem(user.get(),company,path,ex);
} catch (NumberFormatException e){
yield super.doGet(path,ex);
}
}
case LOCATION -> {
try {
var location = Location.of(Long.parseLong(path.pop()));
@@ -100,6 +108,15 @@ public class StockModule extends BaseHandler implements StockService {
}
case LOCATIONS -> getLocations(path,user.get(),ex);
case PROPERTIES -> getProperties(ex);
case USER -> {
try {
var userId = Long.parseLong(path.pop());
if (userId != user.get().id()) throw forbidden("You are not allowed to access items of another user!");
yield getItem(user.get(),user.get(),path,ex);
} catch (NumberFormatException e){
yield super.doGet(path,ex);
}
}
case null, default -> super.doGet(path,ex);
};
} catch (UmbrellaException e){
@@ -157,6 +174,23 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex, stockDb.listChildLocations(parentId).stream().sorted(comparing(l -> l.name().toLowerCase())).map(DbLocation::toMap));
}
private boolean getItem(UmbrellaUser user, Owner owner, Path path, HttpExchange ex) throws IOException {
if (!assigned(owner,user)) throw forbidden("You are not allowed to access items of {0}",owner);
return switch (path.pop()){
case ITEM -> {
try {
var itemId = Long.parseLong(path.pop());
var item = stockDb.loadItem(owner.dbCode(),itemId);
stockDb.loadProperties(item);
yield sendContent(ex,item);
} catch (NumberFormatException e) {
yield super.doGet(path,ex);
}
}
case null, default -> super.doGet(path,ex);
};
}
private boolean getLocationEntities(Location location, HttpExchange ex) throws IOException {
var items = stockDb.listItemsAt(location).stream().map(Item::toMap).toList();
var owner = location.resolve().owner();