public async Task <bool> UpdateCategory(ProductCategoryUpdateRequest categoryUpdateRequest)
        {
            var sessions   = _httpContextAccessor.HttpContext.Session.GetString(SystemConstants.AppSettings.Token);
            var languageId = _httpContextAccessor.HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId);
            var client     = _httpClientFactory.CreateClient();

            client.BaseAddress = new Uri(_configuration[SystemConstants.AppSettings.BaseAddress]);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sessions); //lấy token

            var requestContent = new MultipartFormDataContent();

            requestContent.Add(new StringContent(categoryUpdateRequest.SortOrder.ToString()), "SortOrder");
            requestContent.Add(new StringContent(categoryUpdateRequest.status.ToString()), "status");
            requestContent.Add(new StringContent(categoryUpdateRequest.ParentId.ToString()), "ParentId");
            requestContent.Add(new StringContent(categoryUpdateRequest.IsShowOnHome.ToString()), "IsShowOnHome");

            requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryUpdateRequest.Name) ? "" : categoryUpdateRequest.Name.ToString()), "Name");
            requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryUpdateRequest.SeoDescription) ? "" : categoryUpdateRequest.SeoDescription.ToString()), "SeoDescription");
            requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryUpdateRequest.SeoTitle) ? "" : categoryUpdateRequest.SeoTitle.ToString()), "SeoTitle");
            requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryUpdateRequest.SeoAlias) ? "" : categoryUpdateRequest.SeoAlias.ToString()), "SeoAlias");
            requestContent.Add(new StringContent(languageId), "LanguageId");

            var response = await client.PutAsync($"/api/categories/" + categoryUpdateRequest.CategoryId, requestContent);

            return(response.IsSuccessStatusCode);
        }
        public async Task <int> Update(ProductCategoryUpdateRequest request)
        {
            var category = await _context.Categories.FindAsync(request.CategoryId);

            var productCategoryTranslations = await _context.CategoryTranslations.FirstOrDefaultAsync(x => x.CategoryId == request.CategoryId &&
                                                                                                      x.LanguageId == request.LanguageId);

            if (category == null || productCategoryTranslations == null)
            {
                throw new EShopException($"Không thể tìm thấy một sản phẩm có id : {request.CategoryId}");
            }

            productCategoryTranslations.Name           = request.Name;
            productCategoryTranslations.SeoAlias       = request.SeoAlias;
            productCategoryTranslations.SeoDescription = request.SeoDescription;
            productCategoryTranslations.SeoTitle       = request.SeoTitle;
            productCategoryTranslations.CategoryId     = request.CategoryId;

            category.SortOrder    = request.SortOrder;
            category.IsShowOnHome = request.IsShowOnHome;
            category.ParentId     = request.ParentId;
            category.Status       = (Status)request.status;

            return(await _context.SaveChangesAsync());
        }
 public int Update(ProductCategoryUpdateRequest model)
 {
     Adapter.ExecuteQuery("dbo.Product_Category_Update", new[] {
         SqlDbParameter.Instance.BuildParameter("@CategoryName", model.CategoryName, System.Data.SqlDbType.NVarChar),
         SqlDbParameter.Instance.BuildParameter("@Description", model.Description, System.Data.SqlDbType.NVarChar),
         SqlDbParameter.Instance.BuildParameter("@IsActive", model.IsActive, System.Data.SqlDbType.Bit),
         SqlDbParameter.Instance.BuildParameter("@Id", model.Id, System.Data.SqlDbType.Int)
     });
     return(0);
 }
 public IHttpActionResult Put(ProductCategoryUpdateRequest model)
 {
     try
     {
         ItemResponse <int> response = new ItemResponse <int>
         {
             Item         = _productCategoryService.Update(model),
             IsSuccessful = true
         };
         return(Ok());
     }
     catch (Exception ex) { return(BadRequest(ex.Message)); }
 }
Exemplo n.º 5
0
        public async Task <IActionResult> Update([FromRoute] int categoryId, [FromForm] ProductCategoryUpdateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            request.CategoryId = categoryId;
            var affectedResult = await _categoryService.Update(request);

            if (affectedResult == 0)
            {
                return(BadRequest());
            }

            return(Ok());
        }
        public async Task <IActionResult> Edit([FromForm] ProductCategoryUpdateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(View(request));
            }
            var result = await _categoryApiClient.UpdateCategory(request);

            if (result)
            {
                //TempData["result"] = "Cập nhập sản phẩm thành công";
                _notyf.Success("Cập nhập sản phẩm thành công");
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Cập nhập sản phẩm thất bại");
            return(View(request));
        }
        public async Task <IActionResult> Edit(int id)
        {
            var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId);

            var category = await _categoryApiClient.GetById(id, languageId);

            ViewBag.categories = category;
            var editVm = new ProductCategoryUpdateRequest()
            {
                CategoryId     = category.Id,
                Name           = category.Name,
                SeoAlias       = category.SeoAlias,
                SeoDescription = category.SeoDescription,
                SeoTitle       = category.SeoTitle,
                SortOrder      = category.SortOrder,
                status         = (Status)category.status,
                ParentId       = category.ParentId,
                IsShowOnHome   = category.IsShowOnHome
            };

            return(View(editVm));
        }
        public async Task UpdateProductCategory_Success()
        {
            var             dbContext          = _fixture.Context;
            ProductCategory oldProductCategory = new ProductCategory {
                Name = "Test product category"
            };
            await dbContext.ProductCategories.AddAsync(oldProductCategory);

            await dbContext.SaveChangesAsync();

            ProductCategoryUpdateRequest newProductCategory = new ProductCategoryUpdateRequest {
                Name = "Test1 product category"
            };
            var controller = new ProductCategoriesController(dbContext);
            var result     = await controller.PutProductCategory(1, newProductCategory);

            var updatAtActionResult = Assert.IsType <OkObjectResult>(result.Result);
            var resultValue         = Assert.IsType <ProductCategoryUpdateRequest>(updatAtActionResult.Value);

            Assert.Equal(oldProductCategory.Name, resultValue.Name);
            Assert.Equal(oldProductCategory.Name, newProductCategory.Name);
        }
        public async Task <ActionResult <ProductCategoryVm> > PutProductCategory(short id, ProductCategoryUpdateRequest productCategoryUpdateRequest)
        {
            var productCategory = await _context.ProductCategories.FirstOrDefaultAsync(x => x.ID == id);

            productCategory.Name         = productCategoryUpdateRequest.Name;
            productCategory.ModifiedDate = DateTime.Now;
            productCategory.Status       = productCategoryUpdateRequest.Status;
            if (id != productCategory.ID)
            {
                return(BadRequest());
            }

            _context.Entry(productCategory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductCategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }