public BlogDtoDll GetBlog(int BlogId) { // We can write one line of code if you like =>_unitOfWork.GetRepository<Blog>().Add(blog); IRepository <Blog> repBlog = this._unitOfWork.GetRepository <Blog>(); BlogDtoDll BlogDtoDll = null; //var blog = repBlog.Single(o=>o.BlogId == BlogId); //Blog blog = repBlog.Single(m => m.BlogId == BlogId, include: source => source.Include(m => m.Post).ThenInclude(post => post.Comment)); Blog blog = repBlog.Query().Include(blogt => blogt.Post).ThenInclude(post => post.Comment).Single(m => m.BlogId == BlogId); if (blog != null) { BlogDtoDll = new BlogDtoDll(); BlogDtoDll.BlogId = blog.BlogId; BlogDtoDll.Url = blog.Url; foreach (var item in blog.Post) { PostDtoDll post = new PostDtoDll(); post.Content = item.Content; post.Title = item.Title; BlogDtoDll.PostDtoDll.Add(post); } } return(BlogDtoDll); }
public IActionResult GetBlog([FromRoute] int id) { if (ModelState.IsValid) { BlogManager blogManager = new BlogManager(this._unitOfWork); BlogDtoDll blogDtoDll = blogManager.GetBlog(id); BlogDto blogDto = new BlogDto { BlogId = blogDtoDll.BlogId, Url = blogDtoDll.Url }; List <PostDto> postDtolist = new List <PostDto>(); foreach (var post in blogDtoDll.PostDtoDll) { PostDto postDto = new PostDto { BlogId = post.BlogId, Content = post.Content, Title = post.Title, PostId = post.PostId }; postDtolist.Add(postDto); } blogDto.PostDto = postDtolist; if (blogDto != null) { return(Ok(blogDto)); } return(StatusCode(404)); } else { return(StatusCode(400)); } //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} //var blog = await this._unitOfWork.GetRepository<Blog>().Single(m => m.BlogId == id); ////var blog = await _context.Blog.SingleOrDefaultAsync(m => m.BlogId == id); //if (blog == null) //{ // return NotFound(); //} }
public async Task Add_Blog() { try { string connection = @"Server=(localdb)\mssqllocaldb;Database=BloggingGeneric;Trusted_Connection=True;ConnectRetryCount=0"; var options = new DbContextOptionsBuilder <DataContext>() .UseSqlServer(connection) .Options; //// Create the schema in the database //using (var context = new BloggingContext(options)) //{ // context.Database.EnsureCreated(); //} // Run the test against one instance of the context using (var context = new DataContext(options)) { //IBlogRepositoryGeneric blogsRepository = new BlogRepositoryGenerics(context); var uow = new UnitOfWork <DataContext>(context); //var repo = uow.GetRepository<Blog>(); BlogManager blogManager = new BlogManager(uow); BlogDtoDll blogDtoDll = new BlogDtoDll(); blogDtoDll.Url = "test from VS"; blogManager.AddBlog(blogDtoDll); } //Use a separate instance of the context to verify correct data was saved to database //using (var context = new BloggingContext(options)) //{ // Assert.AreEqual(1, context.Blogs.Count()); // Assert.AreEqual("http://sample.com", context.Blogs.Single().Url); //} } catch (Exception e) { } finally { } }
public IEnumerable <BlogDtoDll> GetBlogAll() { // We can write one line of code if you like =>_unitOfWork.GetRepository<Blog>().Add(blog); IRepository <Blog> repBlog = this._unitOfWork.GetRepository <Blog>(); List <BlogDtoDll> blogDtolsit = new List <BlogDtoDll>(); //var blog = rerepBlogpBlog.Single(o=>o.BlogId == BlogId); //IEnumerable<Blog> bloglist = repBlog.GetAll(include: source => source.Include(blog => blog.Post).ThenInclude(post => post.Comment)); //IEnumerable<Blog> bloglist = repBlog.Query(); IEnumerable <Blog> bloglist = repBlog.Query().Include(blog => blog.Post).ThenInclude(post => post.Comment); foreach (Blog blog in bloglist) { BlogDtoDll blogDto = new BlogDtoDll { BlogId = blog.BlogId, Url = blog.Url }; List <PostDtoDll> postDtolist = new List <PostDtoDll>(); foreach (var post in blog.Post) { PostDtoDll postDto = new PostDtoDll { BlogId = post.BlogId, Content = post.Content, Title = post.Title, PostId = post.PostId }; postDtolist.Add(postDto); } blogDto.PostDtoDll = postDtolist; blogDtolsit.Add(blogDto); } return(blogDtolsit.ToList()); }
public int AddBlog(BlogDtoDll BlogDtoDll) { Blog blog = new Blog(); blog.Url = BlogDtoDll.Url; foreach (var item in BlogDtoDll.PostDtoDll) { Post post = new Post(); post.Content = item.Content; post.Title = item.Title; blog.Post.Add(post); } // We can write one line of code if you like =>_unitOfWork.GetRepository<Blog>().Add(blog); IRepository <Blog> repBlog = this._unitOfWork.GetRepository <Blog>(); blog = repBlog.Add(blog); this._unitOfWork.SaveChanges(); return(blog.BlogId); }
public IActionResult PostBlog([FromBody] BlogDto BlogDto) { int blog_id = 0; if (ModelState.IsValid) { BlogDtoDll BlogDtoDll = new BlogDtoDll(); BlogDtoDll.Url = BlogDto.Url; foreach (var item in BlogDto.PostDto) { PostDtoDll post = new PostDtoDll(); post.Content = item.Content; post.Title = item.Title; BlogDtoDll.PostDtoDll.Add(post); } BlogManager blogManager = new BlogManager(this._unitOfWork); blog_id = blogManager.AddBlog(BlogDtoDll); } return(CreatedAtAction(actionName: "GetBlog", routeValues: new { id = blog_id }, value: null)); }