public async Task <IActionResult> Add(AddArticleInputModel input) { if (!this.ModelState.IsValid) { return(this.View()); } var user = await this.userManager.GetUserAsync(this.User); await this.articleService.CreateAsync(input, user.Id); this.TempData["Message"] = "Article added successfully."; return(this.Redirect("/Blog/Blog")); }
public async Task CreateMethodShouldAddCorrectNewArticleToDb() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new ApplicationDbContext(optionsBuilder.Options); var articleService = new ArticleService(dbContext, null); var articleToAdd = new AddArticleInputModel { Content = "testContent", Title = "testTitle", }; await articleService.CreateAsync(articleToAdd, "Icaka99"); Assert.NotNull(dbContext.Articles.FirstOrDefaultAsync()); Assert.Equal("testContent", dbContext.Articles.FirstAsync().Result.Content); Assert.Equal("testTitle", dbContext.Articles.FirstAsync().Result.Title); Assert.Equal("Icaka99", dbContext.Articles.FirstAsync().Result.UserId); }
public async Task CreateAsync(AddArticleInputModel input, string id) { string imageUrl = this.defaultArticlePicture; if (input.PictureFile != null) { imageUrl = await this.cloudinaryService.UploudAsync(input.PictureFile); } var dbArticle = new Article { Title = input.Title, Content = input.Content, UserId = id, ImageUrl = imageUrl, }; await this.db.Articles.AddAsync(dbArticle); await this.db.SaveChangesAsync(); }