public async Task CreatePostAsync(PostAdminCreateViewModel inputModel) { if (!this.dbContext.Users.Any(u => u.Id == inputModel.AuthorId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidUserId, inputModel.AuthorId)); } if (!this.dbContext.BlogCategories.Any(u => u.Id == inputModel.CategoryId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidPostCategoryId, inputModel.CategoryId)); } var post = new Post { Title = inputModel.Title, Description = inputModel.Description, AuthorId = inputModel.AuthorId, CategoryId = inputModel.CategoryId, Rating = inputModel.Rating, Type = inputModel.Type, VideoUrl = inputModel.VideoUrl, }; await this.dbContext.Post.AddAsync(post); await this.dbContext.SaveChangesAsync(); }
public async Task <IActionResult> Create(PostAdminCreateViewModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } await this.postsService.CreatePostAsync(inputModel); return(this.RedirectToAction("All")); }
public async Task <IActionResult> Create() { var categories = this.categoriesService.GetAllPostCategories(); var adminId = await this.usersService.GetAdminIdAsync(); var postViewModel = new PostAdminCreateViewModel { AuthorId = adminId, Categories = categories, }; return(this.View(postViewModel)); }