public async Task <IActionResult> Edit(PostEditBindingModel model)
        {
            bool canEditPost = await this.postsService.CheckIfIsAllowedToPerformAsync(this.User.Identity.Name, model.Id);

            if (!canEditPost)
            {
                var messageModel = new MessageModel()
                {
                    Type    = MessageType.Warning,
                    Message = Messages.NotAllowedMsg
                };
                TempDataExtensions.Put(this.TempData, Constants.TempDataKey, messageModel);
                return(this.RedirectToAction("Index"));
            }

            if (!this.ModelState.IsValid)
            {
                model.Categories = await this.postsService.GenerateCategoriesSelectListAsync();

                return(this.View(model));
            }

            model = TSelfExtensions.TrimStringProperties(model);
            int id = await this.postsService.EditPostAsync(model);

            var message = new MessageModel()
            {
                Type    = MessageType.Success,
                Message = string.Format(Messages.EntityEditSuccess, nameof(Post), id)
            };

            TempDataExtensions.Put(this.TempData, Constants.TempDataKey, message);

            return(this.RedirectToAction("Details", new { id = id }));
        }
        public async Task <int> EditPostAsync(PostEditBindingModel model)
        {
            Validator.ThrowIfNull(model);
            var postFromDb = await this.DbContext.Posts
                             .Include(p => p.Tags)
                             .ThenInclude(t => t.Tag)
                             .FirstOrDefaultAsync(p => p.Id == model.Id);

            postFromDb.Title          = model.Headline;
            postFromDb.Slug           = model.Slug;
            postFromDb.Content        = model.Content;
            postFromDb.LastUpdateDate = model.LastUpdateDate;
            postFromDb.CategoryId     = model.CategoryId;
            if (model.Tags != null)
            {
                var postTagsNames  = postFromDb.Tags.Select(t => t.Tag.Name.Replace(Constants.HashTag, string.Empty)).ToList();
                var modelTagsNames = model.Tags
                                     .Split(", ", StringSplitOptions.RemoveEmptyEntries)
                                     .ToList()
                                     .Select(t => t.ToLower())
                                     .Select(t => t.Trim())
                                     .ToList();
                var tagsToAdd    = modelTagsNames.Where(t => !postTagsNames.Contains(t)).ToList();
                var tagsToRemove = postFromDb.Tags
                                   .Where(t => !modelTagsNames.Contains(t.Tag.Name.Replace(Constants.HashTag, string.Empty))).ToList();
                foreach (var tag in tagsToRemove)
                {
                    postFromDb.Tags.Remove(tag);
                }

                await this.DbContext.SaveChangesAsync();

                if (tagsToAdd.Count != 0)
                {
                    var  tagsToAddAsString      = string.Join(", ", tagsToAdd);
                    bool addTagsToPostSucceeded = await this.AddTagsToPostAsync(tagsToAddAsString, postFromDb.Id);

                    if (!addTagsToPostSucceeded)
                    {
                        throw new InvalidOperationException(string.Format(Messages.UnexpectedError, nameof(model.Tags), nameof(Post), postFromDb.Id));
                    }
                }
            }

            if (model.Image != null)
            {
                bool uploadImageSucceeded = await this.UploadFileInFileSystemAsync(model.Image, postFromDb.Id);

                if (!uploadImageSucceeded)
                {
                    throw new InvalidOperationException(string.Format(Messages.UnexpectedError, nameof(model.Image), nameof(Post), postFromDb.Id));
                }
            }

            return(postFromDb.Id);
        }