public async Task <Paged <Post> > SearchAsync(PostDbQuery dbQuery) { ArgumentGuard.NotNull(dbQuery, nameof(dbQuery)); var query = _blogDbContext.Posts.Include(p => p.Author).Include(p => p.Tags).AsQueryable(); if (dbQuery.SearchText != null) { query = query.Where(p => p.Title.Contains(dbQuery.SearchText) || p.Content.Contains(dbQuery.SearchText) || p.Tags.Any(t => dbQuery.SearchText.Contains(t.Name))); } if (dbQuery.Status.HasValue) { query = query.Where(p => p.Status == dbQuery.Status.Value); } var totalCount = query?.Count() ?? default(long); var results = await query.Skip(dbQuery.PageSize *(dbQuery.PageNumber - 1)).Take(dbQuery.PageSize).ToListAsync(); var posts = results.Select(x => new Post(x)).ToList(); return(new Paged <Post> { PageSize = dbQuery.PageSize, PageNumber = dbQuery.PageNumber, TotalCount = totalCount, Items = posts }); }
public PostsController(AppDb db) { Db = db; userQuery = new UserDbQuery(Db); postQuery = new PostDbQuery(Db); authorizationQuery = new AuthorizationQuery(Db); threadQuery = new ThreadDbQuery(Db); locationQuery = new LocationQuery(Db); }