Exemple #1
0
        public async Task AddAsync(TEntity entity)
        {
            using var context = new UdemyBlogContext();
            await context.AddAsync(entity);

            await context.SaveChangesAsync();
        }
        public async Task <List <Category> > GetAllCategoryWithBlogsCount()
        {
            using var context = new UdemyBlogContext();
            var categories = await context.Categories.OrderByDescending(i => i.Id).Include(i => i.categoryBlogs).ToListAsync();

            return(categories);
        }
 public async Task <List <Category> > GetCategoriesAsync(int blogId)
 {
     using var context = new UdemyBlogContext();
     return(await context.Categories.Join(context.CategoryBlogs, c => c.Id, cb => cb.CategoryId, (category, categoryBlog) => new
     {
         category,
         categoryBlog
     }).Where(I => I.categoryBlog.BlogId == blogId).Select(I => new Category
     {
         Id = I.category.Id,
         Name = I.category.Name
     }).ToListAsync());
 }
 public async Task <List <Blog> > GetAllByCategoryIdAsync(int categoryId)
 {
     using var context = new UdemyBlogContext();
     return(await context.Blogs.Join(context.CategoryBlogs, b => b.Id, cb => cb.BlogId, (blog, categoryBlog) => new
     {
         blog,
         categoryBlog
     }).Where(I => I.categoryBlog.CategoryId == categoryId).Select(I => new Blog
     {
         AppUser = I.blog.AppUser,
         AppUserId = I.blog.AppUserId,
         CategoryBlogs = I.blog.CategoryBlogs,
         Comments = I.blog.Comments,
         Description = I.blog.Description,
         Id = I.blog.Id,
         ImagePath = I.blog.ImagePath,
         PostedTime = I.blog.PostedTime,
         ShortDescription = I.blog.ShortDescription,
         Title = I.blog.Title
     }).ToListAsync());
 }
Exemple #5
0
        private async Task GetComments(int blogid, int?parentId, List <Comment> result)
        {/*Burayı debuga alıp denenecek.*/
            using var context = new UdemyBlogContext();
            var comments = await context.Comments.Where(i => i.BlogId == blogid && i.ParentCommentId == parentId).OrderByDescending(i => i.PostedTime).ToListAsync();

            if (comments.Count > 0)
            {
                foreach (var comment in comments)
                {
                    if (comment.SubComment == null)
                    {
                        comment.SubComment = new List <Comment>();
                    }

                    await GetComments(comment.BlogId, comment.Id, comment.SubComment);

                    if (!result.Contains(comment))
                    {
                        result.Add(comment);
                    }
                }
            }
        }
Exemple #6
0
        private async Task GetComments(int blogId, int?parentId, List <Comment> result)
        {
            using var context = new UdemyBlogContext();
            var comments = await context.Comments.Where(I => I.BlogId == blogId && I.ParentCommentId == parentId).OrderByDescending(I => I.PostedTime).ToListAsync();

            if (comments.Count > 0)
            {
                foreach (var comment in comments)
                {
                    if (comment.SubComments == null)
                    {
                        comment.SubComments = new List <Comment>();
                    }

                    await GetComments(comment.BlogId, comment.Id, comment.SubComments);

                    if (!result.Contains(comment))
                    {
                        result.Add(comment);
                    }
                }
            }
        }
Exemple #7
0
 public async Task UpdateAsync(TEntity entity)
 {
     using var context = new UdemyBlogContext();
     context.Update(entity);
     await context.SaveChangesAsync();
 }
Exemple #8
0
 public async Task <TEntity> GetAsync(Expression <Func <TEntity, bool> > filter)
 {
     using var context = new UdemyBlogContext();
     return(await context.Set <TEntity>().FirstOrDefaultAsync(filter));
 }
Exemple #9
0
 public async Task <List <TEntity> > GetAllAsync <TKey>(Expression <Func <TEntity, TKey> > keySelector)
 {
     using var context = new UdemyBlogContext();
     return(await context.Set <TEntity>().OrderByDescending(keySelector).ToListAsync());
 }
Exemple #10
0
 public async Task <List <TEntity> > GetAllAsync(Expression <Func <TEntity, bool> > filter)
 {
     using var context = new UdemyBlogContext();
     return(await context.Set <TEntity>().Where(filter).ToListAsync());
 }
Exemple #11
0
        public async Task <List <TEntity> > GetAllAsync()
        {
            using var context = new UdemyBlogContext();

            return(await context.Set <TEntity>().ToListAsync());
        }
Exemple #12
0
 public async Task <TEntity> FindByIdAsync(int id)
 {
     using var context = new UdemyBlogContext();
     return(await context.FindAsync <TEntity>(id));
 }
 public async Task <List <Blog> > GetLastFiveAsync()
 {
     using var context = new UdemyBlogContext();
     return(await context.Blogs.OrderByDescending(I => I.PostedTime).Take(5).ToListAsync());
 }
Exemple #14
0
 public async Task RemoveAsync(TEntity entity)
 {
     using var context = new UdemyBlogContext();
     context.Set <TEntity>().Remove(entity);
     await context.SaveChangesAsync();
 }
 public async Task <List <Category> > GetAllWithCategoryBlogsAsync()
 {
     using var context = new UdemyBlogContext();
     return(await context.Categories.OrderByDescending(I => I.Id).Include(I => I.CategoryBlogs).ToListAsync());
 }