public async Task <IActionResult> Delete(int id, ResourceFormModel model) { var courseId = model.CourseId; var userId = this.userManager.GetUserId(this.User); if (userId == null) { this.TempData.AddErrorMessage(WebConstants.InvalidUserMsg); return(this.RedirectToTrainersIndex()); } var isTrainer = await this.trainerService.IsTrainerForCourseAsync(userId, courseId); if (!isTrainer) { this.TempData.AddErrorMessage(WebConstants.NotTrainerForCourseMsg); return(this.RedirectToTrainersIndex()); } var resourceExists = this.resourceService.Exists(id); if (!resourceExists || id != model.ResourceId) { this.TempData.AddErrorMessage(WebConstants.ResourceNotFoundMsg); return(this.RedirectToTrainersResources(courseId)); } var success = await this.resourceService.RemoveAsync(id); if (!success) { this.TempData.AddErrorMessage(WebConstants.ResourceNotDeletedMsg); } this.TempData.AddSuccessMessage(WebConstants.ResourceDeletedMsg); return(this.RedirectToTrainersResources(courseId)); }
public async Task <IActionResult> Edit(int id, [FromQuery] int articleId, ResourceFormModel model) { if (!ModelState.IsValid) { var returnModel = new ResourceFormContainerModel { Form = model, ArticleId = articleId }; return(View(returnModel)); } var result = await this.resources.EditAsync(id, model.Title, model.Url, model.ResourceType); if (!result) { return(NotFound()); } TempData[TempDataSuccessMessageKey] = $"{model.Title} was edited successfully."; return(RedirectToAction(nameof(Details), new { Id = id })); }
public async Task <IActionResult> Create(int id, ResourceFormModel model) { if (!ModelState.IsValid) { var returnModel = new ResourceFormContainerModel { ArticleId = id, Form = model }; return(View(returnModel)); } if (id <= 0) { return(NotFound()); } var articleExists = this.articles.Exists(id); if (!articleExists) { return(NotFound()); } var newId = await this.resources.CreateAsync(model.Title, model.Url, model.ResourceType); if (newId <= 0) { throw new InvalidOperationException("The resource was not saved in the database."); } await this.resources.LinkToArticleAsync(newId, id); TempData[TempDataSuccessMessageKey] = $"{model.Title} was added to the resources."; return(RedirectToAction("Edit", "Articles", new { Area = WebConstants.AdminArea, Id = id })); }