public static Model.NoteSearchQuery Convert(View.NoteSearchQuery viewNoteSearchQuery)
        {
            if (viewNoteSearchQuery == null)
            {
                throw new ArgumentNullException(nameof(viewNoteSearchQuery));
            }

            var modelNoteSearchQuery = new Model.NoteSearchQuery
            {
                Skip       = viewNoteSearchQuery.Skip ?? 0,
                Take       = viewNoteSearchQuery.Take ?? 10,
                Favorite   = viewNoteSearchQuery.Favorite ?? false,
                FromDate   = viewNoteSearchQuery.FromDate,
                ToDate     = viewNoteSearchQuery.FromDate,
                Descending = viewNoteSearchQuery.Descending ?? false,
            };

            return(modelNoteSearchQuery);
        }
예제 #2
0
        public IReadOnlyList <Note> Search(NoteSearchQuery searchQuery)
        {
            if (searchQuery == null)
            {
                throw new ArgumentNullException(nameof(searchQuery));
            }

            var searchResult = this.notes.AsEnumerable();

            if (searchQuery.FromDate.HasValue)
            {
                searchResult = searchResult.Where(it => it.CreatedAt >= searchQuery.FromDate);
            }

            if (searchQuery.ToDate.HasValue)
            {
                searchResult = searchResult.Where(it => it.CreatedAt < searchQuery.ToDate);
            }

            if (searchQuery.Favorite == true)
            {
                searchResult = searchResult.Where(it => it.Favorite);
            }

            if (searchQuery.Descending == true)
            {
                searchResult = searchResult.OrderByDescending(it => it.CreatedAt);
            }
            else
            {
                searchResult = searchResult.OrderBy(it => it.CreatedAt);
            }

            searchResult = searchResult.Skip(searchQuery.Skip ?? 0);

            // ограничиваем количество получаемых заметок до 100 чтобы сервис не упал
            searchResult = searchResult.Take(Math.Min(searchQuery.Take ?? 10, 100));

            return(searchResult.ToList());
        }