public async Task <int> CreatePostAsync(PostCreateBindingModel model, string userName) { Validator.ThrowIfNull(model); Validator.ThrowIfNullEmptyOrWhiteSpace(userName, nameof(userName)); var post = this.Mapper.Map <Post>(model); post.AuthorId = this.DbContext.Users.FirstOrDefaultAsync(u => u.UserName == userName).Result.Id; await this.DbContext.Posts.AddAsync(post); await this.DbContext.SaveChangesAsync(); if (model.Image != null) { bool uploadImageSucceeded = await this.UploadFileInFileSystemAsync(model.Image, post.Id); if (!uploadImageSucceeded) { throw new InvalidOperationException(string.Format(Messages.UnexpectedError, nameof(model.Image), nameof(Post), post.Id)); } } if (model.Tags != null) { bool addTagsToPostSucceeded = await this.AddTagsToPostAsync(model.Tags, post.Id); if (!addTagsToPostSucceeded) { throw new InvalidOperationException(string.Format(Messages.UnexpectedError, nameof(model.Tags), nameof(Post), post.Id)); } } return(post.Id); }
public async Task <PostCreateBindingModel> PreparePostCreateFormAsync() { var categories = await this.GenerateCategoriesSelectListAsync(); var model = new PostCreateBindingModel() { Categories = categories.OrderBy(c => c.Value) }; return(model); }
public async Task <IActionResult> Create(PostCreateBindingModel model) { if (!this.ModelState.IsValid) { model.Categories = await this.postsService.GenerateCategoriesSelectListAsync(); return(this.View(model)); } model = TSelfExtensions.TrimStringProperties(model); int id = await this.postsService.CreatePostAsync(model, this.User.Identity.Name); var messageModel = new MessageModel() { Type = MessageType.Success, Message = string.Format(Messages.EntityCreateSuccess, nameof(Post), id) }; TempDataExtensions.Put(this.TempData, Constants.TempDataKey, messageModel); return(this.RedirectToAction("Details", new { id = id })); }