public async Task <IEnumerable <AuditLog> > FindAsync(SensateUser user, string text, int skip = 0, int limit = 0, OrderDirection direction = OrderDirection.None)
        {
            var query = this.Data.Where(log => log.AuthorId == user.Id && log.Route.Contains(text));

            if (skip > 0)
            {
                query = query.Skip(skip);
            }

            if (limit > 0)
            {
                query = query.Take(limit);
            }

            query = direction switch {
                OrderDirection.Ascending => query.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => query.OrderByDescending(x => x.Timestamp),
                _ => query
            };

            var result = await query.ToListAsync().AwaitBackground();

            foreach (var log in result)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(result);
        }
        public async Task <PaginationResult <AuditLog> > FindAsync(string text,
                                                                   RequestMethod method     = RequestMethod.Any,
                                                                   int skip                 = 0,
                                                                   int limit                = 0,
                                                                   OrderDirection direction = OrderDirection.None)
        {
            IQueryable <AuditLog> query;
            var rv = new PaginationResult <AuditLog>();

            if (text == null)
            {
                return(await this.GetAllAsync(method, skip, limit).AwaitBackground());
            }

            if (method != RequestMethod.Any)
            {
                query = this.Data.Where(log => log.Route.Contains(text) && log.Method == method);
            }
            else
            {
                query = this.Data.Where(log => log.Route.Contains(text));
            }

            rv.Count = await query.CountAsync().AwaitBackground();

            if (skip > 0)
            {
                query = query.Skip(skip);
            }

            if (limit > 0)
            {
                query = query.Take(limit);
            }

            query = direction switch {
                OrderDirection.Ascending => query.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => query.OrderByDescending(x => x.Timestamp),
                _ => query
            };

            rv.Values = await query.ToListAsync().AwaitBackground();

            foreach (var log in rv.Values)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(rv);
        }
        public async Task <IEnumerable <AuditLog> > GetByRouteAsync(SensateUser user,
                                                                    string route,
                                                                    DateTime start,
                                                                    DateTime end,
                                                                    int skip  = 0,
                                                                    int limit = 0,
                                                                    OrderDirection direction = OrderDirection.None)
        {
            var result =
                from log in this.Data
                where log.AuthorId == user.Id &&
                log.Route == route &&
                log.Timestamp >= start &&
                log.Timestamp <= end
                orderby log.Timestamp
                select log;

            var data = result.AsQueryable();

            if (skip > 0)
            {
                data = data.Skip(skip);
            }

            if (limit > 0)
            {
                data = data.Take(limit);
            }

            data = direction switch {
                OrderDirection.Ascending => data.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => data.OrderByDescending(x => x.Timestamp),
                _ => data
            };

            var rv = await data.ToListAsync().AwaitBackground();

            foreach (var log in result)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(rv);
        }
        public async Task <PaginationResult <AuditLog> > GetByRequestTypeAsync(SensateUser user,
                                                                               RequestMethod method,
                                                                               int skip  = 0,
                                                                               int limit = 0,
                                                                               OrderDirection direction = OrderDirection.None)
        {
            var rv = new PaginationResult <AuditLog>();

            var query =
                from log in this.Data
                where log.AuthorId == user.Id &&
                log.Method == method
                orderby log.Timestamp
                select log;
            var data = query.AsQueryable();

            rv.Count = await data.CountAsync().AwaitBackground();

            if (skip > 0)
            {
                data = data.Skip(skip);
            }

            if (limit > 0)
            {
                data = data.Take(limit);
            }

            data = direction switch {
                OrderDirection.Ascending => data.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => data.OrderByDescending(x => x.Timestamp),
                _ => data
            };

            rv.Values = await data.ToListAsync().AwaitBackground();

            foreach (var log in rv.Values)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(rv);
        }
        public async Task <PaginationResult <AuditLog> > GetAllAsync(RequestMethod method, int skip = 0, int limit = 0, OrderDirection direction = OrderDirection.None)
        {
            IQueryable <AuditLog> result;
            var rv = new PaginationResult <AuditLog>();

            if (method == RequestMethod.Any)
            {
                result = this.Data.AsQueryable();
            }
            else
            {
                result = this.Data.Where(log => log.Method == method);
            }

            result = direction switch {
                OrderDirection.Ascending => result.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => result.OrderByDescending(x => x.Timestamp),
                _ => result
            };

            rv.Count = await result.CountAsync().AwaitBackground();

            if (skip > 0)
            {
                result = result.Skip(skip);
            }

            if (limit > 0)
            {
                result = result.Take(limit);
            }

            rv.Values = await result.ToListAsync().AwaitBackground();

            foreach (var log in rv.Values)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(rv);
        }
        public async Task <PaginationResult <AuditLog> > GetByUserAsync(SensateUser user,
                                                                        int skip  = 0,
                                                                        int limit = 0,
                                                                        OrderDirection direction = OrderDirection.None)
        {
            var data = this.Data.Where(x => x.AuthorId == user.Id);
            var rv   = new PaginationResult <AuditLog> {
                Count = await data.CountAsync().AwaitBackground()
            };

            if (skip > 0)
            {
                data = data.Skip(skip);
            }

            if (limit > 0)
            {
                data = data.Take(limit);
            }

            data = direction switch {
                OrderDirection.Ascending => data.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => data.OrderByDescending(x => x.Timestamp),
                _ => data
            };

            rv.Values = await data.ToListAsync().AwaitBackground();

            foreach (var log in rv.Values)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(rv);
        }
    }
        public async Task <PaginationResult <AuditLog> > FindAsync(
            IEnumerable <string> uids,
            string text,
            RequestMethod method     = RequestMethod.Any,
            int skip                 = 0,
            int limit                = 0,
            OrderDirection direction = OrderDirection.None
            )
        {
            IQueryable <AuditLog> logs;
            var rv = new PaginationResult <AuditLog>();

            if (method == RequestMethod.Any)
            {
                if (text == null)
                {
                    logs =
                        from log in this.Data
                        where uids.Contains(log.AuthorId)
                        select log;
                }
                else
                {
                    logs =
                        from log in this.Data
                        where uids.Contains(log.AuthorId) &&
                        log.Route.Contains(text)
                        select log;
                }
            }
            else
            {
                if (text == null)
                {
                    logs = this.Data.Where(log => uids.Contains(log.AuthorId) && log.Method == method);
                }
                else
                {
                    logs = this.Data.Where(log => uids.Contains(log.AuthorId) && log.Method == method && log.Route.Contains(text));
                }
            }

            logs = direction switch {
                OrderDirection.Ascending => logs.OrderBy(x => x.Timestamp),
                OrderDirection.Descending => logs.OrderByDescending(x => x.Timestamp),
                _ => logs
            };

            rv.Count = await logs.CountAsync().AwaitBackground();

            if (skip > 0)
            {
                logs = logs.Skip(skip);
            }

            if (limit > 0)
            {
                logs = logs.Take(limit);
            }

            rv.Values = await logs.ToListAsync().AwaitBackground();

            foreach (var log in rv.Values)
            {
                log.Timestamp = DateTime.SpecifyKind(log.Timestamp, DateTimeKind.Utc);
            }

            return(rv);
        }