Пример #1
0
        [ValidateAntiForgeryToken] //thuoc tinh ngan chan mot yeu cau gia mao
        public ActionResult Create(ItemProduct item)
        {
            if (String.IsNullOrEmpty(item.Id))
            {
                ModelState.AddModelError(nameof(item.Id), "Product's ID is required");      //thong bao loi khi Name co gia tri null/ rong
            }
            if (String.IsNullOrEmpty(item.Name))
            {
                ModelState.AddModelError(nameof(item.Name), "The name is required");      //thong bao loi khi Name co gia tri null/ rong
            }
            if (String.IsNullOrEmpty(item.Price.ToString()))
            {
                ModelState.AddModelError(nameof(item.Price), "Product's price is required");      //thong bao loi khi Name co gia tri null/ rong
            }
            if (String.IsNullOrEmpty(item.Amount.ToString()))
            {
                ModelState.AddModelError(nameof(item.Amount), "Amount is required");      //thong bao loi khi Name co gia tri null/ rong
            }
            if (String.IsNullOrEmpty(item.BranchId))
            {
                ModelState.AddModelError(nameof(item.BranchId), "Branch's ID is required");      //thong bao loi khi Name co gia tri null/ rong
            }
            if (String.IsNullOrEmpty(item.ClassifyId))
            {
                ModelState.AddModelError(nameof(item.ClassifyId), "Classify's ID is required");
                //thong bao loi khi Name co gia tri null/ rong
            }
            if (String.IsNullOrEmpty(item.ProviderId))
            {
                ModelState.AddModelError(nameof(item.ProviderId), "Provider's ID is required");      //thong bao loi khi Name co gia tri null/ rong
            }


            if (ModelState.IsValid)     //Name hop le
            {
                Product p = new Product();
                p.Id         = item.Id;
                p.Name       = item.Name;
                p.BranchId   = item.BranchId;
                p.Amount     = item.Amount;
                p.ClassifyId = item.ClassifyId;
                p.ProviderId = item.ProviderId;
                p.Details    = item.Details;
                p.Discount   = item.Discount;
                p.Price      = item.Price;

                string image1 = Guid.NewGuid() + Path.GetExtension(item.Image1.FileName);
                item.Image1.SaveAs(filename: Server.MapPath("~/Images" + image1));
                p.Image1 = item.Image1.ToString();
                string image2 = Guid.NewGuid() + Path.GetExtension(item.Image2.FileName);
                item.Image2.SaveAs(filename: Server.MapPath("~/Images" + image1));
                p.Image2 = item.Image2.ToString();
                string image3 = Guid.NewGuid() + Path.GetExtension(item.Image3.FileName);
                item.Image3.SaveAs(filename: Server.MapPath("~/Images" + image1));
                p.Image3 = item.Image3.ToString();
                db.Add(p);
                return(RedirectToAction("Details", new { id = p.Id }));
            }
            return(View());
        }
Пример #2
0
 private void CreateProduct()
 {
     productData.Add(Product);
     Product.ImgPath = Utilities.Utilities.AddImageFromFile(Product, webHostEnvironment.WebRootPath, Image);
     RecalculatePrice();
     Product.ImgPath = Product.ImgPath ?? "gorecerydefault.jpg";
 }
Пример #3
0
        public IActionResult Add(ProductEditModel model)
        {
            if (ModelState.IsValid)
            {
                var newProduct = new ProductInventory();
                newProduct.Name     = model.Name;
                newProduct.Price    = model.Price;
                newProduct.Quantity = model.Quantity;
                newProduct.Type     = model.Type;

                newProduct = _productData.Add(newProduct);
                return(RedirectToAction(nameof(List)));
            }
            else
            {
                return(View());
            }
        }
 public ActionResult Create(Product product)
 {
     if (ModelState.IsValid)
     {
         db.Add(product);
         return(RedirectToAction("Details", new { id = product.ID }));
     }
     return(View());
 }
Пример #5
0
 public IActionResult Add([FromBody] Product product)
 {
     try
     {
         return(Ok(_productData.Add(product)));
     }
     catch (Exception ex)
     {
         return(Problem(ex.Message, nameof(Add), StatusCodes.Status500InternalServerError, "Fail adding the product."));
     }
 }
