working on loading items for location
This commit is contained in:
@@ -5,9 +5,11 @@ public class Constants {
|
||||
|
||||
private Constants(){}
|
||||
|
||||
|
||||
public static final String BELOW = "below";
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
|
||||
public static final String ITEM_ID = "item_id";
|
||||
public static final String ITEMS_AT = "items_at";
|
||||
public static final String LOCATIONS = "locations";
|
||||
public static final String OF_USER = "of_user";
|
||||
public static final String PROPERTY_ID = "prop_id";
|
||||
|
||||
@@ -13,7 +13,9 @@ import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import de.srsoftware.umbrella.core.model.Item;
|
||||
@@ -139,6 +141,38 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Item> listItemsAt(long locationId) {
|
||||
try {
|
||||
var rs = select(ALL).from(TABLE_ITEMS).where(LOCATION_ID,equal(locationId)).exec(db);
|
||||
var list = new ArrayList<Item>();
|
||||
|
||||
while (rs.next()) {
|
||||
var ownerId = rs.getLong(OWNER);
|
||||
Mappable owner = ownerId < 0 ? ModuleRegistry.companyService().get(-ownerId) : ModuleRegistry.userService().loadUser(ownerId);
|
||||
var location = loadLocation(rs.getLong(LOCATION_ID));
|
||||
list.add(Item.of(rs, owner, location));
|
||||
}
|
||||
rs.close();
|
||||
return list;
|
||||
} catch (SQLException e){
|
||||
throw databaseException("Failed to load items at {0}",locationId);
|
||||
}
|
||||
}
|
||||
|
||||
private Location loadLocation(long locationId) {
|
||||
try {
|
||||
var rs = select(ALL).from(TABLE_LOCATIONS).where(ID,equal(locationId)).exec(db);
|
||||
Location loc = null;
|
||||
if (rs.next()) loc = Location.of(rs);
|
||||
rs.close();
|
||||
if (loc != null) return loc;
|
||||
throw databaseException("Failed to load location with id = {0}",locationId);
|
||||
} catch (SQLException e){
|
||||
throw databaseException("Failed to load location with id = {0}",locationId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Location> listLocations(long companyId) {
|
||||
return List.of();
|
||||
|
||||
@@ -19,6 +19,7 @@ import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.api.StockService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Item;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
@@ -45,14 +46,26 @@ public class StockApi extends BaseHandler implements StockService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case ITEMS_AT -> {
|
||||
try {
|
||||
var id = Long.parseLong(path.pop());
|
||||
yield getItemsAt(user.get(),id,ex);
|
||||
} catch (Exception e){
|
||||
yield super.doGet(path,ex);
|
||||
}
|
||||
}
|
||||
case LOCATIONS -> getLocations(path,user.get(),ex);
|
||||
case null, default -> doGet(path,ex);
|
||||
case null, default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getItemsAt(UmbrellaUser user, long locationId, HttpExchange ex) throws IOException {
|
||||
return sendContent(ex, stockDb.listItemsAt(locationId).stream().map(Item::toMap).toList());
|
||||
}
|
||||
|
||||
private boolean getLocations(Path path, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
|
||||
@@ -14,6 +14,8 @@ public interface StockDb {
|
||||
|
||||
Collection<Item> listItems(long companyId) throws UmbrellaException;
|
||||
|
||||
Collection<Item> listItemsAt(long locationId);
|
||||
|
||||
Collection<Location> listLocations(long companyId);
|
||||
|
||||
Collection<Location> listUserLocations(UmbrellaUser userId);
|
||||
|
||||
Reference in New Issue
Block a user