private void Seed() { using (var context = new BlogPostsContext(_contextOptions)) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); int count = 3; for (int i = 0; i < count; i++) { int id = i + 1; var blogPost = new BlogPost { PostId = id, Title = $"Title {id}", Body = $"Body {id}", Creator = $"Creator {id}", Dt = DateTime.Now }; context.Set <BlogPost>().Add(blogPost); } context.SaveChanges(); } }
public void BlogPostsController_Get_All_BlogPosts_Result_Is_3_BlogPosts() { var context = new BlogPostsContext(_contextOptions); var repo = new DataRepository <BlogPost>(context); BlogPostsController controller = new BlogPostsController(context, repo); var result = controller.GetBlogPost(); Assert.NotNull(result); Assert.NotNull(result.Result); Assert.IsAssignableFrom <IEnumerable <BlogPost> >(result.Result.Value); Assert.Equal(3, result.Result.Value.Count()); }
public int GetTotalPages() { using var context = new BlogPostsContext(_connectionString); var blogs = GetBlogPosts(1); if (blogs.Count % 3 == 0) { return(blogs.Count); } else { return(blogs.Count + 1); } }
public void BlogPostsController_Adds_BlogPosts_Result_Is_Not_Null_Repo_Add_and_Save_Once() { var blogPost = new Mock <BlogPost>(); var context = new BlogPostsContext(_contextOptions); var mockRepository = new Mock <IDataRepository <BlogPost> >(); mockRepository.Setup(m => m.Add(It.IsAny <BlogPost>())); mockRepository.Setup(m => m.SaveAsync(It.IsAny <BlogPost>())); BlogPostsController controller = new BlogPostsController(context, mockRepository.Object); var result = controller.PostBlogPost(blogPost.Object); mockRepository.Verify(m => m.Add(It.IsAny <BlogPost>()), Times.Once()); mockRepository.Verify(m => m.SaveAsync(It.IsAny <BlogPost>()), Times.Once()); Assert.NotNull(result); Assert.NotNull(result.Result); Assert.IsType <ActionResult <BlogPost> >(result.Result); }
public TagRepo(BlogPostsContext context) { _context = context; }
public BlogsController(BlogPostsContext db) => _db = db;
public UnitOfWork(BlogPostsContext context) { _context = context; }
public List <BlogPost> GetBlogPosts(int skip) { using var context = new BlogPostsContext(_connectionString); return(context.BlogPosts.OrderByDescending(bp => bp.Id).Include(c => c.Comments) .Skip((skip - 1) * 3).Take(3).ToList()); }
public BlogPostsController(BlogPostsContext context, IDataRepository <BlogPost> repo, ILogger <BlogPostsController> logger) { _context = context; _repo = repo; _logger = logger; }
public void AddComment(Comment comment) { using var context = new BlogPostsContext(_connectionString); context.Comments.Add(comment); context.SaveChanges(); }
public List <Comment> GetCommentsForBlogPost(int blogPostId) { using var context = new BlogPostsContext(_connectionString); return(context.Comments.Where(c => c.BlogPostId == blogPostId).ToList()); }
public BlogPost GetById(int id) { using var context = new BlogPostsContext(_connectionString); return(context.BlogPosts.Where(bp => bp.Id == id).Include(c => c.Comments).FirstOrDefault()); }
public BlogPost GetMostRecent() { using var context = new BlogPostsContext(_connectionString); return(context.BlogPosts.OrderByDescending(bp => bp.Id).Include(c => c.Comments).FirstOrDefault()); }
public DataRepository(BlogPostsContext context) { _context = context; }
public BlogPostController(BlogPostsContext blogPostsContext, IRepository <BlogPost> repository) { _blogPostsContext = blogPostsContext; _dataRepository = repository; }
public BlogPostsController(BlogPostsContext context, IDataRepository <BlogPost> repo) { _context = context; _repo = repo; }
public void AddBlogPost(BlogPost bp) { using var context = new BlogPostsContext(_connectionString); context.BlogPosts.Add(bp); context.SaveChanges(); }