public IEnumerable <OSSong> _FilterSongs(IEnumerable <OSSong> songs, SongFilterParameter queryParameter) { if (queryParameter != null) { if (queryParameter.SearchCriteriaBuildType == SearchCriteriaBuildType.Any) { return(songs.Where(s => (!string.IsNullOrEmpty(queryParameter.Author) && s.Author != null && s.Author.Contains(queryParameter.Author)) || (!string.IsNullOrEmpty(queryParameter.Title) && s.Title != null && s.Title.Contains(queryParameter.Title)) || (!string.IsNullOrEmpty(queryParameter.Lyrics) && s.Content != null && s.Content.Contains(queryParameter.Lyrics)) || (!string.IsNullOrEmpty(queryParameter.Themes) && s.Themes != null && s.Themes.Contains(queryParameter.Themes)) )); } else if (queryParameter.SearchCriteriaBuildType == SearchCriteriaBuildType.All) { return(songs.Where(s => (string.IsNullOrEmpty(queryParameter.Author) || (s.Author != null && s.Author.Contains(queryParameter.Author))) && (string.IsNullOrEmpty(queryParameter.Title) || (s.Title != null && s.Title.Contains(queryParameter.Title))) && (string.IsNullOrEmpty(queryParameter.Lyrics) || (s.Content != null && s.Content.Contains(queryParameter.Lyrics))) && (string.IsNullOrEmpty(queryParameter.Themes) || (s.Themes != null && s.Themes.Contains(queryParameter.Themes))) )); } else { return(songs.Where(s => (string.IsNullOrEmpty(queryParameter.Author) || (s.Author != null && s.Author.Equals(queryParameter.Author, StringComparison.InvariantCultureIgnoreCase))) && (string.IsNullOrEmpty(queryParameter.Title) || (s.Title != null && s.Title.Equals(queryParameter.Title, StringComparison.InvariantCultureIgnoreCase))) && (string.IsNullOrEmpty(queryParameter.Lyrics) || (s.Content != null && s.Content.Equals(queryParameter.Lyrics, StringComparison.InvariantCultureIgnoreCase))) && (string.IsNullOrEmpty(queryParameter.Themes) || (s.Themes != null && s.Themes.Equals(queryParameter.Themes, StringComparison.InvariantCultureIgnoreCase))) )); } } return(songs); }
public async Task <IActionResult> All([FromQuery] SongSearchParameters songSearchParameters = null) { SongFilterParameter songFilterParameter = null; if (songSearchParameters != null && !string.IsNullOrEmpty(songSearchParameters.Text)) { songFilterParameter = new SongFilterParameter { Author = songSearchParameters.type == SongSearchType.All || songSearchParameters.type == SongSearchType.Author ? songSearchParameters.Text : null, Lyrics = songSearchParameters.type == SongSearchType.All || songSearchParameters.type == SongSearchType.Lyrics ? songSearchParameters.Text : null, Title = songSearchParameters.type == SongSearchType.All || songSearchParameters.type == SongSearchType.Title ? songSearchParameters.Text : null, Themes = songSearchParameters.type == SongSearchType.All || songSearchParameters.type == SongSearchType.Tags ? songSearchParameters.Text : null, SearchCriteriaBuildType = SearchCriteriaBuildType.Any }; } var result = await _songManager.All(songFilterParameter); return(result.Match <IActionResult>(vm => { return Ok(vm); }, (error) => { return BadRequest(new ErrorViewModel(error, HttpContext)); } )); }
public async Task <IEnumerable <OSSong> > All(SongFilterParameter queryParameter) { if (_useCache) { return(_FilterSongs((await _GetCache()), queryParameter)); } else { return(await _All_fromDB(queryParameter)); } }
public async Task <PaginatedList <SongBrief> > Page(SongFilterParameter queryParameter, PagingParameter pagingParameter) { if (_useCache) { return(await _Page_fromCache(queryParameter, pagingParameter)); } else { return(await _Page_fromDB(queryParameter, pagingParameter)); } }
protected async Task <IEnumerable <OSSong> > _All_fromDB(SongFilterParameter queryParameter) { var query = _context.OSSongs .Include(s => s.CreatedBy) .AsNoTracking(); _SetIQueryableWhereConditions(query, queryParameter); query = query.OrderBy(s => s.Title); return(await query.ToListAsync()); }
protected void _SetIQueryableWhereConditions(IQueryable <OSSong> query, SongFilterParameter queryParameter) { if (queryParameter != null) { if (queryParameter.SearchCriteriaBuildType == SearchCriteriaBuildType.Any) { query = query.Where(s => (!string.IsNullOrEmpty(queryParameter.Author) && s.Author.Contains(queryParameter.Author)) || (!string.IsNullOrEmpty(queryParameter.Title) && s.Title.Contains(queryParameter.Title)) || (!string.IsNullOrEmpty(queryParameter.Lyrics) && s.Content.Contains(queryParameter.Lyrics)) || (!string.IsNullOrEmpty(queryParameter.Themes) && s.Themes.Contains(queryParameter.Themes))); } else if (queryParameter.SearchCriteriaBuildType == SearchCriteriaBuildType.All) { if (!string.IsNullOrEmpty(queryParameter.Author)) { query = query.Where(s => s.Author != null && s.Author.Contains(queryParameter.Author)); } if (!string.IsNullOrEmpty(queryParameter.Title)) { query = query.Where(s => s.Title != null && s.Title.Contains(queryParameter.Title)); } if (!string.IsNullOrEmpty(queryParameter.Lyrics)) { query = query.Where(s => s.Content != null && s.Content.Contains(queryParameter.Lyrics)); } if (!string.IsNullOrEmpty(queryParameter.Themes)) { query = query.Where(s => s.Themes != null && s.Themes.Contains(queryParameter.Themes)); } } else { if (!string.IsNullOrEmpty(queryParameter.Author)) { query = query.Where(s => s.Author != null && s.Author.Equals(queryParameter.Author, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(queryParameter.Title)) { query = query.Where(s => s.Title != null && s.Title.Equals(queryParameter.Title, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(queryParameter.Lyrics)) { query = query.Where(s => s.Content != null && s.Content.Equals(queryParameter.Lyrics, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(queryParameter.Themes)) { query = query.Where(s => s.Themes != null && s.Themes.Equals(queryParameter.Themes, StringComparison.InvariantCultureIgnoreCase)); } } } }
public async Task <PaginatedList <SongBrief> > _Page_fromDB(SongFilterParameter queryParameter, PagingParameter pagingParameter) { var query = _context.OSSongs .Include(s => s.CreatedBy) .AsNoTracking(); _SetIQueryableWhereConditions(query, queryParameter); var count = await query.CountAsync(); if (pagingParameter != null) { bool bIsAscending = (pagingParameter.columnOrder?.ToLower() ?? "asc") == "asc" ? true : false; pagingParameter.columnToOrder = pagingParameter.columnToOrder?.ToLower() ?? "title"; switch (pagingParameter?.columnToOrder) { case "title": default: query = bIsAscending ? query.OrderBy(s => s.Title) : query.OrderByDescending(s => s.Title); break; // TODO: other cases as needed. } //Apply paging //Offset before limit! if (pagingParameter.offset != null) { query = query.Skip((int)pagingParameter?.offset); } if (pagingParameter.limit != null) { query = query.Take((int)pagingParameter?.limit); } } var songs = await query.ToListAsync(); return(new PaginatedList <SongBrief> { Items = songs.Select(s => new SongBrief(s)).ToList(), Total = count, CurrentIndex = pagingParameter?.offset ?? 0, PageSize = pagingParameter?.limit ?? 0 }); }
public async Task <PaginatedList <SongBrief> > _Page_fromCache(SongFilterParameter queryParameter, PagingParameter pagingParameter) { IEnumerable <OSSong> songs = _FilterSongs(await _GetCache(), queryParameter); var count = songs.Count(); if (pagingParameter != null) { bool bIsAscending = (pagingParameter.columnOrder?.ToLower() ?? "asc") == "asc" ? true : false; pagingParameter.columnToOrder = pagingParameter.columnToOrder?.ToLower() ?? "title"; switch (pagingParameter?.columnToOrder) { case "title": default: songs = bIsAscending ? songs.OrderBy(s => s.Title) : songs.OrderByDescending(s => s.Title); break; // TODO: other cases as needed. } //Apply paging //Offset before limit! if (pagingParameter.offset != null) { songs = songs.Skip((int)pagingParameter?.offset); } if (pagingParameter.limit != null) { songs = songs.Take((int)pagingParameter?.limit); } } return(new PaginatedList <SongBrief> { Items = songs.Select(s => new SongBrief(s)).ToList(), Total = count, CurrentIndex = pagingParameter?.offset ?? 0, PageSize = pagingParameter?.limit ?? 0 }); }
public async Task <Either <List <SongBrief>, ErrorInfo> > All(SongFilterParameter queryParameter) { var results = await _OSSongRepo.All(queryParameter); return(results.Select(s => new SongBrief(s)).ToList()); }
public async Task <Either <PaginatedList <SongBrief>, ErrorInfo> > Page(SongFilterParameter queryParameter, PagingParameter pagingParameter) { return(await _OSSongRepo.Page(queryParameter, pagingParameter)); }