Beispiel #1
0
        public static async Task <DL.Models.Product> GetProduct(Guid productId, DateTimeOffset referenceDate)
        {
            DL.Models.Product result = null;

            if (!ProductStocks.ContainsKey(productId))
            {
                await _loadLock.WaitAsync().ConfigureAwait(false);

                try
                {
                    result = await LoadProduct(productId).ConfigureAwait(false);

                    SetProductState(result, referenceDate);
                    return(result);
                }
                catch (Exception exception)
                {
                    // Log exception
                    throw;
                }
                finally
                {
                    _loadLock.Release();
                }
            }

            return(result);
        }
Beispiel #2
0
        internal static DL.Models.Product SetProduct(ProductStock.DL.Models.Product product)
        {
            if (!ProductStocks.ContainsKey(product.Id))
            {
                ProductStocks.Add(product.Id, product);

                var result = new DL.Models.Product(product.Id, product.Number, product.Description);
                Products.Add(product.Id, result);

                var priceChanges = ChangeFactory.ToOrderedChangeEvent <
                    DL.Models.Product,
                    ProductStock.DL.Models.ProductPrice,
                    Guid,
                    double,
                    DateTimeOffset>(
                    x => x.Price,
                    0,
                    product.Prices,
                    x => x.Id,
                    x => x.Price,
                    x => x.RegisterDate).
                                   Cast <IEvent <DL.Models.Product, Guid> >().ToList();

                var stockAvailableMutations = MutationFactory.ToOrderedMutationEvent <
                    DL.Models.Product,
                    ProductStock.DL.Models.ProductStockMutation,
                    Guid,
                    int,
                    DateTimeOffset>(
                    x => x.Available,
                    product.StockMutations,
                    x => x.Id,
                    x => x.Amount * (x.Type == ProductStock.DL.Enums.MutationType.Purchase ? -1 : 1),
                    x => x.OrderDate).Cast <IEvent <DL.Models.Product, Guid> >().ToList();

                var stockInStockMutations = MutationFactory.ToOrderedMutationEvent <
                    DL.Models.Product,
                    ProductStock.DL.Models.ProductStockMutation,
                    Guid,
                    int,
                    DateTimeOffset>(
                    x => x.InStock,
                    product.StockMutations,
                    x => x.Id,
                    x => x.Amount * (x.Type == ProductStock.DL.Enums.MutationType.Purchase ? -1 : 1),
                    x => x.ShipmentDate).Cast <IEvent <DL.Models.Product, Guid> >().ToList();

                AddEvents(product.Id, new OrderedEventCollection <DL.Models.Product, Guid, DateTimeOffset>(priceChanges.ToList()));
                AddEvents(product.Id, new OrderedEventCollection <DL.Models.Product, Guid, DateTimeOffset>(stockAvailableMutations.ToList()));
                AddEvents(product.Id, new OrderedEventCollection <DL.Models.Product, Guid, DateTimeOffset>(stockInStockMutations.ToList()));

                return(result);
            }

            return(Products[product.Id]);
        }