@ -90,6 +90,14 @@ public class StockModule extends BaseHandler implements StockService {
@@ -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 {
@@ -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 {
@@ -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 ( ) ;