/// <summary> /// Найти заметки /// </summary> /// <param name="query">Поисковый запрос</param> /// <param name="cancelltionToken">Токен отмены операции</param> /// <returns>Задача, представляющая асинхронный поиск заметок. Результат выполнения - список найденных заметок</returns> public Task <IReadOnlyList <NoteInfo> > SearchAsync(NoteInfoSearchQuery query, CancellationToken cancelltionToken) { if (query == null) { throw new ArgumentNullException(nameof(query)); } cancelltionToken.ThrowIfCancellationRequested(); var search = this.sequenceIndex.AsEnumerable(); if (query.CreatedFrom != null) { search = search.Where(note => note.CreatedAt >= query.CreatedFrom.Value); } if (query.CreatedTo != null) { search = search.Where(note => note.CreatedAt <= query.CreatedTo.Value); } if (query.UserId != null) { search = search.Where(note => note.UserId == query.UserId.Value); } if (query.Favorite != null) { search = search.Where(note => note.Favorite = query.Favorite.Value); } if (query.Tags != null) { bool whereTagsPresent(NoteInfo noteInfo) { return(query.Tags.All(tag => noteInfo.Tags.Contains(tag))); } search = search.Where(whereTagsPresent); } if (query.Offset != null) { search = search.Take(query.Offset.Value); } if (query.Limit != null) { search = search.Take(query.Limit.Value); } var sort = query.Sort ?? SortType.Ascending; var sortBy = query.SortBy ?? NoteSortBy.Creation; if (sort != SortType.Ascending || sortBy != NoteSortBy.Creation) { DateTime select(NoteInfo note) { switch (sortBy) { case NoteSortBy.LastUpdate: return(note.LastUpdatedAt); case NoteSortBy.Creation: return(note.CreatedAt); default: throw new ArgumentException($"Unknown note sort by value \"{sortBy}\".", nameof(query)); } } search = sort == SortType.Ascending ? search.OrderBy(select) : search.OrderByDescending(select); } var result = search.ToList(); return(Task.FromResult <IReadOnlyList <NoteInfo> >(result)); }
public Task <IReadOnlyList <NoteInfo> > SearchAsync(NoteInfoSearchQuery query, CancellationToken cancelltionToken) { if (query == null) { throw new ArgumentNullException(nameof(query)); } cancelltionToken.ThrowIfCancellationRequested(); var builder = Builders <Note> .Filter; var filter = builder.Empty; if (query.CreatedFrom != null) { filter |= builder.Gte("CreatedAt", query.CreatedFrom.Value); } if (query.CreatedTo != null) { filter |= builder.Lte("CreatedAt", query.CreatedTo.Value); } if (query.UserId != null) { filter |= builder.Eq("UserId", query.UserId.Value); } if (query.Favorite != null) { filter |= builder.Eq("Favorite", query.Favorite.Value); } var result = notes.Find(filter); if (query.Offset != null) { result = result.Skip(query.Offset.Value); } if (query.Limit != null) { result = result.Limit(query.Offset.Value); } var sort = query.Sort ?? SortType.Ascending; var sortBy = query.SortBy ?? NoteSortBy.Creation; if (sort != SortType.Ascending || sortBy != NoteSortBy.Creation) { string sortField = ""; switch (sortBy) { case NoteSortBy.LastUpdate: sortField = "LastUpdatedAt"; break; case NoteSortBy.Creation: sortField = "CreatedAt"; break; default: throw new ArgumentException($"Unknown note sort by value \"{sortBy}\".", nameof(query)); } var sortDefinition = sort == SortType.Ascending ? new SortDefinitionBuilder <Note>().Ascending(sortField) : new SortDefinitionBuilder <Note>().Ascending(sortField); result = result.Sort(sortDefinition); } var listResult = result.ToList(); if (query.Tags != null) { listResult = listResult .Where(note => query.Tags.All(tag => note.Tags.Contains(tag))) .ToList(); } return(Task.FromResult <IReadOnlyList <NoteInfo> >(listResult)); }