예제 #1
0
 public void SaveChanges()
 {
     _koopDbContext.SaveChanges();
 }
예제 #2
0
        public ProblemResponse UpdateProduct(Product product, IFormFile picture)
        {
            string dirPath = "Resources/ProductImgs";

            ProblemResponse problemResponse = new ProblemResponse()
            {
                Detail = "Wystąpił błąd nieznanego pochodzenia",
                Status = 500
            };

            using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
            try
            {
                Product updatedProduct;
                if (product.ProductId == Guid.Empty)
                {
                    Product newProductTmp = new Product();
                    var     newProduct    = _mapper.Map(product, newProductTmp);
                    _koopDbContext.Products.Add(newProduct);
                    updatedProduct = newProduct;
                }
                else
                {
                    var productExist = _koopDbContext.Products.SingleOrDefault(p => p.ProductId == product.ProductId);

                    if (productExist is null)
                    {
                        throw new Exception($"Nie znaleziono produktu o podanym Id: {product.ProductId}");
                    }

                    var productUpdated = _mapper.Map(product, productExist);
                    _koopDbContext.Products.Update(productUpdated);
                    updatedProduct = productUpdated;
                }

                _koopDbContext.SaveChanges();

                Console.WriteLine($"productID: {updatedProduct.ProductId}");

                if (updatedProduct.Category.Any())
                {
                    var currentProductCategories =
                        _koopDbContext.ProductCategories.Where(p => p.ProductId == product.ProductId).Include(p => p.Category);

                    var categoriesToAdd    = updatedProduct.Category.Where(p => !currentProductCategories.Select(q => q.Category.CategoryName).Contains(p.CategoryName));
                    var categoriesToRemove = currentProductCategories.Where(p =>
                                                                            !updatedProduct.Category.Select(q => q.CategoryName).Contains(p.Category.CategoryName));

                    foreach (var category in categoriesToAdd)
                    {
                        ProductCategory productCategory = new ProductCategory()
                        {
                            CategoryId = category.CategoryId,
                            ProductId  = updatedProduct.ProductId
                        };

                        _koopDbContext.ProductCategories.Add(productCategory);
                    }

                    _koopDbContext.ProductCategories.RemoveRange(categoriesToRemove);
                }

                if (updatedProduct.AvailQuantity.Any())
                {
                    var currentAvailableQuantities =
                        _koopDbContext.AvailableQuantities.Where(p => p.ProductId == product.ProductId);

                    var availQuantToAdd = updatedProduct.AvailQuantity.Where(p =>
                                                                             !currentAvailableQuantities.Select(q => q.Quantity).Contains(p.Quantity));
                    var availQuantToRemove = currentAvailableQuantities.Where(p =>
                                                                              !updatedProduct.AvailQuantity.Select(q => q.Quantity).Contains(p.Quantity));

                    foreach (var quantity in availQuantToAdd)
                    {
                        AvailableQuantity availableQuantity = new AvailableQuantity()
                        {
                            // AvailableQuantityId = Guid.Empty,
                            ProductId = updatedProduct.ProductId,
                            Quantity  = quantity.Quantity
                        };

                        _koopDbContext.AvailableQuantities.Add(availableQuantity);
                    }

                    _koopDbContext.AvailableQuantities.RemoveRange(availQuantToRemove);
                }

                if (picture is not null && picture.Length > 0)
                {
                    var fileExtension = picture.FileName.Split('.');
                    if (fileExtension.Length != 2)
                    {
                        throw new Exception($"Problem z obrazkiem - za dużo kropek");
                    }

                    var fileName = $"{updatedProduct.ProductId}.{fileExtension[1]}";

                    var fullPath = Path.Combine(dirPath, fileName);

                    // Remove files with the same name
                    var filesWithSameName = Directory.GetFiles(dirPath, $"{updatedProduct.ProductId}*");
                    foreach (var file in filesWithSameName)
                    {
                        File.Delete(file);
                    }

                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        picture.CopyTo(stream);
                    }

                    updatedProduct.Picture = fullPath;
                    _koopDbContext.Products.Update(updatedProduct);
                    _koopDbContext.SaveChanges();
                }
                else
                {
                    if (updatedProduct.Picture is null || updatedProduct.Picture.Equals(String.Empty))
                    {
                        var filesWithSameName = Directory.GetFiles(dirPath, $"{updatedProduct.ProductId}*");
                        foreach (var file in filesWithSameName)
                        {
                            File.Delete(file);
                        }
                    }
                }

                _koopDbContext.SaveChanges();
                problemResponse.Detail = "Dane zostały poprawnie zapisane";
                problemResponse.Status = 200;
                scope.Complete();
            }