Пример #1
0
        /// <summary>
        /// Изменяет сущность продукта и асинхронно сохраняет ее.
        /// </summary>
        public async Task <OperationResult> Edit(ProductCreateDTO editModel)
        {
            if (editModel?.Product == null ||
                !await Database.Products.Select(p => p.Id).ContainsAsync(editModel.Product.Id))
            {
                return(OperationResult(ResultType.Error, "Product not found."));
            }

            List <string> messages = new List <string>();

            var product = new Product
            {
                Id          = editModel.Product.Id,
                BrandId     = editModel.Product.BrandId,
                CategoryId  = editModel.Product.CategoryId,
                CountryId   = editModel.Product.CountryId,
                Description = editModel.Product.Description,
                Name        = editModel.Product.Name,
                Price       = editModel.Product.Price,
                Removed     = editModel.Product.Removed,
                Weight      = editModel.Product.Weight
            };

            if (product.CategoryId.HasValue)
            {
                bool categoryExists = await Database.Categories
                                      .Select(c => c.Id)
                                      .ContainsAsync(product.CategoryId.Value);

                if (!categoryExists)
                {
                    messages.Append("The category does not exist in the system.");
                    product.CategoryId = null;
                }
            }

            if (product.BrandId.HasValue)
            {
                bool brandExists = await Database.Brands
                                   .Select(c => c.Id)
                                   .ContainsAsync(product.BrandId.Value);

                if (!brandExists)
                {
                    messages.Append("The brand does not exist in the system.");
                    product.BrandId = null;
                }
            }

            if (product.CountryId.HasValue)
            {
                bool countryExists = await Database.Countries
                                     .Select(c => c.Id)
                                     .ContainsAsync(product.CountryId.Value);

                if (!countryExists)
                {
                    messages.Append("The country does not exist in the system.");
                    product.CountryId = null;
                }
            }

            if (editModel.ProductCharacteristics != null && editModel.ProductCharacteristics.Count > 0)
            {
                List <string> availableChars = await Database.Characteristics
                                               .Select(c => c.Name).ToListAsync();

                var validChars = new Dictionary <string, string>();

                foreach (var characteristic in editModel.ProductCharacteristics)
                {
                    if (!availableChars.Contains(characteristic.Key))
                    {
                        messages.Append($"Characteristic {characteristic} does not exists.");
                        continue;
                    }

                    validChars.Add(characteristic.Key, characteristic.Value);
                }

                product.Character = GetCharacteristicsFromDictionary(validChars);
            }

            string oldFilePath = await Database.Products.Where(p => p.Id == product.Id)
                                 .Select(p => p.Image).FirstOrDefaultAsync();

            if (!string.IsNullOrWhiteSpace(oldFilePath))
            {
                OperationResult deleteResult = Database.ProductsImages.Delete(oldFilePath);

                if (deleteResult.Type != ResultType.Success &&
                    deleteResult.Messages != null && deleteResult.Any())
                {
                    messages.AddRange(deleteResult);
                }
            }

            if (editModel.ProductImage != null && editModel.ProductImage.Length > 0)
            {
                ImageSaveResult saveResult = await Database.ProductsImages.Save(editModel.ProductImage);

                if (saveResult.Type == ResultType.Success && !string.IsNullOrWhiteSpace(saveResult.OutputPath))
                {
                    product.Image = saveResult.OutputPath;
                }
                else if (saveResult.Messages != null && saveResult.Any())
                {
                    messages.AddRange(saveResult);
                }
            }

            Database.Products.Update(product);
            await Database.SaveChangesAsync();

            ResultType type = messages.Any()
                ? ResultType.Warning
                : ResultType.Success;

            return(OperationResult(type, messages.ToArray()));
        }
Пример #2
0
        public async Task <OperationResult> CreateAsync(ProductCreateDTO createModel)
        {
            List <string> messages = new List <string>();

            var product = new Product
            {
                CategoryId  = createModel.Product.CategoryId,
                CountryId   = createModel.Product.CountryId,
                BrandId     = createModel.Product.BrandId,
                Description = createModel.Product.Description,
                Name        = createModel.Product.Name,
                Price       = createModel.Product.Price,
                Weight      = createModel.Product.Weight
            };

            if (product.CategoryId.HasValue)
            {
                bool categoryExists = await Database.Categories
                                      .Select(c => c.Id)
                                      .ContainsAsync(product.CategoryId.Value);

                if (!categoryExists)
                {
                    messages.Append("The category does not exist in the system.");
                    product.CategoryId = null;
                }
            }

            if (product.BrandId.HasValue)
            {
                bool brandExists = await Database.Brands
                                   .Select(c => c.Id)
                                   .ContainsAsync(product.BrandId.Value);

                if (!brandExists)
                {
                    messages.Append("The brand does not exist in the system.");
                    product.BrandId = null;
                }
            }

            if (product.CountryId.HasValue)
            {
                bool countryExists = await Database.Countries
                                     .Select(c => c.Id)
                                     .ContainsAsync(product.CountryId.Value);

                if (!countryExists)
                {
                    messages.Append("The country does not exist in the system.");
                    product.CountryId = null;
                }
            }

            if (createModel.ProductCharacteristics != null && createModel.ProductCharacteristics.Count > 0)
            {
                List <string> availableChars = await Database.Characteristics
                                               .Select(c => c.Name).ToListAsync();

                var validChars = new Dictionary <string, string>();

                foreach (var characteristic in createModel.ProductCharacteristics)
                {
                    if (!availableChars.Contains(characteristic.Key))
                    {
                        messages.Append($"Characteristic {characteristic} does not exists.");
                        continue;
                    }

                    validChars.Add(characteristic.Key, characteristic.Value);
                }

                product.Character = GetCharacteristicsFromDictionary(validChars);
            }

            if (createModel.ProductImage != null && createModel.ProductImage.Length > 0)
            {
                ImageSaveResult saveResult = await Database.ProductsImages.Save(createModel.ProductImage);

                if (saveResult.Type == ResultType.Success && !string.IsNullOrWhiteSpace(saveResult.OutputPath))
                {
                    product.Image = saveResult.OutputPath;
                }
                else if (saveResult.Messages != null && saveResult.Any())
                {
                    messages.AddRange(saveResult.Messages);
                }
            }

            await Database.Products.CreateAsync(product);

            await Database.SaveChangesAsync();

            ResultType type = messages.Any()
                ? ResultType.Warning
                : ResultType.Success;

            return(OperationResult(type, messages.ToArray()));
        }