public async Task <bool> CreateCategory(ProductCategoryCreateRequest categoryCreateRequest) { 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(categoryCreateRequest.SortOrder.ToString()), "SortOrder"); requestContent.Add(new StringContent(categoryCreateRequest.IsShowOnHome.ToString()), "IsShowOnHome"); requestContent.Add(new StringContent(categoryCreateRequest.status.ToString()), "status"); requestContent.Add(new StringContent(categoryCreateRequest.ParentId.ToString()), "ParentId"); requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryCreateRequest.Name) ? "" : categoryCreateRequest.Name.ToString()), "Name"); requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryCreateRequest.SeoDescription) ? "" : categoryCreateRequest.SeoDescription.ToString()), "SeoDescription"); requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryCreateRequest.SeoTitle) ? "" : categoryCreateRequest.SeoTitle.ToString()), "SeoTitle"); requestContent.Add(new StringContent(string.IsNullOrEmpty(categoryCreateRequest.SeoAlias) ? "" : categoryCreateRequest.SeoAlias.ToString()), "SeoAlias"); requestContent.Add(new StringContent(languageId), "LanguageId"); var response = await client.PostAsync($"/api/categories/", requestContent); return(response.IsSuccessStatusCode); }
public async Task PostProductCategory_Success() { var dbContext = _fixture.Context; var productcategory = new ProductCategoryCreateRequest { Name = "Test product category" }; var controller = new ProductCategoriesController(dbContext); var result = await controller.PostProductCategory(productcategory); var createdActionResult = Assert.IsType <CreatedAtActionResult>(result.Result); var returnValue = Assert.IsType <ProductCategoryVm>(createdActionResult.Value); Assert.Equal("Tests product category", returnValue.Name); }
public async Task <IActionResult> Create([FromForm] ProductCategoryCreateRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var categoryId = await _categoryService.Create(request); if (categoryId == 0) { return(BadRequest());//400 } var product = await _categoryService.GetById(categoryId, request.LanguageId); return(CreatedAtAction(nameof(GetById), new { id = categoryId }, product)); }
public async Task <ActionResult <ProductCategory> > PostProductCategory(ProductCategoryCreateRequest productCategoryCreateRequest) { var productCategory = new ProductCategory { Name = productCategoryCreateRequest.Name, MetaTitle = productCategoryCreateRequest.MetaTitle, DisplayOrder = productCategoryCreateRequest.DisplayOrder, Status = productCategoryCreateRequest.Status, ShowOnHome = productCategoryCreateRequest.ShowOnHome }; _context.ProductCategories.Add(productCategory); await _context.SaveChangesAsync(); return(CreatedAtAction("GetProductCategory", new { id = productCategory.ID }, productCategory)); }
public async Task <IActionResult> Create([FromForm] ProductCategoryCreateRequest request) { if (!ModelState.IsValid) { return(View(request)); } var result = await _categoryApiClient.CreateCategory(request); if (result) { //TempData["result"] = "Thêm mới danh mục sản phẩm thành công"; _notyf.Success("Thêm mới danh mục sản phẩm thành công"); return(RedirectToAction("Index")); } ModelState.AddModelError("", "Thêm danh mục sản phẩm thất bại"); return(View(request)); }
public async Task <int> Create(ProductCategoryCreateRequest request) { var languages = _context.Languages; var translations = new List <CategoryTranslation>(); foreach (var language in languages) { if (language.Id == request.LanguageId) { //create CategoryTranslations translations.Add(new CategoryTranslation() { Name = request.Name, SeoDescription = request.SeoDescription, SeoAlias = request.SeoAlias, SeoTitle = request.SeoTitle, LanguageId = request.LanguageId }); } else { translations.Add(new CategoryTranslation() { Name = CategoryConstants.NA, SeoAlias = CategoryConstants.NA, LanguageId = language.Id }); } } var category = new Category() { SortOrder = request.SortOrder, IsShowOnHome = request.IsShowOnHome, Status = (Status)request.status, CategoryTranslations = translations }; _context.Categories.Add(category); await _context.SaveChangesAsync(); return(category.Id); }