void FilterSimple(WebData webData, ITable table, string fieldName, string filter, int page) { if (table == null) { throw new ArgumentNullException(nameof(table)); } if (fieldName == null) { throw new ArgumentNullException(nameof(fieldName)); } Search search; long rowCount; if (filter == null) { search = Search.None; rowCount = table.RowCount; } else { search = Search.FieldLike(fieldName, MDBSearch.Text(filter)); rowCount = table.Count(search); } ResultOption options = ResultOption.Limit(RowsPerPage) + ResultOption.SortAscending(fieldName); if (page > 0) { options += ResultOption.Offset(page * RowsPerPage); } var rows = table.GetRows(search, options); webData.Result.AddMessage(webData.Method, "Retrieved {0}.", table); AddPagination(webData, page, rowCount); webData.Result.AddRows(rows, table.Layout); }
void FilterList(WebData webData, ITable table, string nameField, string guidField, string text) { var ids = new Set <long>(); if (text == null) { ids.IncludeRange(table.FindRows(Search.None, ResultOption.Limit(20))); } else { ids.AddRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text(text + "%")) & Search.FieldNotEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Group(guidField) + ResultOption.Limit(20))); if (ids.Count < 20) { ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text("% " + text + "%")) & Search.FieldNotEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Group(guidField) + ResultOption.Limit(20))); } if (ids.Count < 20) { ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text(text + "%")) & Search.FieldEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Limit(20 - ids.Count))); } if (ids.Count < 20) { ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text("% " + text + "%")) & Search.FieldEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Limit(20 - ids.Count))); } } var json = new JsonWriter(); json.BeginArray("results"); if (ids.Count > 0) { //get items var values = table.GetValues <string>(nameField, false, ids.SubRange(0, Math.Min(20, ids.Count))); foreach (var value in values) { json.BeginObject(); json.String("id", value); json.String("text", value); json.EndObject(); } } json.EndArray(); var message = WebMessage.Create(webData.Method, $"Filter {nameField} {text}"); webData.Answer = WebAnswer.Json(webData.Request, message, json.ToString()); }
/// <summary>Gets the subset audio file IDs.</summary> /// <param name="subsetID">The subset identifier.</param> /// <param name="minDuration">The minimum duration.</param> /// <param name="maxDuration">The maximum duration.</param> /// <returns></returns> public IList <long> GetSubsetAudioFileIDs(long subsetID, TimeSpan minDuration, TimeSpan maxDuration) { Search search = Search.None; #region whitelist { var whitelist = SubsetFilters.GetStructs(Search.FieldEquals(nameof(MDBSubsetFilter.SubsetID), subsetID) & Search.FieldEquals(nameof(MDBSubsetFilter.Mode), MDBSubsetFilterMode.Whitelist)); foreach (MDBSubsetFilter filter in whitelist) { switch (filter.Type) { case MDBSubsetFilterType.Album: foreach (long albumID in Albums.FindRows(Search.FieldLike(nameof(MDBAlbum.Name), MDBSearch.Text(filter.Text)))) { search |= Search.FieldEquals(nameof(MDBAudioFile.AlbumID), albumID); } break; case MDBSubsetFilterType.Artist: foreach (long artistID in Artists.FindRows(Search.FieldLike(nameof(MDBArtist.Name), MDBSearch.Text(filter.Text)))) { search |= Search.FieldEquals(nameof(MDBAudioFile.SongArtistID), artistID); } break; case MDBSubsetFilterType.Category: { var ids = Categories.FindRows(nameof(MDBCategory.Name), MDBSearch.Text(filter.Text)); if (ids.Count != 1) { this.LogError(string.Format("Error at SubsetFilter {0}", filter)); } foreach (long id in ids) { search |= Search.FieldEquals(nameof(MDBAudioFile.CategoryID), id); } break; } case MDBSubsetFilterType.Tag: { search |= Search.FieldLike(nameof(MDBAudioFile.Tags), MDBSearch.Text(filter.Text)); break; } case MDBSubsetFilterType.Genre: { search |= Search.FieldLike(nameof(MDBAudioFile.Genres), MDBSearch.Text(filter.Text)); break; } case MDBSubsetFilterType.Title: { search |= Search.FieldLike("Title", MDBSearch.Text(filter.Text)); break; } default: this.LogError(string.Format("Unknown SubsetType at SubsetFilter {0}", filter)); break; } } } #endregion #region blacklist { var blacklist = SubsetFilters.GetStructs(Search.FieldEquals(nameof(MDBSubsetFilter.SubsetID), subsetID) & Search.FieldEquals(nameof(MDBSubsetFilter.Mode), MDBSubsetFilterMode.Blacklist)); foreach (MDBSubsetFilter filter in blacklist) { switch (filter.Type) { case MDBSubsetFilterType.Album: foreach (long albumID in Albums.FindRows(Search.FieldLike(nameof(MDBAlbum.Name), MDBSearch.Text(filter.Text)))) { search &= !Search.FieldEquals(nameof(MDBAudioFile.AlbumID), albumID); } break; case MDBSubsetFilterType.Artist: foreach (long artistID in Artists.FindRows(Search.FieldLike(nameof(MDBArtist.Name), MDBSearch.Text(filter.Text)))) { search &= !Search.FieldEquals(nameof(MDBAudioFile.SongArtistID), artistID); } break; case MDBSubsetFilterType.Category: { var ids = Categories.FindRows(nameof(MDBCategory.Name), MDBSearch.Text(filter.Text)); if (ids.Count != 1) { this.LogError(string.Format("Error at SubsetFilter {0}", filter)); } foreach (long id in ids) { search &= !Search.FieldEquals(nameof(MDBAudioFile.CategoryID), id); } break; } case MDBSubsetFilterType.Tag: { search &= !Search.FieldLike(nameof(MDBAudioFile.Tags), MDBSearch.Text(filter.Text)); break; } case MDBSubsetFilterType.Genre: { search &= !Search.FieldLike(nameof(MDBAudioFile.Genres), MDBSearch.Text(filter.Text)); break; } case MDBSubsetFilterType.Title: { search &= !Search.FieldLike("Title", MDBSearch.Text(filter.Text)); break; } default: this.LogError(string.Format("Unknown SubsetType at SubsetFilter {0}", filter)); break; } } } #endregion if (minDuration > TimeSpan.Zero) { search &= Search.FieldGreaterOrEqual(nameof(MDBAudioFile.Duration), minDuration); } if (maxDuration > minDuration) { search &= Search.FieldSmallerOrEqual(nameof(MDBAudioFile.Duration), maxDuration); } return(AudioFiles.FindRows(search)); }
public void SearchAlbums(WebData webData, long artistID = 0, string filter = null, int page = 0, long categoryID = 0, long genreID = 0, long tagID = 0, string genre = null, string tag = null) { ICollection <long> albumIDs = null; //select audio files if (genreID != 0 || categoryID != 0 || tagID != 0 || genre != null || tag != null) { Search s = Search.None; if (genreID != 0) { s &= Search.FieldEquals(nameof(MDBAudioFile.GenreID), genreID); } if (tagID != 0) { s &= Search.FieldEquals(nameof(MDBAudioFile.TagID), tagID); } if (genre != null) { s &= Search.FieldLike(nameof(MDBAudioFile.Genres), MDBSearch.Text("%" + genre + "%")); } if (tag != null) { s &= Search.FieldLike(nameof(MDBAudioFile.Tags), MDBSearch.Text("%" + tag + "%")); } if (categoryID > 0) { s &= GetCategorySearch(categoryID); } int fieldIndex = mdb.AudioFiles.Layout.GetFieldIndex(nameof(MDBAudioFile.AlbumID)); albumIDs = mdb.AudioFiles.GetRows(s).Select(r => (long)r.GetValue(fieldIndex)).ToList(); } //select artists IList <MDBAlbum> albums; long rowCount; { Search search = Search.None; if (filter != null) { search &= Search.FieldLike(nameof(MDBAlbum.Name), MDBSearch.Text("%" + filter + "%")); } if (albumIDs != null) { search &= Search.FieldIn(nameof(MDBAlbum.ID), albumIDs); } if (artistID != 0) { search &= Search.FieldEquals(nameof(MDBAlbum.ArtistID), artistID); } if (search.Mode == SearchMode.None) { rowCount = mdb.Albums.RowCount; } else { rowCount = mdb.Albums.Count(search); } albums = mdb.Albums.GetStructs(search, ResultOption.SortAscending(nameof(MDBAlbum.Name)) + ResultOption.Offset(page * RowsPerPage) + ResultOption.Limit(RowsPerPage)); } //join var result = albums.Select(i => RPCAlbum.Load(mdb, i)); //return webData.Result.AddMessage(webData.Method, "Retrieved Albums."); AddPagination(webData, page, rowCount); webData.Result.AddStructs(result); }
public void SearchAudioFiles(WebData webData, int page = 0, long artistID = 0, long albumID = 0, long categoryID = 0, long genreID = 0, long tagID = 0, string filter = null, string title = null, string album = null, string artist = null, string genre = null, string tag = null) { if (title == null) { title = filter; } Search search = Search.None; if (artistID > 0) { if (artist != null) { throw new WebServerException(WebError.InvalidParameters, 0, "Cannot use artist search and artistID at the same time!"); } search &= Search.FieldEquals(nameof(MDBAudioFile.AlbumArtistID), artistID) | Search.FieldEquals(nameof(MDBAudioFile.SongArtistID), artistID); } else if (artist != null) { var ids = mdb.Artists.FindRows(Search.FieldLike(nameof(MDBArtist.Name), artist)); search &= Search.FieldIn(nameof(MDBAudioFile.AlbumArtistID), ids) | Search.FieldIn(nameof(MDBAudioFile.SongArtistID), ids); } if (albumID > 0) { search &= Search.FieldEquals(nameof(MDBAudioFile.AlbumID), albumID); if (album != null) { throw new WebServerException(WebError.InvalidParameters, 0, "Cannot use album search and albumID at the same time!"); } } else if (album != null) { var ids = mdb.Albums.FindRows(Search.FieldLike(nameof(MDBArtist.Name), album)); search &= Search.FieldIn(nameof(MDBAudioFile.AlbumID), ids); } if (categoryID > 0) { search &= GetCategorySearch(categoryID); } if (genreID > 0) { search &= Search.FieldEquals(nameof(MDBAudioFile.GenreID), genreID); } if (tagID > 0) { search &= Search.FieldEquals(nameof(MDBAudioFile.TagID), tagID); } if (genre != null) { search &= Search.FieldLike(nameof(MDBAudioFile.Genres), MDBSearch.Text("%" + genre + "%")); } if (tag != null) { search &= Search.FieldLike(nameof(MDBAudioFile.Tags), MDBSearch.Text("%" + tag + "%")); } if (title != null) { search &= Search.FieldLike(nameof(MDBAudioFile.Title), MDBSearch.Text("%" + title + "%")); } { long rowCount = mdb.AudioFiles.Count(search); var ids = mdb.AudioFiles.FindRows(search, ResultOption.Limit(RowsPerPage) + ResultOption.Offset(page * RowsPerPage) + ResultOption.SortAscending(nameof(MDBAudioFile.Title))); webData.Result.AddMessage(webData.Method, "Found {0} matching AudioFiles.", rowCount); AddPagination(webData, page, rowCount); var files = ids.Select(i => RPCAudioFile.Load(mdb, i)); webData.Result.AddStructs(files); } }