Example #1
0
 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"));
        }
Example #3
0
        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);
            }
        }
Example #4
0
        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);
                }
            }
        }
Example #5
0
        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));
        }
Example #6
0
        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"));
        }
Example #7
0
        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);
                }
            }
        }
Example #8
0
        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);
                }
            }
        }