public async Task EditAsync_ShouldSaveChangesToDb_WhenCategoryExists() { var categoryToEditId = Guid.NewGuid().ToString(); var category = new Category { Id = categoryToEditId, Name = "CurrentName", }; await this.dbContext.Categories.AddAsync(category); await this.dbContext.SaveChangesAsync(); var categoryWithEdits = new CategoryUpdateInputModel() { Id = categoryToEditId, Name = "NewName" }; var categoryService = new CategoryService(this.dbContext); await categoryService.EditAsync(categoryWithEdits); var actualInterview = this.dbContext.Categories.First(); Assert.Equal(categoryWithEdits.Id, actualInterview.Id); Assert.Equal(categoryWithEdits.Name, actualInterview.Name); }
public async Task <IActionResult> Update(CategoryUpdateInputModel category) { if (!ModelState.IsValid) { var error = new CategoryError(); error.ErrorMessage = GlobalConstants.CategotyinputModelUpdateErrorMessage; return(this.RedirectToAction("Error", "Model", error)); } if (this.User.IsInRole(GlobalConstants.AdministratorRoleName)) { var isSame = await this.categoryService .IsSameCategoryAsync(category.Name, category.ImageAddress); if (isSame) { var error = new CategoryError(); error.ErrorMessage = GlobalConstants.CategotyUpdateErrorMessage; return(this.RedirectToAction("Error", "Model", error)); } var categoryDto = CategoryUpdateInputMapper.Map(category); await this.categoryService.UpdateCategoryAsync(categoryDto); return(this.Redirect("/Category/Index")); } return(this.Redirect("/Identity/Account/AccessDenied")); }
public async Task <Category> EditAsync(CategoryUpdateInputModel categoryUpdateInputModel) { var category = await this.GetAsync(categoryUpdateInputModel.Id); category.Name = categoryUpdateInputModel.Name; await this.db.SaveChangesAsync(); return(category); }
public static CategoryUpdateInputDtoModel Map(CategoryUpdateInputModel category) { var categoryDto = new CategoryUpdateInputDtoModel { Id = category.Id, Name = category.Name, ImageAddress = category.ImageAddress, }; return(categoryDto); }
public ActionResult Update(CategoryUpdateInputModel model) { if (!this.ModelState.IsValid) { return this.View(model); } this.categories.Update(model.Id, model.Name); return this.RedirectToAction("ListCategories", "ListCategories"); }
public void EditAsync_ShouldThrowInvalidCategoryException_WhenCategoryNotExists() { var categoryWithEdits = new CategoryUpdateInputModel { Id = Guid.NewGuid().ToString() }; var categoryService = new CategoryService(this.dbContext); Assert.Throws <InvalidCategoryException>( () => categoryService.EditAsync(categoryWithEdits).GetAwaiter().GetResult()); }
public async Task <IActionResult> Recover(CategoryUpdateInputModel categoryUpdateInputModel) { var archivedCategory = await this.categoryService .RecoverAsync(categoryUpdateInputModel.Id); var message = $"Категорията \"{archivedCategory.Name}\" беше възстановена успешно."; this.logger.LogInformation((int)LoggingEvents.RecoverItem, message); this.SetAlertMessage(AlertMessageLevel.Success, message); return(RedirectToAction(nameof(Index))); }
public ActionResult Update(CategoryUpdateInputModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } this.categories.Update(model.Id, model.Name); this.TempData["SuccessNotification"] = $"Successfuly updated category {model.Name}"; return(this.RedirectToAction("ListCategories", "ListCategories")); }
public async Task EditAsync_ShouldThrowInvalidCategoryException_WhenIdExistsButCategoryIsSoftDeleted() { var categoryId = Guid.NewGuid().ToString(); await this.dbContext.Categories.AddAsync(new Category { Id = categoryId, IsDeleted = true }); await this.dbContext.SaveChangesAsync(); var categoryWithEdits = new CategoryUpdateInputModel { Id = categoryId }; var categoryService = new CategoryService(this.dbContext); Assert.Throws <InvalidCategoryException>( () => categoryService.EditAsync(categoryWithEdits).GetAwaiter().GetResult()); }
public async Task <IActionResult> Edit(CategoryUpdateInputModel categoryUpdateInputModel) { if (!this.ModelState.IsValid) { this.SetAlertMessage(AlertMessageLevel.Error, this.GetModelStateErrorMessages()); return(RedirectToAction(nameof(Index))); } var categoryToEdit = await this.categoryService.EditAsync(categoryUpdateInputModel); var message = $"Категорията \"{categoryToEdit.Name}\" беше променена успешно."; this.logger.LogInformation((int)LoggingEvents.UpdateItem, message); this.SetAlertMessage(AlertMessageLevel.Success, message); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> Update(CategoryUpdateInputModel input) { if (this.User.Identity.Name == GlobalConstants.AdminName) { try { await this.categoriesService.UpdateAsync(input.Id, input.Name, input.Picture); return(this.Ok(new { Message = Messages.SuccessfullyUpdated, })); } catch (Exception) { return(this.BadRequest(new BadRequestViewModel { Message = Messages.UnknownError, })); } } return(this.Unauthorized()); }