Пример #6
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            productData.Add(Product);
            productData.Commit();

            return(RedirectToPage("./List"));
        }
Пример #7
0
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                productData.Add(product);
                return(RedirectToAction("Index"));
            }
            ViewBag.TypeId   = new SelectList(typeData.GetAll(), "TypeId", "Name", product.TypeId);
            ViewBag.AuthorId = new SelectList(authorData.GetAll(), "AuthorId", "AuthorName", product.AuthorId);

            return(View(product));
        }
Пример #8
0
        public IActionResult Add(ProductDTO product)
        {
            var prod = product.FromDTO();

            prod.SectionId = prod.Section.Id;
            prod.Section   = null;
            prod.BrandId   = prod.Brand?.Id;
            prod.Brand     = null;
            var id = _ProductData.Add(prod);

            return(Ok(id));
        }
Пример #9
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            if (Product.Id > 0)
            {
                Product = productData.Update(Product);
            }
            else
            {
                productData.Add(Product);
            }

            productData.Commit();
            TempData["Message"] = "Product saved!";
            return(RedirectToPage("./Details", new { productId = Product.Id }));
        }
Пример #10
0
        public IActionResult Post(ProductDTO dto)
        {
            try
            {
                Product = ApiRepository.DtoToProduct(dto);

                productData.Add(Product);

                if (productData.Commit() > 0)
                {
                    return(CreatedAtAction("GetById", new { id = Product.ProductId }, Product));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }
Пример #11
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                Categories = htmlHelper.GetEnumSelectList <Category>();
                return(Page());
            }

            if (Product.Id > 0)
            {
                productData.Update(Product);
            }
            else
            {
                productData.Add(Product); productData.Commit();
            }

            productData.Commit();
            TempData["Message"] = "Product Saved";
            return(RedirectToPage("./Detail", new { productId = Product.Id }));
        }
Пример #12
0
        public async Task <IActionResult> EndCreate(ProductCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Create", model));
            }

            model.Product.ImageUrl = "noimage.jpg";

            if (model.Image != null)
            {
                // путь к папке Files
                string fileName = model.Image.FileName;
                string path     = "/images/shop/" + fileName;

                // сохраняем файл в папку Files в каталоге wwwroot
                model.Product.ImageUrl     = fileName;
                await using var fileStream = new FileStream(appEnvironment.WebRootPath + path, FileMode.Create);
                await model.Image.CopyToAsync(fileStream);
            }

            var brand   = productData.GetBrandById(model.Product.BrandId !.Value);
            var section = productData.GetSectionById(model.Product.SectionId);

            model.Product.Brand          = brand.FromDto();
            model.Product.Section        = section.FromDto();
            model.Product.Brand.Products = productData.GetProducts().Products
                                           .FromDto()
                                           .Where(x => x.BrandId == brand.Id)
                                           .ToList();
            model.Product.Section.Products = productData.GetProducts().Products
                                             .FromDto()
                                             .Where(prod => prod.SectionId == section.Id)
                                             .ToList();

            productData.Add(model.Product.ToDto());
            return(RedirectToAction(nameof(Index)));
        }
Пример #13
0
 private void ProcessProductChanges()
 {
     if (Product.Id > 0)
     {
         if (Photos.Count > 0)
         {
             Product.Photos = ProcessUploadedFile(Product.Id);
             productData.DeletePhotos(Product.Id);
             Product = productData.Update(Product);
         }
         else
         {
             Product = productData.Update(Product);
         }
     }
     else
     {
         Product = productData.Add(Product);
         productData.Commit();
         Product.Photos = ProcessUploadedFile(Product.Id);
     }
     productData.Commit();
 }
Пример #14
0
 public static bool Add(int productId, string tableName, ProductDataInfo dataInfo)
 {
     return(dal.Add(productId, tableName, dataInfo));
 }
Пример #15
0
 public IEnumerable <ProductInventory> AddCustomer([FromBody] ProductInventory model)
 {
     _productData.Add(model);
     return(_productData.GetAll());
 }