public ResponseEntity getLocations()

in dashboard-web-app/src/main/java/org/inventory/hub/controller/ProductsInventoryController.java [163:208]


    public ResponseEntity<?> getLocations() {
        try {

            final Flux<ProductsInventory> productsInventoriesFlux = productsInventoryRepository.findAll();
            final Iterable<ProductsInventory> productsInventories = productsInventoriesFlux.toIterable();

            final ResponseEntity<Iterable<ProductsInventory>> productInventories =
                new ResponseEntity<Iterable<ProductsInventory>>(productsInventories, HttpStatus.OK);

            System.out.println("======= /api/locations ===== ");
            System.out.println(productInventories.toString());

            final Iterable <ProductsInventory> iterable = (Iterable<ProductsInventory>) productInventories.getBody();
            final Iterator <ProductsInventory> it = makeCollection(iterable).iterator();

            final Map<String, Integer> locations = new HashMap<>();

            ProductsInventory productInventory = null;

            while (it.hasNext()) {
                productInventory = it.next();

                final String currentLocation = productInventory.getLocation();
                Integer totalItems = locations.get(currentLocation);
                if (totalItems == null) {
                    totalItems = Integer.valueOf(productInventory.getTotalCount());
                    locations.put(currentLocation, totalItems);
                } else {
                    locations.put(currentLocation, totalItems + Integer.valueOf(productInventory.getTotalCount()));
                }
            }
            for (final Map.Entry<String, Integer> locationEntry : locations.entrySet()) {
                System.out.format("== locations === %s: %d\n",
                    locationEntry.getKey(), locationEntry.getValue());
            }

            System.out.println("====== success");

            return new ResponseEntity<Map<String, Integer>> (locations, HttpStatus.OK);

        } catch (Exception e) {
            System.out.println("======== EXCEPTION =======");
            e.printStackTrace();
            return new ResponseEntity<String>("Nothing found", HttpStatus.NOT_FOUND);
        }
    }