コード例 #1
0
        public async Task <IActionResult> Edit([FromForm] ProductWithCategoryViewModel productExtended, [FromForm] int id)
        {
            var role = await _context.UserRole.FirstOrDefaultAsync(role => role.Id == this.GetUserRole());

            if (role.CanEditProducts == false)
            {
                return(Forbid());
            }


            if (ModelState.IsValid)
            {
                if (productExtended.ID <= 0)
                {
                    return(BadRequest());
                }

                var product = _productExtendedRepository.ProductsExtended
                              .FirstOrDefault(p => p.ID == productExtended.ID);

                if (productExtended.Product.Product.Image != null)
                {
                    var imageName = DateTime.Now.ToString() + productExtended.Product.Product.Name;
                    var image     = await _imagesDb.StoreImage(productExtended.Product.Product.Image.OpenReadStream(),
                                                               imageName);

                    productExtended.Product.Product.ImageId = image;
                }

                product.Product.ID      = productExtended.Product.ProductIdentifier;
                product.Product.Name    = productExtended.Product.Product.Name;
                product.Product.ImageId = productExtended.Product.Product.ImageId;
                product.Product.Price   = productExtended.Product.Product.Price;
                var category = _categoryRepository.Categories.FirstOrDefault(c => c.ID == productExtended.Product.Product.Category.ID);
                product.Product.Category    = category;
                product.Product.Description = productExtended.Product.Product.Description;
                product.LongDescription     = productExtended.Product.LongDescription;
                product.Manufacturer        = productExtended.Product.Manufacturer;
                product.OriginCountry       = productExtended.Product.OriginCountry;
                product.Image    = productExtended.Product.Image;
                product.Comments = productExtended.Product.Comments;

                try
                {
                    _productRepository.SaveProduct(product.Product);
                    _productExtendedRepository.SaveProductExtended(product);
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    return(BadRequest("The product is already editing"));
                }

                return(Ok());
            }
            else
            {
                // there is something wrong with the data values
                return(BadRequest(productExtended));
            }
        }
コード例 #2
0
        public async Task <IActionResult> Edit(ProductWithCategoryViewModel productExtended, int id)
        {
            try
            {
                HttpResponseMessage response = null;

                using (var httpClient = new HttpClient())
                {
                    MultipartFormDataContent data = new MultipartFormDataContent();

                    productExtended.Product.Product.ImageId = String.Empty;

                    if (productExtended.Product.Product.Image != null)
                    {
                        var imageName = DateTime.Now.ToString() + productExtended.Product.Product.Name;
                        var image     = await _imagesDb.StoreImage(productExtended.Product.Product.Image.OpenReadStream(),
                                                                   imageName);

                        productExtended.Product.Product.ImageId = image;
                    }

                    data.Add(new StringContent(id.ToString()), "ID");
                    data.Add(new StringContent(productExtended.Product.ID.ToString()), "Product.ID");
                    data.Add(new StringContent(productExtended.Product.ProductIdentifier.ToString()), "Product.ProductIdentifier");
                    data.Add(new StringContent(productExtended.Product.LongDescription), "Product.LongDescription");
                    data.Add(new StringContent(productExtended.Product.Manufacturer), "Product.Manufacturer");
                    data.Add(new StringContent(productExtended.Product.OriginCountry), "Product.OriginCountry");
                    data.Add(new StringContent(productExtended.Product.Product.ID.ToString()), "Product.Product.ID");
                    data.Add(new StringContent(productExtended.Product.Product.Name), "Product.Product.Name");
                    data.Add(new StringContent(productExtended.Product.Product.Description), "Product.Product.Description");
                    data.Add(new StringContent(productExtended.Product.Product.Price.ToString()), "Product.Product.Price");
                    data.Add(new StringContent(productExtended.Product.Product.ImageId), "Product.Product.ImageId");
                    data.Add(new StringContent(productExtended.Product.Product.Category.ID.ToString()), "Product.Product.Category.ID");


                    httpClient.DefaultRequestHeaders.Authorization =
                        new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", TokenKeeper.Token);
                    response = await httpClient.PutAsync(_apiPath, data);

                    if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        return(View(productExtended));
                    }

                    TempData["message"] = $"{productExtended.Product.Product.Name} был сохранен";

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #3
0
        public ActionResult Category(int type)
        {
            var products = _service.GetProductsByCategory(type);

            ViewBag.CategoryNumber = type;
            var model = new ProductWithCategoryViewModel
            {
                Category = (ProductCategoryEnum)type,
                Products = products
            };

            return(View("ProductsWithCategory", model));
        }
コード例 #4
0
        public async Task <IActionResult> Create([FromForm] ProductWithCategoryViewModel productExtended)
        {
            var role = await _context.UserRole.FirstOrDefaultAsync(role => role.Id == this.GetUserRole());

            if (role.CanAddProducts == false)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                if (productExtended.Product.Product.Image != null)
                {
                    var imageName = DateTime.Now.ToString() + productExtended.Product.Product.Name;
                    var image     = await _imagesDb.StoreImage(productExtended.Product.Product.Image.OpenReadStream(),
                                                               imageName);

                    productExtended.Product.Product.ImageId = image;
                }

                productExtended.Product.Product.Category = _categoryRepository.Categories
                                                           .FirstOrDefault(c => c.ID == productExtended.Product.Product.Category.ID);

                _productExtendedRepository.SaveProductExtended(productExtended.Product);

                _stockRepository.SaveStockItem(new Stock {
                    Product = productExtended.Product.Product, Quantity = 0
                });

                return(Ok());
            }
            else
            {
                return(BadRequest(productExtended));
            }
        }