public async Task <IActionResult> AddArticle(AdminArticleBindingModel model) { if (!ModelState.IsValid) { return(View()); } await _articlesService.AddArticleAsync(model); return(RedirectToAction("ArticlesAdmin")); }
public async Task AddArticleAsync(AdminArticleBindingModel article) { if (article.Category == null) { var newCategory = new Category { CategoryName = article.NewCategory }; await DbContext.Categories.AddAsync(newCategory); await DbContext.SaveChangesAsync(); article.Category = article.NewCategory; } var model = Mapper.Map <Article>(article); model.Date = DateTime.UtcNow; model.CategoryId = DbContext.Categories.FirstOrDefault(ca => string.Equals(ca.CategoryName, article.Category, StringComparison.Ordinal)).Id; await DbContext.Articles.AddAsync(model); await DbContext.SaveChangesAsync(); }
public void AddArticleAsyncShouldReturnTeamMemberCorrectly() { var options = new DbContextOptionsBuilder <IntillegioContext>() .UseInMemoryDatabase(databaseName: "Add_Article_Db") .Options; var dbContext = new IntillegioContext(options); var articleBindingModel = new AdminArticleBindingModel { Image350X220 = "http://specthemes.com/redbiz/redbiz-5/img/thumbs/t-1.jpg", Name = "Advices for young designers", Image825X530 = "http://specthemes.com/redbiz/redbiz-5/img/blog/blog-post.jpg", Image390X245 = "http://specthemes.com/redbiz/redbiz-5/img/blog/blog-4.jpg", Image65X65 = "http://specthemes.com/redbiz/redbiz-5/img/thumbs/t-1.jpg", Content = "There’s a lot that goes into becoming a truly great designer. It can’t be done by simply reading a book or watching a YouTube video." }; var mapper = new Mock <IMapper>(); mapper.Setup(m => m.Map <Article>(articleBindingModel)) .Returns(new Article { Image350X220 = "http://specthemes.com/redbiz/redbiz-5/img/thumbs/t-1.jpg", Name = "Advices for young designers", Image825X530 = "http://specthemes.com/redbiz/redbiz-5/img/blog/blog-post.jpg", Image390X245 = "http://specthemes.com/redbiz/redbiz-5/img/blog/blog-4.jpg", Image65X65 = "http://specthemes.com/redbiz/redbiz-5/img/thumbs/t-1.jpg", Content = "There’s a lot that goes into becoming a truly great designer. It can’t be done by simply reading a book or watching a YouTube video." }); var service = new BlogService(dbContext, mapper.Object); service.AddArticleAsync(articleBindingModel); Assert.True(dbContext.Articles.Any(n => n.Name == articleBindingModel.Name)); Assert.True(dbContext.Articles.Any(a => a.Image350X220 == articleBindingModel.Image350X220)); }