public async Task <GarmentAvalProduct> Handle(UpdateGarmentAvalProductCommand request, CancellationToken cancellaitonToken)
        {
            var garmentAvalProduct = _garmentAvalProductRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentAvalProduct == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            garmentAvalProduct.SetRONo(request.RONo);
            garmentAvalProduct.SetArticle(request.Article);
            garmentAvalProduct.SetAvalDate(request.AvalDate);

            var dbGarmentAvalProduct = _garmentAvalProductItemRepository.Find(y => y.APId == garmentAvalProduct.Identity);
            var updatedItems         = request.Items.Where(x => dbGarmentAvalProduct.Any(y => y.APId == garmentAvalProduct.Identity));
            var addedItems           = request.Items.Where(x => !dbGarmentAvalProduct.Any(y => y.APId == garmentAvalProduct.Identity));
            var deletedItems         = dbGarmentAvalProduct.Where(x => !request.Items.Any(y => y.APId == garmentAvalProduct.Identity));

            foreach (var item in updatedItems)
            {
                var dbItem = dbGarmentAvalProduct.Find(x => x.Identity == item.Identity);
                dbItem.setPreparingId(item.PreparingId);
                dbItem.setPreparingItemId(item.PreparingItemId);
                dbItem.setProductId(new ProductId(item.Product.Id));
                dbItem.setProductCode(item.Product.Code);
                dbItem.setProductName(item.Product.Name);
                dbItem.setDesignColor(item.DesignColor);
                dbItem.setQuantity(item.Quantity);
                dbItem.setUomId(new UomId(item.Uom.Id));
                dbItem.setUomUnit(item.Uom.Unit);
                await _garmentAvalProductItemRepository.Update(dbItem);
            }

            addedItems.Select(x => new GarmentAvalProductItem(Guid.NewGuid(), garmentAvalProduct.Identity, x.PreparingId, x.PreparingItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit, x.BasicPrice, x.IsReceived)).ToList()
            .ForEach(async x => await _garmentAvalProductItemRepository.Update(x));

            foreach (var item in deletedItems)
            {
                item.SetDeleted();
                await _garmentAvalProductItemRepository.Update(item);
            }


            garmentAvalProduct.SetModified();

            await _garmentAvalProductRepository.Update(garmentAvalProduct);

            _storage.Save();

            return(garmentAvalProduct);
        }
        public async Task <GarmentAvalProduct> Handle(RemoveGarmentAvalProductCommand request, CancellationToken cancellationToken)
        {
            var garmentAvalProduct = _garmentAvalProductRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentAvalProduct == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            var garmentAvalProductItems = _garmentAvalProductItemRepository.Find(x => x.APId == request.Id);

            foreach (var item in garmentAvalProductItems)
            {
                item.Remove();
                await _garmentAvalProductItemRepository.Update(item);

                var garmentPreparingItem = _garmentPreparingItemRepository.Find(o => o.Identity == Guid.Parse(item.PreparingItemId.Value)).Single();

                garmentPreparingItem.setRemainingQuantityZeroValue(garmentPreparingItem.RemainingQuantity + item.Quantity);

                garmentPreparingItem.SetModified();
                await _garmentPreparingItemRepository.Update(garmentPreparingItem);
            }

            garmentAvalProduct.Remove();

            await _garmentAvalProductRepository.Update(garmentAvalProduct);

            _storage.Save();

            return(garmentAvalProduct);
        }
        public async Task <GarmentAvalProduct> Handle(PlaceGarmentAvalProductCommand request, CancellationToken cancellationToken)
        {
            //var garmentAvalProduct = _garmentAvalProductRepository.Find(o =>
            //                       o.RONo == request.RONo &&
            //                       o.Article == request.Article &&
            //                       o.AvalDate == request.AvalDate).FirstOrDefault();
            //List<GarmentAvalProductItem> garmentAvalProductItem = new List<GarmentAvalProductItem>();
            //if (garmentAvalProduct == null)
            //{
            GarmentAvalProduct garmentAvalProduct = new GarmentAvalProduct(Guid.NewGuid(), request.RONo, request.Article, request.AvalDate, new Domain.Shared.ValueObjects.UnitDepartmentId(request.Unit.Id), request.Unit.Code, request.Unit.Name);

            request.Items.Select(x => new GarmentAvalProductItem(Guid.NewGuid(), garmentAvalProduct.Identity, x.PreparingId, x.PreparingItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit, x.BasicPrice, x.IsReceived)).ToList()
            .ForEach(async x => await _garmentAvalProductItemRepository.Update(x));
            //}

            foreach (var itemPreparing in request.Items)
            {
                var garmentPreparingItem = _garmentPreparingItemRepository.Find(o => o.Identity == Guid.Parse(itemPreparing.PreparingItemId.Value)).Single();

                garmentPreparingItem.setRemainingQuantityZeroValue(garmentPreparingItem.RemainingQuantity - itemPreparing.Quantity);

                garmentPreparingItem.SetModified();
                await _garmentPreparingItemRepository.Update(garmentPreparingItem);
            }

            garmentAvalProduct.SetModified();

            await _garmentAvalProductRepository.Update(garmentAvalProduct);

            _storage.Save();

            return(garmentAvalProduct);
        }
        public async Task <bool> Handle(UpdateIsReceivedGarmentAvalProductCommand request, CancellationToken cancellationToken)
        {
            List <Guid> guids = new List <Guid>();

            foreach (var id in request.Identities)
            {
                guids.Add(Guid.Parse(id));
            }
            var AvalProductItems = _garmentAvalProductItemRepository.Query.Where(a => guids.Contains(a.Identity)).Select(a => new GarmentAvalProductItem(a)).ToList();

            foreach (var item in AvalProductItems)
            {
                item.SetIsReceived(request.IsReceived);
                item.SetDeleted();
                await _garmentAvalProductItemRepository.Update(item);
            }
            _storage.Save();

            return(request.IsReceived);
        }