public IActionResult Post([FromBody] ProductViewModel product)
        {
            bool   result   = true;
            string errorMsg = "";
            var    created  = eFProductManager.SaveProduct(mapper.Map <ProductViewModel, Product>(product));

            result   = created.IsSuccess;
            errorMsg = created.ErrorMessage;
            return(Json(new { result, errorMsg }));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.LogoFile != null)
                {
                    string absPath  = uploadsFile + "/" + Guid.NewGuid().ToString() + Path.GetExtension(model.LogoFile.FileName);
                    string filePath = env.WebRootPath + absPath;
                    string savePath = await fileManager.SaveFileAsync(model.LogoFile, filePath);

                    model.LogoPath = string.IsNullOrEmpty(savePath) ? "" : absPath;
                }
                bool    isCreate = true;
                Product product  = mapper.Map <ProductViewModel, Product>(model);
                if (product.ProductID > 0)
                {
                    isCreate = false;
                }
                var res = repo.SaveProduct(product);
                if (res.IsSuccess)
                {
                    if (isCreate)
                    {
                        string cookieHubConnectionID = httpContextAccessor.HttpContext.Request.Cookies.ContainsKey("hubConnectionId") ?
                                                       httpContextAccessor.HttpContext.Request.Cookies["hubConnectionId"] : "";
                        if (!string.IsNullOrEmpty(cookieHubConnectionID))
                        {
                            await hubContext.Clients.AllExcept(cookieHubConnectionID).SendAsync("CreateProductNotify", $"Добавлен новый товар {product.Name}");
                        }
                    }
                    TempData["message"] = $"{model.Name} has been saved";
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", res.ErrorMessage);
                    return(View(model));
                }
            }
            else
            {
                return(View(model));
            }
        }