Skip to content

Commit

Permalink
Add Client Orders Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
TudorOrban committed Apr 26, 2024
1 parent 1336252 commit 0374270
Show file tree
Hide file tree
Showing 13 changed files with 859 additions and 206 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/chainoptim/desktop/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ protected void configure() {
// - Client
bind(ClientService.class).to(ClientServiceImpl.class);
bind(ClientWriteService.class).to(ClientWriteServiceImpl.class);
bind(ClientOrderService.class).to(ClientOrderServiceImpl.class);
bind(ClientOrderWriteService.class).to(ClientOrderWriteServiceImpl.class);
bind(ClientOrdersService.class).to(ClientOrdersServiceImpl.class);
bind(ClientOrdersWriteService.class).to(ClientOrdersWriteServiceImpl.class);

// - SC Analysis
bind(ProductProductionGraphService.class).to(ProductProductionGraphServiceImpl.class);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package org.chainoptim.desktop.features.client.dto;

import org.chainoptim.desktop.shared.enums.OrderStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateClientOrderDTO {

private Integer organizationId;
private Integer clientId;
private Integer productId;
private Float quantity;
private Float deliveredQuantity;
private LocalDateTime orderDate;
private LocalDateTime estimatedDeliveryDate;
private LocalDateTime deliveryDate;
private OrderStatus status;
private String companyId;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.chainoptim.desktop.features.client.dto;

import org.chainoptim.desktop.shared.enums.OrderStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -20,4 +21,5 @@ public class UpdateClientOrderDTO {
private LocalDateTime orderDate;
private LocalDateTime estimatedDeliveryDate;
private LocalDateTime deliveryDate;
private OrderStatus status;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.chainoptim.desktop.features.client.model;


import org.chainoptim.desktop.features.product.model.Product;
import org.chainoptim.desktop.shared.enums.OrderStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -12,15 +14,33 @@
@NoArgsConstructor
public class ClientOrder {

public ClientOrder(ClientOrder clientOrder) {
this.id = clientOrder.id;
this.clientId = clientOrder.clientId;
this.createdAt = clientOrder.createdAt;
this.updatedAt = clientOrder.updatedAt;
this.product = clientOrder.product;
this.organizationId = clientOrder.organizationId;
this.quantity = clientOrder.quantity;
this.deliveredQuantity = clientOrder.deliveredQuantity;
this.orderDate = clientOrder.orderDate;
this.estimatedDeliveryDate = clientOrder.estimatedDeliveryDate;
this.deliveryDate = clientOrder.deliveryDate;
this.status = clientOrder.status;
this.companyId = clientOrder.companyId;
}

private Integer id;
private Integer clientId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Integer productId;
private Product product;
private Integer organizationId;
private Float quantity;
private Float deliveredQuantity;
private LocalDateTime orderDate;
private LocalDateTime estimatedDeliveryDate;
private LocalDateTime deliveryDate;
private String status;
private OrderStatus status;
private String companyId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface ClientOrderService {
public interface ClientOrdersService {

CompletableFuture<Result<List<ClientOrder>>> getClientOrdersByOrganizationId(Integer organizationId);
CompletableFuture<Result<PaginatedResults<ClientOrder>>> getClientOrdersByClientIdAdvanced(
Integer clientId,
SearchParams searchParams
);
CompletableFuture<Result<ClientOrder>> getClientOrderById(Integer orderId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import org.chainoptim.desktop.core.user.service.TokenManager;
import org.chainoptim.desktop.features.client.model.ClientOrder;
import org.chainoptim.desktop.features.client.service.ClientOrdersService;
import org.chainoptim.desktop.shared.caching.CacheKeyBuilder;
import org.chainoptim.desktop.shared.caching.CachingService;
import org.chainoptim.desktop.shared.httphandling.RequestBuilder;
import org.chainoptim.desktop.shared.httphandling.RequestHandler;
import org.chainoptim.desktop.shared.httphandling.Result;
import org.chainoptim.desktop.shared.search.model.PaginatedResults;
import org.chainoptim.desktop.shared.search.model.SearchParams;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.inject.Inject;

Expand All @@ -18,42 +18,40 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ClientOrderServiceImpl implements ClientOrderService {
public class ClientOrdersServiceImpl implements ClientOrdersService {

private final CachingService<PaginatedResults<ClientOrder>> cachingService;
private final RequestHandler requestHandler;
private final RequestBuilder requestBuilder;
private final RequestHandler requestHandler;
private final TokenManager tokenManager;

private static final int STALE_TIME = 300;

@Inject
public ClientOrderServiceImpl(CachingService<PaginatedResults<ClientOrder>> cachingService,
RequestHandler requestHandler,
RequestBuilder requestBuilder,
TokenManager tokenManager) {
public ClientOrdersServiceImpl(CachingService<PaginatedResults<ClientOrder>> cachingService,
RequestBuilder requestBuilder,
RequestHandler requestHandler,
TokenManager tokenManager) {
this.cachingService = cachingService;
this.requestHandler = requestHandler;
this.requestBuilder = requestBuilder;
this.requestHandler = requestHandler;
this.tokenManager = tokenManager;
}

public CompletableFuture<Result<List<ClientOrder>>> getClientOrdersByOrganizationId(Integer organizationId) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/organization/" + organizationId.toString();

HttpRequest request = requestBuilder.buildReadRequest(routeAddress, tokenManager.getToken());
if (request == null) return requestHandler.getParsingErrorResult();

return requestHandler.sendRequest(request, new TypeReference<List<ClientOrder>>() {});
}


public CompletableFuture<Result<PaginatedResults<ClientOrder>>> getClientOrdersByClientIdAdvanced(
Integer clientId,
SearchParams searchParams
) {
String rootAddress = "http://localhost:8080/api/v1/";
String cacheKey = CacheKeyBuilder.buildAdvancedSearchKey("client-orders", "client", clientId.toString(), searchParams);

String routeAddress = rootAddress + cacheKey;

HttpRequest request = requestBuilder.buildReadRequest(routeAddress, tokenManager.getToken());
Expand All @@ -68,12 +66,4 @@ public CompletableFuture<Result<PaginatedResults<ClientOrder>>> getClientOrdersB
});
}

public CompletableFuture<Result<ClientOrder>> getClientOrderById(Integer orderId) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/" + orderId.toString();

HttpRequest request = requestBuilder.buildReadRequest(routeAddress, tokenManager.getToken());
if (request == null) return requestHandler.getParsingErrorResult();

return requestHandler.sendRequest(request, new TypeReference<ClientOrder>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import org.chainoptim.desktop.features.client.model.ClientOrder;
import org.chainoptim.desktop.shared.httphandling.Result;

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

public interface ClientOrderWriteService {
public interface ClientOrdersWriteService {

CompletableFuture<Result<ClientOrder>> createClientOrder(CreateClientOrderDTO orderDTO);
CompletableFuture<Result<ClientOrder>> updateClientOrder(UpdateClientOrderDTO orderDTO);
CompletableFuture<Result<Integer>> deleteClientOrder(Integer orderId);
CompletableFuture<Result<ClientOrder>> createClientOrder(CreateClientOrderDTO clientDTO);

CompletableFuture<Result<List<Integer>>> deleteClientOrderInBulk(List<Integer> orderIds);

CompletableFuture<Result<List<ClientOrder>>> updateClientOrdersInBulk(List<UpdateClientOrderDTO> orderDTOs);

CompletableFuture<Result<List<ClientOrder>>> createClientOrdersInBulk(List<CreateClientOrderDTO> orderDTOs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@

import org.chainoptim.desktop.core.user.service.TokenManager;
import org.chainoptim.desktop.features.client.dto.CreateClientOrderDTO;
import org.chainoptim.desktop.features.client.dto.UpdateClientDTO;
import org.chainoptim.desktop.features.client.dto.UpdateClientOrderDTO;
import org.chainoptim.desktop.features.client.model.ClientOrder;
import org.chainoptim.desktop.features.client.service.ClientOrdersWriteService;
import org.chainoptim.desktop.shared.caching.CachingService;
import org.chainoptim.desktop.shared.httphandling.HttpMethod;
import org.chainoptim.desktop.shared.httphandling.RequestBuilder;
import org.chainoptim.desktop.shared.httphandling.RequestHandler;
import org.chainoptim.desktop.shared.httphandling.Result;

import org.chainoptim.desktop.shared.search.model.PaginatedResults;
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 ClientOrderWriteServiceImpl implements ClientOrderWriteService {
public class ClientOrdersWriteServiceImpl implements ClientOrdersWriteService {

private final RequestHandler requestHandler;
private final CachingService<PaginatedResults<ClientOrder>> cachingService;
private final RequestBuilder requestBuilder;
private final RequestHandler requestHandler;
private final TokenManager tokenManager;

@Inject
public ClientOrderWriteServiceImpl(RequestHandler requestHandler,
RequestBuilder requestBuilder,
TokenManager tokenManager) {
this.requestHandler = requestHandler;
public ClientOrdersWriteServiceImpl(CachingService<PaginatedResults<ClientOrder>> cachingService,
RequestBuilder requestBuilder,
RequestHandler requestHandler,
TokenManager tokenManager) {
this.cachingService = cachingService;
this.requestBuilder = requestBuilder;
this.requestHandler = requestHandler;
this.tokenManager = tokenManager;
}

Expand All @@ -36,28 +41,42 @@ public CompletableFuture<Result<ClientOrder>> createClientOrder(CreateClientOrde

HttpRequest request = requestBuilder.buildWriteRequest(
HttpMethod.POST, routeAddress, tokenManager.getToken(), orderDTO);
if (request == null) return requestHandler.getParsingErrorResult();

return requestHandler.sendRequest(request, new TypeReference<ClientOrder>() {});
return requestHandler.sendRequest(request, new TypeReference<ClientOrder>() {}, order -> {
cachingService.clear(); // Invalidate cache
});
}

public CompletableFuture<Result<List<ClientOrder>>> createClientOrdersInBulk(List<CreateClientOrderDTO> orderDTOs) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/create/bulk";

HttpRequest request = requestBuilder.buildWriteRequest(
HttpMethod.POST, routeAddress, tokenManager.getToken(), orderDTOs);

return requestHandler.sendRequest(request, new TypeReference<List<ClientOrder>>() {}, orders -> {
cachingService.clear(); // Invalidate cache
});
}

public CompletableFuture<Result<ClientOrder>> updateClientOrder(UpdateClientOrderDTO orderDTO) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/update";
public CompletableFuture<Result<List<ClientOrder>>> updateClientOrdersInBulk(List<UpdateClientOrderDTO> orderDTOs) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/update/bulk";

HttpRequest request = requestBuilder.buildWriteRequest(
HttpMethod.PUT, routeAddress, tokenManager.getToken(), orderDTO);
if (request == null) return requestHandler.getParsingErrorResult();
HttpMethod.PUT, routeAddress, tokenManager.getToken(), orderDTOs);

return requestHandler.sendRequest(request, new TypeReference<ClientOrder>() {});
return requestHandler.sendRequest(request, new TypeReference<List<ClientOrder>>() {}, orders -> {
cachingService.clear(); // Invalidate cache
});
}

public CompletableFuture<Result<Integer>> deleteClientOrder(Integer orderId) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/delete/" + orderId;
public CompletableFuture<Result<List<Integer>>> deleteClientOrderInBulk(List<Integer> orderIds) {
String routeAddress = "http://localhost:8080/api/v1/client-orders/delete/bulk";

HttpRequest request = requestBuilder.buildWriteRequest(
HttpMethod.DELETE, routeAddress, tokenManager.getToken(), null);
if (request == null) return requestHandler.getParsingErrorResult();
HttpMethod.DELETE, routeAddress, tokenManager.getToken(), orderIds);

return requestHandler.sendRequest(request, new TypeReference<Integer>() {});
return requestHandler.sendRequest(request, new TypeReference<List<Integer>>() {}, ids -> {
cachingService.clear(); // Invalidate cache
});
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package org.chainoptim.desktop.features.product.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.chainoptim.desktop.features.productpipeline.model.Stage;
import org.chainoptim.desktop.shared.features.location.model.Location;

import java.time.LocalDateTime;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {

public Product(Product newProduct) {
this.id = newProduct.getId();
this.name = newProduct.getName();
this.description = newProduct.getDescription();
this.createdAt = newProduct.getCreatedAt();
this.updatedAt = newProduct.getUpdatedAt();
this.organizationId = newProduct.getOrganizationId();
this.unit = newProduct.getUnit();
this.stages = newProduct.getStages();
}

private Integer id;
private String name;
private String description;
Expand Down
Loading

0 comments on commit 0374270

Please sign in to comment.