예제 #1
0
        public int EditProduct(string id, string name, string price, bool isAvailable, string brandId, string categoryId)
        {
            var idValidated       = ValidateId(id, "Id");
            var nameValidated     = ValidateName(name);
            var priceValidated    = ValidatePrice(price);
            var brandValidated    = ValidateId(brandId, "Brand");
            var categoryValidated = ValidateId(categoryId, "Category");

            var productDal = new ProductDal();

            return(productDal.EditProduct(idValidated, nameValidated, priceValidated, isAvailable, brandValidated, categoryValidated));
        }
예제 #2
0
        public async Task <IActionResult> EditAsync(IFormCollection collection)
        {
            try
            {
                var imageFile = Request?.Form.Files[0];

                if (imageFile != null && imageFile.Length > 0 && imageFile.ContentType.Contains("image"))
                {
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/product-images", imageFile.FileName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await imageFile.CopyToAsync(stream);
                    }
                }

                var productDto = new ProductDto();

                productDto.Id          = Convert.ToInt32(collection["editProductId"]);
                productDto.Name        = collection["editName"].ToString();
                productDto.Price       = Convert.ToDecimal(collection["editPrice"]);
                productDto.Barcode     = collection["editBarcode"].ToString();
                productDto.UserName    = User.Identity.Name;
                productDto.CategoryId  = Convert.ToInt32(collection["editCategoryId"]);
                productDto.Description = collection["editDescription"].ToString();
                productDto.Image       = string.IsNullOrEmpty(imageFile.FileName) ? string.Empty : $"{Request.Scheme}://{Request.Host}/images/product-images/{imageFile.FileName}";

                var result = _productDal.EditProduct(productDto);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }