Browse Source

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>
module/stock
Stephan Richter 1 week ago
parent
commit
0f7fb99eaa
  1. 13
      frontend/src/routes/stock/Index.svelte
  2. 31
      stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java
  3. 2
      stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java
  4. 34
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

13
frontend/src/routes/stock/Index.svelte

@ -92,6 +92,12 @@
} }
} }
async function loadItem(){
if (!item_id) return;
const url = api(`stock/${owner}/${owner_id}/item/${item_id}`);
const res = await get(url);
}
async function loadPath(){ async function loadPath(){
if (!location_id) return; if (!location_id) return;
const url = api(`stock/location/${location_id}`); const url = api(`stock/location/${location_id}`);
@ -140,6 +146,7 @@
async function load(){ async function load(){
await loadUserLocations(); await loadUserLocations();
loadItem();
loadPath(); loadPath();
loadProperties(); loadProperties();
} }
@ -244,4 +251,10 @@
</div> </div>
{/if} {/if}
{/await} {/await}
</div>
<div>
<pre>
{JSON.stringify({owner,owner_id,item_id},null,2)}
</pre>
</div> </div>

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

@ -211,21 +211,40 @@ public class SqliteDb extends BaseDb implements StockDb {
while (rs.next()) list.add(Item.of(rs)); while (rs.next()) list.add(Item.of(rs));
rs.close(); rs.close();
for (var item : list){ for (var item : list) loadProperties(item);
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 list; return list;
} catch (SQLException e){ } catch (SQLException e){
throw databaseException("Failed to load items at {0}",location); throw databaseException("Failed to load items at {0}",location);
} }
} }
@Override
public Item loadProperties(Item item){
try {
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 @Override
public Item loadItem(long id) { 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 { try {
var rs = select(ALL).from(TABLE_ITEMS).where(ID,equal(id)).exec(db); var rs = query.exec(db);
Item result = null; Item result = null;
if (rs.next()) result = Item.of(rs); if (rs.next()) result = Item.of(rs);
rs.close(); rs.close();

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

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

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

@ -90,6 +90,14 @@ public class StockModule extends BaseHandler implements StockService {
if (user.isEmpty()) return unauthorized(ex); if (user.isEmpty()) return unauthorized(ex);
var head = path.pop(); var head = path.pop();
return switch (head) { 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 -> { case LOCATION -> {
try { try {
var location = Location.of(Long.parseLong(path.pop())); 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 LOCATIONS -> getLocations(path,user.get(),ex);
case PROPERTIES -> getProperties(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); case null, default -> super.doGet(path,ex);
}; };
} catch (UmbrellaException e){ } 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)); 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 { private boolean getLocationEntities(Location location, HttpExchange ex) throws IOException {
var items = stockDb.listItemsAt(location).stream().map(Item::toMap).toList(); var items = stockDb.listItemsAt(location).stream().map(Item::toMap).toList();
var owner = location.resolve().owner(); var owner = location.resolve().owner();

Loading…
Cancel
Save