public PostCategoriesController(IPostCategoryServices postCategoryServices, IPostCategoryCommanderServices postCategoryCommanderServices, ILogger <PostCategoriesController> logger) { _postCategoryServices = postCategoryServices; _postCategoryCommanderServices = postCategoryCommanderServices; _logger = logger; }
public async Task <IActionResult> DeleteConfirmed(int id, [FromServices] IPostCategoryServices postCategoryServices) { var category = await categoryServices.GetCategory(id); await categoryServices.DeleteCategory(category); return(RedirectToAction("Index")); }
private async Task DeletePostCategories(int id, [FromServices] IPostCategoryServices postCategoryServices) { List <PostCategory> postCategories = await postCategoryServices.GetAllWithSameIdAsync(id); foreach (PostCategory postCategory in postCategories) { await postCategoryServices.DeletePostCategory(postCategory); } }
private async Task DeleteExistingPostCategoryEntries(int id, IPostCategoryServices postCategoryServices) { var itemsToDelete = await postCategoryServices.GetAllWithSameIdAsync(id); if (itemsToDelete != null && itemsToDelete.Count > 0) { foreach (var itemToDelete in itemsToDelete) { await postCategoryServices.DeletePostCategory(itemToDelete); } } }
public async Task <IActionResult> Edit([Bind("Post, Photo, SelectedCategories")] CreatePostViewModel createPostViewModel, [FromServices] IPostCategoryServices postCategoryServices, [FromServices] ICategoryServices categoryServices) { if (ModelState.IsValid) { await postServices.UpdatePost(createPostViewModel.Post, createPostViewModel.Photo); await UpdatePostCategory(createPostViewModel, postCategoryServices, categoryServices); return(RedirectToAction("Index")); } return(View(createPostViewModel)); }
public async Task <IActionResult> DeleteConfirmed(int id, [FromServices] IPostCategoryServices postcategoryServices, [FromServices] ICommentServices commentServices) { await postServices.DeletePost(id); //two steps underneath not necessary Entity frameworks allready solves it, but it was a good exercise ;-) await DeleteExistingPostCategoryEntries(id, postcategoryServices); await DeleteExistingPostCommentEntries(id, commentServices); return(RedirectToAction("Index")); }
private async Task SavePostCategory(CreatePostViewModel createPostViewModel, IPostCategoryServices postCategoryServices, ICategoryServices categoryServices) { if (createPostViewModel.SelectedCategories?.Count > 0) { foreach (string category in createPostViewModel.SelectedCategories) { Category cat = await categoryServices.GetCategory(new Category { Name = category }); PostCategory postCategory = new PostCategory { PostId = createPostViewModel.Post.Id, CategoryId = cat.Id }; await postCategoryServices.SavePostCategory(postCategory); } } }
private async Task UpdatePostCategory(CreatePostViewModel createPostViewModel, IPostCategoryServices postCategoryServices, ICategoryServices categoryServices) { //Delete existing PostCategory entries await DeleteExistingPostCategoryEntries(createPostViewModel.Post.Id, postCategoryServices); //Add new PostCategory entries if there are any entries if (createPostViewModel.SelectedCategories != null) { foreach (string category in createPostViewModel.SelectedCategories) { Category cat = await categoryServices.GetCategory(new Category { Name = category }); PostCategory postCategory = new PostCategory { PostId = createPostViewModel.Post.Id, CategoryId = cat.Id }; await postCategoryServices.SavePostCategory(postCategory); } } }