using System.Runtime.InteropServices;
using System.Text.Json;
namespace StateSet.Embedded;
public sealed class StateSetCommerce : IDisposable
{
private IntPtr _handle;
private bool _disposed;
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
public CustomersApi Customers { get; }
public ProductsApi Products { get; }
public OrdersApi Orders { get; }
public InventoryApi Inventory { get; }
public CartsApi Carts { get; }
public ReturnsApi Returns { get; }
public PaymentsApi Payments { get; }
public AnalyticsApi Analytics { get; }
public ShipmentsApi Shipments { get; }
public WarrantiesApi Warranties { get; }
public SuppliersApi Suppliers { get; }
public PurchaseOrdersApi PurchaseOrders { get; }
public InvoicesApi Invoices { get; }
public BomApi Bom { get; }
public WorkOrdersApi WorkOrders { get; }
public CurrencyApi Currency { get; }
public SubscriptionsApi Subscriptions { get; }
public PromotionsApi Promotions { get; }
public TaxApi Tax { get; }
public QualityApi Quality { get; }
public LotsApi Lots { get; }
public SerialsApi Serials { get; }
public WarehouseApi Warehouse { get; }
public ReceivingApi Receiving { get; }
public FulfillmentApi Fulfillment { get; }
public AccountsPayableApi AccountsPayable { get; }
public AccountsReceivableApi AccountsReceivable { get; }
public CostAccountingApi CostAccounting { get; }
public CreditApi Credit { get; }
public BackordersApi Backorders { get; }
public GeneralLedgerApi GeneralLedger { get; }
public StateSetCommerce(string dbPath)
{
_handle = NativeMethods.stateset_commerce_new(dbPath);
if (_handle == IntPtr.Zero)
{
throw new StateSetException("Failed to create commerce instance");
}
Customers = new CustomersApi(this);
Products = new ProductsApi(this);
Orders = new OrdersApi(this);
Inventory = new InventoryApi(this);
Carts = new CartsApi(this);
Returns = new ReturnsApi(this);
Payments = new PaymentsApi(this);
Analytics = new AnalyticsApi(this);
Shipments = new ShipmentsApi(this);
Warranties = new WarrantiesApi(this);
Suppliers = new SuppliersApi(this);
PurchaseOrders = new PurchaseOrdersApi(this);
Invoices = new InvoicesApi(this);
Bom = new BomApi(this);
WorkOrders = new WorkOrdersApi(this);
Currency = new CurrencyApi(this);
Subscriptions = new SubscriptionsApi(this);
Promotions = new PromotionsApi(this);
Tax = new TaxApi(this);
Quality = new QualityApi(this);
Lots = new LotsApi(this);
Serials = new SerialsApi(this);
Warehouse = new WarehouseApi(this);
Receiving = new ReceivingApi(this);
Fulfillment = new FulfillmentApi(this);
AccountsPayable = new AccountsPayableApi(this);
AccountsReceivable = new AccountsReceivableApi(this);
CostAccounting = new CostAccountingApi(this);
Credit = new CreditApi(this);
Backorders = new BackordersApi(this);
GeneralLedger = new GeneralLedgerApi(this);
}
internal IntPtr Handle
{
get
{
ObjectDisposedException.ThrowIf(_disposed, this);
return _handle;
}
}
internal static T? ParseJson<T>(IntPtr ptr) where T : class
{
if (ptr == IntPtr.Zero)
return null;
try
{
var json = Marshal.PtrToStringUTF8(ptr);
if (string.IsNullOrEmpty(json))
return null;
return JsonSerializer.Deserialize<T>(json, JsonOptions);
}
finally
{
NativeMethods.stateset_string_free(ptr);
}
}
internal static T ParseJsonRequired<T>(IntPtr ptr) where T : class
{
var result = ParseJson<T>(ptr);
return result ?? throw new StateSetException("Failed to parse response");
}
internal static List<T> ParseJsonList<T>(IntPtr ptr) where T : class
{
if (ptr == IntPtr.Zero)
return new List<T>();
try
{
var json = Marshal.PtrToStringUTF8(ptr);
if (string.IsNullOrEmpty(json))
return new List<T>();
return JsonSerializer.Deserialize<List<T>>(json, JsonOptions) ?? new List<T>();
}
finally
{
NativeMethods.stateset_string_free(ptr);
}
}
public void Dispose()
{
if (_disposed)
return;
if (_handle != IntPtr.Zero)
{
NativeMethods.stateset_commerce_free(_handle);
_handle = IntPtr.Zero;
}
_disposed = true;
}
}
public class StateSetException : Exception
{
public StateSetException(string message) : base(message) { }
public StateSetException(string message, Exception inner) : base(message, inner) { }
}