private async Task CreateProductMeasures(Product product, IReadOnlyCollection <CreateProductMeasureDto> requestedMeasures)
        {
            var measures = await _measuresRepository.GetMeasures(requestedMeasures.Select(x => x.MeasureGuid));

            foreach (var measureData in requestedMeasures)
            {
                var measure = measures.FirstOrDefault(x => x.MeasureGuid == measureData.MeasureGuid);
                if (measure == null)
                {
                    throw new InternalException(ErrorCode.MeasureNotExists, measureData.MeasureGuid.ToString());
                }

                var valueInGrams   = measure.IsWeight ? measure.ValueInGrams ?? measureData.ValueInGrams : measureData.ValueInGrams;
                var productMeasure = new ProductMeasure(_guid.GetGuid(), measure, valueInGrams);

                product.AddProductMeasure(productMeasure);
            }

            if (!requestedMeasures.Any(x => x.IsDefault))
            {
                if (!product.ProductMeasures.Any(x => x.Measure.IsBaseline))
                {
                    var baselineMeasure = await _measuresRepository.GetBaselineMeasure();

                    var productMeasure = new ProductMeasure(_guid.GetGuid(), baselineMeasure, baselineMeasure.ValueInGrams.Value);
                    product.AddProductMeasure(productMeasure);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task UpsertMeasures(Product product, IReadOnlyCollection <UpdateProductMeasureDto> requestedMeasures)
        {
            var measures = await _measuresRepository.GetMeasures(requestedMeasures.Select(x => x.MeasureGuid));

            foreach (var measureToDeactivate in product.ProductMeasures
                     .Where(x => x.IsActive && x.ProductMeasureId != 0 && !requestedMeasures.Any(rm => rm.ProductMeasureGuid.HasValue && rm.ProductMeasureGuid == x.ProductMeasureGuid)).ToList())
            {
                measureToDeactivate.Deactivate();
            }

            foreach (var measureData in requestedMeasures)
            {
                if (measureData.ProductMeasureGuid.HasValue)
                {
                    var toUpdate = product.ProductMeasures.First(x => x.ProductMeasureGuid == measureData.ProductMeasureGuid.Value);
                    toUpdate.SetValueInGrams(measureData.ValueInGrams);
                    if (measureData.IsDefault)
                    {
                        product.SetDefaultMeasure(toUpdate.Measure);
                    }
                }
                else
                {
                    var measure = measures.FirstOrDefault(x => x.MeasureGuid == measureData.MeasureGuid);
                    if (measure == null)
                    {
                        throw new InternalException(ErrorCode.MeasureNotExists, measureData.MeasureGuid.ToString());
                    }

                    var valueInGrams   = measure.IsWeight ? measure.ValueInGrams ?? measureData.ValueInGrams : measureData.ValueInGrams;
                    var productMeasure = new ProductMeasure(_guid.GetGuid(), measure, valueInGrams);
                    if (measureData.IsDefault)
                    {
                        product.SetDefaultMeasure(measure);
                    }

                    product.AddProductMeasure(productMeasure);
                }
            }

            if (!requestedMeasures.Any(x => x.IsDefault))
            {
                if (!product.ProductMeasures.Any(x => x.IsActive && x.Measure.IsBaseline))
                {
                    var baselineMeasure = await _measuresRepository.GetBaselineMeasure();

                    var productMeasure = new ProductMeasure(_guid.GetGuid(), baselineMeasure, baselineMeasure.ValueInGrams.Value);

                    product
                    .AddProductMeasure(productMeasure)
                    .SetDefaultMeasure(baselineMeasure);
                }
                else
                {
                    var defaultMeasure = product.ProductMeasures.First(x => x.IsActive && x.Measure.IsBaseline);
                    product.SetDefaultMeasure(defaultMeasure.Measure);
                }
            }
        }