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 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 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)); }