Skip to content

Commit

Permalink
Add loading crates in Warehouse Storage tab
Browse files Browse the repository at this point in the history
  • Loading branch information
TudorOrban committed Jul 27, 2024
1 parent 813e658 commit e34eba1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/org/chainoptim/desktop/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ protected void configure() {
bind(WarehouseInventoryItemService.class).to(WarehouseInventoryItemServiceImpl.class);
bind(WarehouseInventoryItemWriteService.class).to(WarehouseInventoryItemWriteServiceImpl.class);
bind(CompartmentService.class).to(CompartmentServiceImpl.class);
bind(CrateService.class).to(CrateServiceImpl.class);

// - Supplier
bind(SupplierService.class).to(SupplierServiceImpl.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.chainoptim.desktop.features.warehouse.controller;

import org.chainoptim.desktop.core.context.TenantContext;
import org.chainoptim.desktop.features.warehouse.model.Compartment;
import org.chainoptim.desktop.features.warehouse.model.Crate;
import org.chainoptim.desktop.features.warehouse.model.Warehouse;
import org.chainoptim.desktop.features.warehouse.service.CompartmentService;
import org.chainoptim.desktop.features.warehouse.service.CrateService;
import org.chainoptim.desktop.shared.fallback.FallbackManager;
import org.chainoptim.desktop.shared.httphandling.Result;
import org.chainoptim.desktop.shared.util.DataReceiver;
Expand All @@ -18,6 +21,7 @@ public class WarehouseStorageController implements DataReceiver<Warehouse> {

// Services
private final CompartmentService compartmentService;
private final CrateService crateService;

// State
private final FallbackManager fallbackManager;
Expand All @@ -28,13 +32,22 @@ public class WarehouseStorageController implements DataReceiver<Warehouse> {

@Inject
public WarehouseStorageController(CompartmentService compartmentService,
CrateService crateService,
FallbackManager fallbackManager) {
this.compartmentService = compartmentService;
this.crateService = crateService;
this.fallbackManager = fallbackManager;
}

public void setData(Warehouse warehouse) {
loadCompartments(warehouse.getId());

if (TenantContext.getCurrentUser() == null) {
fallbackManager.setErrorMessage("User not logged in");
return;
}
Integer organizationId = TenantContext.getCurrentUser().getOrganization().getId();
loadCrates(organizationId);
}

private void loadCompartments(Integer warehouseId) {
Expand All @@ -45,6 +58,12 @@ private void loadCompartments(Integer warehouseId) {
.thenApply(this::handleCompartmentsResponse)
.exceptionally(this::handleCompartmentsError);
}

private void loadCrates(Integer organizationId) {
crateService.getCratesByOrganizationId(organizationId)
.thenApply(this::handleCratesResponse)
.exceptionally(this::handleCratesError);
}

private Result<List<Compartment>> handleCompartmentsResponse(Result<List<Compartment>> result) {
Platform.runLater(() -> {
Expand Down Expand Up @@ -79,4 +98,25 @@ private void renderCompartments(List<Compartment> compartments) {

fallbackManager.setNoResults(false);
}

private Result<List<Crate>> handleCratesResponse(Result<List<Crate>> result) {
Platform.runLater(() -> {
if (result.getError() != null) {
fallbackManager.setErrorMessage(result.getError().getMessage());
return;
}

fallbackManager.setLoading(false);
System.out.println("Crates loaded successfully");
// renderCrates(result.getData());
});

return result;
}

private Result<List<Crate>> handleCratesError(Throwable throwable) {
fallbackManager.setErrorMessage("Failed to load crates");

return new Result<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.chainoptim.desktop.features.warehouse.service;

import org.chainoptim.desktop.features.warehouse.model.Compartment;
import org.chainoptim.desktop.features.warehouse.model.Crate;
import org.chainoptim.desktop.shared.httphandling.Result;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface CrateService {

CompletableFuture<Result<List<Crate>>> getCratesByOrganizationId(Integer organizationId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.chainoptim.desktop.features.warehouse.service;

import org.chainoptim.desktop.core.user.service.TokenManager;
import org.chainoptim.desktop.features.warehouse.model.Compartment;
import org.chainoptim.desktop.features.warehouse.model.Crate;
import org.chainoptim.desktop.shared.httphandling.RequestBuilder;
import org.chainoptim.desktop.shared.httphandling.RequestHandler;
import org.chainoptim.desktop.shared.httphandling.Result;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.inject.Inject;

import java.net.http.HttpRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class CrateServiceImpl implements CrateService {

private final RequestBuilder requestBuilder;
private final RequestHandler requestHandler;
private final TokenManager tokenManager;

@Inject
public CrateServiceImpl(RequestBuilder requestBuilder,
RequestHandler requestHandler,
TokenManager tokenManager) {
this.requestBuilder = requestBuilder;
this.requestHandler = requestHandler;
this.tokenManager = tokenManager;
}

public CompletableFuture<Result<List<Crate>>> getCratesByOrganizationId(Integer organizationId) {
String routeAddress = "http://localhost:8080/api/v1/crates/organization/" + organizationId.toString();

HttpRequest request = requestBuilder.buildReadRequest(routeAddress, tokenManager.getToken());

return requestHandler.sendRequest(request, new TypeReference<List<Crate>>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.chainoptim.desktop.features.warehouse.controller.WarehouseStorageController"
prefHeight="400.0" prefWidth="600.0">
prefHeight="400.0" prefWidth="600.0" style="-fx-padding: 20px;">

<VBox fx:id="compartmentsVBox"/>
</VBox>

0 comments on commit e34eba1

Please sign in to comment.