public async Task <IActionResult> Save(string strCategory) { JavaScriptSerializer serializer = new JavaScriptSerializer(); CategoryVm categoryVm = serializer.Deserialize <CategoryVm>(strCategory); bool status = false; if (categoryVm.Id == 0) { var request = new CategoryCreateRequest(); request.Name = categoryVm.Name; request.Description = categoryVm.Description; request.UserId = new Guid("69bd714f-9576-45ba-b5b7-f00649be00de"); request.CreatedDate = DateTime.Now; status = await _categoryApiClient.Add(request); } else { var request = new CategoryUpdateRequest(); request.Id = categoryVm.Id; request.Name = categoryVm.Name; request.Description = categoryVm.Description; request.UserId = new Guid("69bd714f-9576-45ba-b5b7-f00649be00de"); request.CreatedDate = DateTime.Now; status = await _categoryApiClient.Update(request); } return(Json(new { status = status })); }
public async Task <IActionResult> Create([Bind("Id,Name,CategoryOrder")] CategoryVm categoryVm) { if (ModelState.IsValid) { Category catetegoryFromDb = _context.Categories.Where(c => c.Name == categoryVm.Name).SingleOrDefault(); if (catetegoryFromDb != null) { categoryVm.StatusMessage = $"Error : {categoryVm.Name} already exists"; return(View(categoryVm)); } Category category = new Category() { Name = categoryVm.Name, CategoryOrder = categoryVm.CategoryOrder }; _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(categoryVm)); }
public CategoryVm GetCategory(Expression <Func <Category, bool> > filterPredicate = null) { Category category = _uow.Repository <Category>().Get(filterPredicate, false, null); CategoryVm categoryVm = Mapper.Map <CategoryVm>(category); return(categoryVm); }
public ActionResult EditCategory(HttpRequestMessage request, CategoryVm category) { try { if (ModelState.IsValid) { if (category.Id == 0) { _categoryRepository.EditCategory(category); } else { _categoryRepository.UpdateCategory(category); } return(new JsonResult()); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { validationErrors = GetErrorMessages() })); } catch (Exception ex) { var message = "Error creating category"; Logger.Error(message, ex); Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(Json(new { exceptionMessage = message })); } }
public void AddOrUpdateCategory(CategoryVm categoryVm) { var category = Mapper.Map <Category>(categoryVm); _uow.Repository <Category>().AddOrUpdate(x => x.CategoryID == category.CategoryID, category); _uow.Save(); }
public async Task <IActionResult> Edit(int id, CategoryVm category, string[] SelectedFolders) { if (id != category.CategoryId) { return(NotFound()); } RemoveFolders(id); if (ModelState.IsValid) { try { Category model = _mapper.Map <Category>(category); AddFolder(model, SelectedFolders); _context.Update(model); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(category.CategoryId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ParentId"] = new SelectList(_context.Category, "CategoryId", "CategoryId", category.ParentId); return(View(category)); }
public async Task <IActionResult> DeleteCategory(int id) { //// GET CATEGORY WITH ID (KEY) var category = await _context.Categories.FindAsync(id); //// IF KEY NOT EXIST (CATEGORY IS NULL), RETURN STATUS 404 if (category == null) { return(NotFound(new ApiNotFoundResponse($"Category with id: {id} is not found"))); } //// REMOVE CATEGORY FROM DATABASE AND SAVE CHANGE _context.Categories.Remove(category); var result = await _context.SaveChangesAsync(); //// IF RESULT AFTER DELETE IS GREATER THAN 0 (TRUE), RETURN HTTP STATUS 200, ELSE RETURN STATUS 400 if (result > 0) { await _cacheService.RemoveAsync(CacheConstants.Categories); CategoryVm categoryvm = CreateCategoryVm(category); return(Ok(categoryvm)); } return(BadRequest(new ApiBadRequestResponse("Delete category failed"))); }
public CategoryVm Get(int id) { Category model = this.Context.Categories.Find(id); CategoryVm vm = Mapper.Map <Category, CategoryVm>(model); return(vm); }
public async Task <IActionResult> Edit(int id, [Bind("CategoryID,CategoryName,Description,Picture")] CategoryVm Category) { if (id != Category.CategoryId) { return(NotFound()); } if (ModelState.IsValid) { try { await _CategoryMapper.BlUpdateAsync(Category); } catch (DbUpdateConcurrencyException) { var Exists = _CategoryMapper.CategoryExists((Category.CategoryId)); if (!Exists) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(Category)); }
public ActionResult Category(CategoryVm model) { if (ModelState.IsValid) { Entity.EntityModels.Category category = Mapper.Map <Entity.EntityModels.Category>(model); if (category.Id == 0) { ViewBag.message = categoryManager.Save(category); } else { ViewBag.message = categoryManager.Update(category); } model = new CategoryVm(); ModelState.Clear(); } List <Category> categories = categoryManager.GetAllCategories(); ViewBag.Category = categories; List <Common> brands = commonManager.GetAllBrand(); ViewBag.Brand = GetItemForBrandDropdownList(); return(View(model)); }
public ActionResult Add() { if (CheckCookies()) { var viewModel = new CategoryVm(); return(View("CategoryForm", viewModel)); } return(RedirectToAction("Index", "Home", new { area = "" })); }
public void DeleteCategory(CategoryVm categoryVm) { //var category = Mapper.Map<Category>(categoryVm); //Category caategory; var category = _uow.Repository <Category>().Get(x => x.CategoryID == categoryVm.CategoryID); _uow.Repository <Category>().Delete(category); _uow.Save(); }
public void Update(CategoryVm categoryVm) { var category = categoryRepository.GetById(categoryVm.CategoryId); category.CategoryName = categoryVm.CategoryName; category.Description = categoryVm.Description; category.ModifiedBy = categoryVm.ModifiedBy; category.ModifiedTime = categoryVm.ModifiedTime; category.MetaTitle = categoryVm.MetaTitle; categoryRepository.Update(category); }
public IHttpActionResult Get(int id) { if (!this._service.CategoryExists(id)) { return(this.NotFound()); } CategoryVm vm = this._service.Get(id); return(this.Ok(vm)); }
public async Task <IActionResult> Create([Bind("CategoryID,CategoryName,Description,Picture")] CategoryVm post) { if (ModelState.IsValid) { await _CategoryMapper.BlInser(post); return(RedirectToAction(nameof(Index))); } return(View(post)); }
public void Add(CategoryVm categoryVm) { var category = new Category(); category.CategoryName = categoryVm.CategoryName; category.Description = categoryVm.Description; category.CreatedBy = categoryVm.CreatedBy; category.CreatedTime = categoryVm.CreatedTime; category.MetaTitle = categoryVm.MetaTitle; category.Status = categoryVm.Status; categoryRepository.Add(category); }
public static SearchVm FromModel(Search search) { return new SearchVm { Category = CategoryVm.FromModel(search.Category), SubCategory = SubCategoryVm.FromModel(search.SubCategory), Keywords = search.Keywords, City = search.City, State = search.State, ZipCode = search.ZipCode }; }
public IActionResult Create(CategoryVm categoryVm) { if (ModelState.IsValid) { _categoryService.AddOrUpdateCategory(categoryVm); return(RedirectToAction("Index")); } else { return(View(ModelState)); } }
public async Task <IActionResult> GetById(int id) { var category = await _context.Categories.FindAsync(id); if (category == null) { return(NotFound(new ApiNotFoundResponse($"Category with id: {id} is not found"))); } CategoryVm categoryvm = CreateCategoryVm(category); return(Ok(categoryvm)); }
public ActionResult Edit(CategoryVm categoryVm) { if (!ModelState.IsValid) { return(View(categoryVm)); } categoryVm.ModifiedBy = Convert.ToInt32(Session[UserSession.UserId]); categoryVm.ModifiedTime = DateTime.Now; categoryVm.MetaTitle = StringHelper.VNDecode(categoryVm.CategoryName); categoryService.Update(categoryVm); TempData["result"] = "Cập nhật thành công"; return(RedirectToAction("Index")); }
public CategoryVm GetCategoryVm(int id) { var category = categoryRepository.GetById(id); var categoryVm = new CategoryVm(); categoryVm.CategoryId = category.CategoryId; categoryVm.CategoryName = category.CategoryName; categoryVm.Description = category.Description; categoryVm.CreatedBy = category.CreatedBy; categoryVm.CreatedTime = category.CreatedTime; categoryVm.Status = category.Status; return(categoryVm); }
// GET api/org public IQueryable <CategoryVm> GetAllCategories() { lock (categoryService) { var models = this.categoryService.GetCategories(); var result = new List <CategoryVm>(); foreach (var category in models) { result.Add(CategoryVm.FromModel(category)); } return(result.AsQueryable <CategoryVm>()); } }
public async Task <IActionResult> Create(CategoryVm category, string[] CategoryFolder) { if (ModelState.IsValid) { Category model = _mapper.Map <Category>(category); AddFolder(model, CategoryFolder); _context.Add(model); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ParentId"] = new SelectList(_context.Category, "CategoryId", "CategoryId", category.ParentId); return(View(category)); }
public async Task <IActionResult> UpdateCategory(CategoryVm categoryVm) { var id = categoryVm.Id; var category = await _context.Categories.FindAsync(id); if (category == null) { return(NotFound()); } category.Name = categoryVm.Name; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult <CategoryVm> > CreateCategory(CategoryVm categoryVm) { var category = new Category { Name = categoryVm.Name, }; _context.Categories.Add(category); await _context.SaveChangesAsync(); return(CreatedAtAction("Get", new { id = category.Id }, new CategoryVm { Id = category.Id, Name = category.Name })); }
public async Task PostCategory_Success() { var category = new CategoryVm { Name = "Watch" }; var controller = new CategoryController(_dbContext); var result = await controller.CreateCategory(category); var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(result.Result); var returnValue = Assert.IsType <CategoryVm>(createdAtActionResult.Value); Assert.Equal("Watch", returnValue.Name); }
public ActionResult Save(CategoryVm model) { if (model.Id == 0) { var newCountry = new AspCountry { Id = model.Id, Name = model.Name }; _context.Countries.Add(newCountry); _context.SaveChanges(); return(RedirectToAction("Index", "Country")); } return(RedirectToAction("Index", "Country")); }
public async Task <IActionResult> GetById(int id) { //// GET CATEGORY WITH ID (KEY) var category = await _context.Categories.FindAsync(id); //// IF KEY IS NOT EXIST (CATEGORY IS NULL), RETURN STATUS 404 if (category == null) { return(NotFound(new ApiNotFoundResponse($"Category with id: {id} is not found"))); } //// GIVE INFORMATIONS TO CategoryVm (JUST SHOW FIELD NEEDED CategoryVm categoryvm = CreateCategoryVm(category); return(Ok(categoryvm)); }
public async Task <ActionResult <CategoryVm> > GetCategory(int id) { var categories = await _dataContext.Categories.FindAsync(id); if (categories == null) { return(NotFound()); } var category = new CategoryVm { CategoryID = categories.CategoryID, Name = categories.CategoryName }; return(category); }
public async Task <CategoryVm> GetCategoryById(int CategoryId) { var category = await _context.Categories.FindAsync(CategoryId); if (category == null) { return(new CategoryVm()); } var categoryVm = new CategoryVm { CategoryId = category.CategoryId, Name = category.Name }; return(categoryVm); }