public ActionResult AddChild(Guid parentId, Guid classId) { var vm = new CategoryBindingModel() { ParentId = parentId, ClassificationId = classId }; var categories = _uow.Categories.AllIncluding(c => c.Parent).Where(c => c.Parent == null) .Project().To<CategoryViewModel>().ToList(); categories.ForEach(n => BuildChildNode(n)); //foreach (var item in categories) // BuildChildNode(item); var list = new List<SelectListItem>(); foreach (var item in categories) { list.Add(new SelectListItem { Value = item.Id.ToString(), Text = item.Name, Selected = item.Id == parentId }); ToSelectList(item, list); } ViewData["Categories"] = list; return View("Create", vm); }
public IHttpActionResult EditCategory(int id, CategoryBindingModel model) { var userId = User.Identity.GetUserId(); var category = DbContext.Categories.FirstOrDefault(p => p.Id == id); if (category == null) { return(NotFound()); } if (userId != category.Household.OwnerOfHouseId) { return(BadRequest("Sorry, You are not allowed to edit categories of this household.")); } category.Name = model.Name; category.Description = model.Description; category.DateUpdated = DateTime.Now; DbContext.SaveChanges(); var viewModel = new CategoryViewModel(category) { IsOwner = category.Household.OwnerOfHouseId == userId }; return(Ok(viewModel)); }
public IHttpActionResult PutCategory(int id, CategoryBindingModel chandegCategory) { var dbCategory = db.Categories.FirstOrDefault(c => c.Id == id); if (dbCategory == null) { return(this.NotFound()); } if (chandegCategory.Name == String.Empty) { return(this.BadRequest("Category name can not be empty")); } var checkDuplicateCategory = db.Categories.FirstOrDefault(c => c.Name == chandegCategory.Name); if (checkDuplicateCategory != null) { return(this.BadRequest("Category exists")); } dbCategory.Name = chandegCategory.Name; db.SaveChanges(); return(this.Ok("Category changed succesfully")); }
public IHttpActionResult Create(CategoryBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var householdOwnerId = HouseholdHelper.GetHhOwnerIdByHhId(model.HouseholdId); if (householdOwnerId == null) { return(NotFound()); } var currentUserId = User.Identity.GetUserId(); var IsOwner = householdOwnerId == currentUserId; if (!IsOwner) { return(Unauthorized()); } var category = Mapper.Map <Category>(model); category.DateCreated = DateTime.Now; DbContext.Categories.Add(category); DbContext.SaveChanges(); var viewModel = Mapper.Map <CategoryViewModel>(category); var url = Url.Link("DefaultApi", new { Action = "GetAllByHhId", model.HouseholdId }); return(Created(url, viewModel)); }
public IHttpActionResult EditCategory([FromUri] int id, CategoryBindingModel categoryBindingModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var category = new Category() { Name = categoryBindingModel.Name }; this.context.Categories.Add(category); try { this.context.SaveChanges(); return(this.Ok("Category added successfully")); } catch (Exception) { return(this.BadRequest("Category already exists")); } }
public void EditNullModel_ShouldRenderDefaultView() { CategoryBindingModel model = null; _controller.WithCallTo(c => c.Edit(model)) .ShouldRenderDefaultView(); }
public IHttpActionResult Create(CategoryBindingModel categoryBindingModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } bool duplicatedCategory = this.data .Categories .All() .FirstOrDefault(c => c.Name == categoryBindingModel.Name) != null; if (duplicatedCategory) { return this.BadRequest("A category with the same name already exists"); } Category category = new Category { Name = categoryBindingModel.Name }; this.data.Categories.Add(category); this.data.Save(); CategoryViewModel categoryViewModel = CategoryViewModel.ConvertToCategoryViewModel(category); return this.Ok(categoryViewModel); }
public IHttpActionResult Add(CategoryBindingModel categoryModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (categoryModel == null) { return(this.BadRequest("Invalid input parameters.")); } var category = new Category(); category.Name = categoryModel.Name; category.Position = categoryModel.Position; this.Data.Categories.Add(category); try { this.Data.SaveChanges(); } catch (Exception ex) { return(this.GetExceptionMessage(ex)); } return(this.Ok(string.Format("Category name: {0} and id: {1} is created", category.Name, category.Id))); }
public IHttpActionResult EditCategory([FromUri]int id, CategoryBindingModel categoryBindingModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var category = new Category() { Name = categoryBindingModel.Name }; this.context.Categories.Add(category); try { this.context.SaveChanges(); return this.Ok("Category added successfully"); } catch (Exception) { return this.BadRequest("Category already exists"); } }
public IHttpActionResult Edit(int id, CategoryBindingModel categoryModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(ModelState)); } var category = this.Data.Categories.All() .Where(x => x.Id == id) .FirstOrDefault(); this.CheckObjectForNull(category, "category", id); category.Name = categoryModel.Name; category.Position = categoryModel.Position; this.Data.Categories.Update(category); try { this.Data.SaveChanges(); } catch (Exception ex) { return(this.GetExceptionMessage(ex)); } return(Ok(string.Format("Category with id {0} is changed successfully", id))); }
public IHttpActionResult UpdateCategoryById(int id, CategoryBindingModel categoryModel) { var existingCategoryById = this.BookShopData.Categories.Find(id); var existingCategoryByName = this.BookShopData.Categories.All() .FirstOrDefault(a => a.Name.Equals(categoryModel.Name)); if (existingCategoryById == null) { return this.BadRequest("No author with such Id."); } if (existingCategoryByName != null) { return this.BadRequest("Category with the specified name already exists."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } existingCategoryById.Name = categoryModel.Name; this.BookShopData.Categories.Update(existingCategoryById); this.BookShopData.SaveChanges(); var categoryViewModel = CategoryViewModel.CreateViewModel(existingCategoryById); return this.Ok(categoryViewModel); }
public async Task Create_WithValid_ShouldRedirectToIndex() { var model = new CategoryBindingModel { Name = name }; this.mockRepository .Setup(repo => repo.CreateAsync(model.Name)) .Returns(Task.FromResult(true)); var tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[WebConstants.TempDataSuccessMessageKey] = It.IsAny <string>()); var controller = new CategoriesController(this.mockRepository.Object); controller.TempData = tempData.Object; var result = await controller.Create(model); var actionName = (result as RedirectToActionResult).ActionName; Assert.AreEqual("Index", actionName); }
public IHttpActionResult PutCategory([FromUri] int id, [FromBody] CategoryBindingModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var category = this.Data.Categories.GetById(id); if (category == null) { return(this.NotFound()); } var duplicate = this.Data.Categories.Find(c => c.Name == model.Name); if (duplicate != null) { var message = string.Format("Category with {0} name already exist!", model.Name); return(this.BadRequest(message)); } category.Name = model.Name; this.Data.SaveChanges(); var viewModel = new CategoryViewModel() { Name = category.Name }; return(this.Ok(viewModel)); }
// POST: api/Categories public IHttpActionResult PostCategory(CategoryBindingModel categoryModel) { if (categoryModel == null) { this.ModelState.AddModelError("categoryModel", "Category model can not be empty!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var checkExistCategory = db.Categories.FirstOrDefault(c => c.Name == categoryModel.Name); if (checkExistCategory != null) { return(this.BadRequest("Category exists")); } var category = new Category() { Name = categoryModel.Name }; db.Categories.Add(category); db.SaveChanges(); return(this.Ok("Category successfully added")); }
public void EditCategory(CategoryBindingModel model) { Category category = this.Data.Categories.Find(model.Id); category.Name = model.Name; this.Data.SaveChanges(); }
public void Insert(CategoryBindingModel model) { using (var context = new NewsBlogDatabase()) { context.Categories.Add(CreateModel(model, new Categories())); context.SaveChanges(); } }
private Category CreateModel(CategoryBindingModel model, Category accounting, HotelContext database) { accounting.Type = model.Type; accounting.Price = model.Price; accounting.Roomnumber = model.Roomnumber; accounting.Sleepingberths = model.Sleepingberths; return(accounting); }
public void Insert(CategoryBindingModel model) { using (var context = new HotelContext()) { context.Category.Add(CreateModel(model, new Category(), context)); context.SaveChanges(); } }
public async Task <IActionResult> Delete(int id, CategoryBindingModel model) { await this.categories.DeleteAsync(id); this.TempData.AddSuccessMessage(string.Format(WebConstants.DeletedMessage, "Category")); return(RedirectToAction("Index")); }
public void PutValidCategory_ShouldModifyExistingCategory() { CategoryBindingModel testCategory = new CategoryBindingModel() { Id = 1, Name = "Fish" }; this._service.EditCategory(testCategory); Assert.AreEqual("Fish", this.dbMock.Object.Categories.Find(1).Name); }
public void PostValidCategory_ShouldAddToRepo() { CategoryBindingModel testCategory = new CategoryBindingModel() { Name = "Meats" }; this._service.CreateNewCategory(testCategory); Assert.AreEqual(4, this.dbMock.Object.Categories.All().Count()); }
public ActionResult Create(CategoryBindingModel model) { if (model != null && ModelState.IsValid) { this.service.CreateNewCategory(model); return(RedirectToAction("Index")); } return(View()); }
public ActionResult Update(int id, CategoryBindingModel model) { var category = this.Data.Categories.All() .FirstOrDefault(c => c.Id == id); category.Name = model.Name; this.Data.SaveChanges(); return this.RedirectToAction("Categories"); }
public void CreateValidModel_ShouldRedirectToIndex() { CategoryBindingModel model = new CategoryBindingModel() { Id = 4, Name = "Chushki" }; _controller.WithCallTo(c => c.Create(model)) .ShouldRedirectTo(c => c.Index); }
public async Task CreateCategoryAsync(CategoryBindingModel model) { var dbModel = new Category() { Name = model.Name }; await this.DbContext.Categories.AddAsync(dbModel); await this.DbContext.SaveChangesAsync(); }
public ActionResult Edit(CategoryBindingModel model) { if (model != null && ModelState.IsValid) { service.EditCategory(model); return(RedirectToAction("Index")); } return(this.View()); }
public void CreateOrUpdate(CategoryBindingModel model) { if (model.Id.HasValue) { _clientStorage.Update(model); } else { _clientStorage.Insert(model); } }
public ActionResult Create(CategoryBindingModel model) { if (model != null && this.ModelState.IsValid) { var category = new Category { Title = model.Title }; this.Data.Categories.Add(category); this.Data.SaveChanges(); return this.RedirectToAction("Index", "Home"); } return this.View(model); }
public async Task Create_WithInvalidModelState_ShouldReturnView() { var controller = new CategoriesController(this.mockRepository.Object); var model = new CategoryBindingModel(); controller.ModelState.AddModelError(string.Empty, "Error"); var result = await controller.Create(model); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public async Task <IActionResult> Edit(int id, CategoryBindingModel model) { if (!ModelState.IsValid) { return(BadRequest()); } await this.categories.EditAsync(id, model); this.TempData.AddSuccessMessage(string.Format(WebConstants.EdittedMessage, "Category")); return(RedirectToAction("Index")); }
public void Update(CategoryBindingModel model) { using (var context = new HotelContext()) { var element = context.Category.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Клиент не найден"); } CreateModel(model, element, context); context.SaveChanges(); } }
public void Delete(CategoryBindingModel model) { var element = _clientStorage.GetElement(new CategoryBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Элемент не найден"); } _clientStorage.Delete(model); }
public void Update(CategoryBindingModel model) { using (var context = new NewsBlogDatabase()) { var category = context.Categories.FirstOrDefault(rec => rec.Idtheme == model.Id); if (category == null) { throw new Exception("Категория не найдена"); } CreateModel(model, category); context.SaveChanges(); } }
public void Delete(CategoryBindingModel model) { var category = _categoryStorage.GetElement(new CategoryBindingModel { Id = model.Id }); if (category == null) { throw new Exception("Категория не найдена"); } _categoryStorage.Delete(model); }
public List <CategoryViewModel> GetFilteredList(CategoryBindingModel model) { if (model == null) { return(null); } using (var context = new HotelContext()) { return(context.Category.Include(x => x.Room) .Where(rec => rec.Type == model.Type) .Select(CreateModel) .ToList()); } }
public CategoryViewModel GetElement(CategoryBindingModel model) { if (model == null) { return(null); } using (var context = new HotelContext()) { var accounting = context.Category.Include(x => x.Room) .FirstOrDefault(rec => rec.Id == model.Id); return(accounting != null?CreateModel(accounting) : null); } }
public IHttpActionResult AddCategory(CategoryBindingModel categoryBindingModel) { var category = new Category() { Name = categoryBindingModel.Name }; this.context.Categories.Add(category); try { this.context.SaveChanges(); return this.Ok("Category added successfully"); } catch (Exception) { return this.BadRequest(); } }
public ActionResult Activate(int id, CategoryBindingModel model) { var category = this.ContestsData.Categories.All() .FirstOrDefault(c => c.Id == id); if (category == null) { this.AddToastMessage("Error", "Non existing category!", ToastType.Error); return this.RedirectToAction("Index"); } category.IsActive = true; this.ContestsData.SaveChanges(); this.AddToastMessage("Success", "Category activated.", ToastType.Success); return this.RedirectToAction("Index"); }
// // GET: /Manage/Category/Create public ActionResult Create() { var repo = _uow.ClassificationRepo.All.ToList(); ViewData["Classifications"] = repo .ToSelectList(c => c.Id.ToString(), c => c.Name.ToString(), string.Empty); var categories = _uow.Categories.AllIncluding(c => c.Parent).Where(c => c.Parent == null) .Project().To<CategoryViewModel>().ToList(); var list = new List<CategoryViewModel>(); foreach (var item in categories) { BuildChildNode(item); list.Add(AutoMapper.Mapper.Map<CategoryViewModel>(item)); } var list2 = new List<SelectListItem>(); foreach (var item in categories) { list2.Add(new SelectListItem { Text = item.Name }); ToSelectList(item, list2); } ViewData["Categories"] = list2; //list.ToSelectList(c => c.Id.ToString(), c => c.Name.ToString(), string.Empty); var vm = new CategoryBindingModel(); return View(vm); }
public async Task<ActionResult> Create(CategoryBindingModel form) { try { if (ModelState.IsValid) { var classifcation = await _uow.ClassificationRepo.FindAsyncById(form.ClassificationId); var parent = _uow.Categories.FindById(form.ParentId); classifcation.Categories.Add(Category.Create(form.Name, form.Description, 1, parent != null ? parent.Depth + 1 : 0, classifcation, parent)); await _uow.SaveAsync(); } return RedirectToAction<ClassificationController>(c => c.Details(form.ClassificationId)).WithSuccess("Created"); } catch { return View(); } }
public IHttpActionResult UpdateCategory(int id, CategoryBindingModel categoryBindingModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingCategory = this.data.Categories.Find(id); if (existingCategory == null) { return this.BadRequest("Category does not exist - invalid id"); } bool duplicatedCategory = this.data .Categories .All() .FirstOrDefault(c => c.Name == categoryBindingModel.Name) != null; if (duplicatedCategory) { return this.BadRequest("A category with the same name already exists"); } existingCategory.Name = categoryBindingModel.Name; this.data.Categories.Update(existingCategory); this.data.Save(); CategoryViewModel categoryViewModel = CategoryViewModel.ConvertToCategoryViewModel(existingCategory); return this.Ok(categoryViewModel); }
public ActionResult AddRoot(Guid classId) { var vm = new CategoryBindingModel() { ClassificationId = classId }; return View("Create", vm); }
public ActionResult Edit(int id, CategoryBindingModel model) { if (model != null || this.ModelState.IsValid) { var category = this.ContestsData.Categories.All() .FirstOrDefault(c => c.Id == id); if (category == null) { this.AddToastMessage("Error", "Non existing category!", ToastType.Error); return this.RedirectToAction("Index"); } if (!this.ContestsData.Categories.All().Any(c => c.Name == model.Name && c.Id != id)) { category.Name = model.Name; this.ContestsData.SaveChanges(); this.AddToastMessage("Success", "Category edited.", ToastType.Success); return this.RedirectToAction("Index"); } this.ModelState.AddModelError("", @"Category already exists!"); } return View(model); }
public async Task<ActionResult> Edit(Guid id, CategoryBindingModel form) { try { if (ModelState.IsValid) { var item = await _uow.Categories.FindAsyncById(id); item.Name = form.Name; item.Description = form.Description; await _uow.SaveAsync(); } return RedirectToAction<ClassificationController>(c => c.Details(form.ClassificationId)) .WithSuccess("Completed"); } catch { return View(); } }
public IHttpActionResult AddCategory(CategoryBindingModel categoryModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingCategory = this.BookShopData.Categories.All() .FirstOrDefault(c => c.Name.Equals(categoryModel.Name)); if (existingCategory != null) { return this.BadRequest("Category with the specified name already exists."); } var newCategory = new Category { Name = categoryModel.Name }; this.BookShopData.Categories.Add(newCategory); this.BookShopData.SaveChanges(); var categoryViewModel = CategoryViewModel.CreateViewModel(newCategory); return this.Ok(categoryViewModel); }
public async Task<ActionResult> AddChild(CategoryBindingModel form) { return await Create(form); }
public ActionResult Create(CategoryBindingModel model) { if (model != null || this.ModelState.IsValid) { if (this.ContestsData.Categories.All().All(c => c.Name != model.Name)) { var category = new Category { Name = model.Name, IsActive = true }; this.ContestsData.Categories.Add(category); this.ContestsData.SaveChanges(); this.AddToastMessage("Success", "Category created.", ToastType.Success); return this.RedirectToAction("Index"); } this.ModelState.AddModelError("", @"Category already exists!"); } return View(model); }