Ejemplo n.º 1
0
        private FilterDefinition <MyTask> CreateFieldfilter(Guid userId, TaskInfoSearchQuery query)
        {
            FilterDefinition <MyTask> filter = FilterDefinition <MyTask> .Empty;


            //  filter = Builders<MyTask>.Filter.Eq("_id", userId);

            if (query.CreatedFrom != null)
            {
                filter = Builders <MyTask> .Filter.Gte(t => t.CreatedAt, query.CreatedFrom.Value);
            }

            if (query.CreatedTo != null)
            {
                filter = filter & Builders <MyTask> .Filter.Lte(t => t.CreatedAt, query.CreatedTo.Value);
            }

            if (query.Done != null)
            {
                filter = filter & Builders <MyTask> .Filter.Eq(t => t.Done, query.Done.Value);
            }

            if (query.ToDeadline != null)
            {
                filter = filter & Builders <MyTask> .Filter.Lte(t => t.DeadLine, query.ToDeadline.Value);
            }

            if (query.Priority != null)
            {
                filter = filter & Builders <MyTask> .Filter.Eq(t => t.Priority, query.Priority.Value);
            }

            return(filter);
        }
Ejemplo n.º 2
0
        public Task <IReadOnlyList <TaskInfo> > SearchAsync(Guid userId, TaskInfoSearchQuery query, CancellationToken cancelltionToken)
        {
            if (userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }

            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            cancelltionToken.ThrowIfCancellationRequested();


            FilterDefinition <MyTask> filter = CreateFieldfilter(userId, query);


            IMongoCollection <MyTask> filteredTask = _tasks;

            /* if (filter != null)
             * {
             *   filteredTask = (IMongoCollection<MyTask>)filteredTask.Find(filter);
             * }*/

            var sort   = query.Sort ?? SortType.Ascending;
            var sortBy = query.SortBy ?? TaskSortBy.Creation;

            IEnumerable <MyTask> resultTasks = null;

            switch (sortBy)
            {
            case TaskSortBy.LastUpdate:
                resultTasks = (sort == SortType.Ascending)
                        ? filteredTask.Find(filter).SortBy(t => t.LastUpdatedAt).ToEnumerable()
                        : filteredTask.Find(filter).SortByDescending(t => t.LastUpdatedAt).ToEnumerable();
                break;

            case TaskSortBy.Creation:
                resultTasks = (sort == SortType.Ascending)
                        ? filteredTask.Find(filter).SortBy(t => t.CreatedAt).ToEnumerable()
                        : filteredTask.Find(filter).SortByDescending(t => t.CreatedAt).ToEnumerable();
                break;

            case TaskSortBy.Priority:
                resultTasks = (sort == SortType.Ascending)
                        ? filteredTask.Find(filter).SortBy(t => t.Priority).ToEnumerable()
                        : filteredTask.Find(filter).SortByDescending(t => t.Priority).ToEnumerable();
                break;

            default:
                throw new ArgumentException($"Unknown note sort by value \"{sortBy}\".");
            }

            if (query.Offset != null)
            {
                resultTasks = resultTasks.Skip(query.Offset.Value);
            }

            if (query.Limit != null)
            {
                resultTasks = resultTasks.Take(query.Limit.Value);
            }

            return(Task.FromResult <IReadOnlyList <TaskInfo> >(resultTasks.ToList()));
        }