public long GetCount(PagingParameters param)
        {
            if (_context.Routes == null || _context.Routes.Count() == 0)
            {
                return(0);
            }

            return(_context.Routes.Where(o => o.Route.StartsWith(param.filter.ToString())).Count());
        }
        public long GetOwnCount(PagingParameters param)
        {
            if (_context.Routes == null || _context.Routes.Count() == 0)
            {
                return(0);
            }

            var count = _context.RelRouteUser.Where(x => x.IdUser == param.user).Count();

            return(count);
        }
        public IEnumerable <UsersByRoutes> GetOwnPaged(PagingParameters param)
        {
            if (_context.Routes == null || _context.Routes.Count() == 0)
            {
                return(null);
            }

            var routesByUser = (from r in _context.Routes.Include("RouteVisit").Include("RelRouteUser")
                                join rr in _context.RelRouteUser on r.Id equals rr.IdRoute
                                where rr.IdUser == param.user
                                select r).Skip(param.pageIndex * param.pageSize).Take(param.pageSize).ToList();

            List <UsersByRoutes> usersByRoutes = new List <UsersByRoutes>();

            foreach (var item in routesByUser)
            {
                var flagUser     = false;
                var usersByRoute = (from rr in _context.RelRouteUser
                                    join u in _context.AspNetUsers on rr.IdUser equals u.Id
                                    where rr.IdRoute == item.Id
                                    select new User {
                    Id = rr.IdUser, UserName = u.UserName
                }).ToList();
                var favoritesUserFlag = _context.Favorites.Where(x => x.IdUser == param.user && item.Id == x.IdRoute).FirstOrDefault();

                if (favoritesUserFlag != null)
                {
                    flagUser = favoritesUserFlag.isFavorite;
                }
                var usersByRoutesModel = new UsersByRoutes {
                    Id = item.Id, PageTitle = item.PageTitle, isFavorite = flagUser, Users = usersByRoute, RedirectUrl = item.RedirectUrl, Route = item.Route, RelRouteUser = item.RelRouteUser, RouteVisit = item.RouteVisit, Status = item.Status
                };
                usersByRoutesModel.Exists = true;
                usersByRoutes.Add(usersByRoutesModel);
            }
            var usersByRoutesQuery = usersByRoutes.AsQueryable();

            if (!string.IsNullOrEmpty(param.sortOrder))
            {
                if (param.sortOrder == "VisitCount")
                {
                    if (param.sortAsc)
                    {
                        usersByRoutesQuery = usersByRoutesQuery.OrderBy(x => x.RouteVisit.Count);
                    }
                    else
                    {
                        usersByRoutesQuery = usersByRoutesQuery.OrderByDescending(x => x.RouteVisit.Count);
                    }
                }
            }

            return(usersByRoutes);
        }
        public IEnumerable <ApplicationUser> GetUsersPaged(PagingParameters param)
        {
            if (_context.Users == null || _context.Users.Count() == 0)
            {
                return(null);
            }

            List <ApplicationUser> users = _context.Users.OrderBy(x => x.UserName).ToList();

            IQueryable <ApplicationUser> usersQuery = users.AsQueryable().Where(u => u.UserName.StartsWith(param.filter.ToString())).Skip(param.pageIndex * param.pageSize).Take(param.pageSize);

            return(usersQuery);
        }
        public long GetOthersCount(PagingParameters param)
        {
            if (_context.Routes == null || _context.Routes.Count() == 0)
            {
                return(0);
            }

            var count = GetCount(param);
            var own   = GetOwnCount(param);

            count = count - own;

            return(count);
        }
        public IEnumerable <UsersByRoutes> GetPaged(PagingParameters param)
        {
            if (_context.Routes == null || _context.Routes.Count() == 0)
            {
                return(null);
            }

            IQueryable <Routes>  routes        = _context.Routes.Include("RouteVisit").Where(o => !string.IsNullOrEmpty(param.filter) ? o.Route.StartsWith(param.filter) : true).Skip(param.pageIndex * param.pageSize).Take(param.pageSize);
            List <UsersByRoutes> usersByRoutes = new List <UsersByRoutes>();

            try
            {
                foreach (var item in routes)
                {
                    var usersByRoute = (from rr in _context.RelRouteUser
                                        join u in _context.AspNetUsers on rr.IdUser equals u.Id
                                        where rr.IdRoute == item.Id
                                        select new User {
                        Id = rr.IdUser, UserName = u.UserName
                    }).ToList();

                    var usersByRoutesModel = new UsersByRoutes {
                        Id = item.Id, PageTitle = item.PageTitle, isFavorite = null, Users = usersByRoute, RedirectUrl = item.RedirectUrl, Route = item.Route, RelRouteUser = item.RelRouteUser, RouteVisit = item.RouteVisit, Status = item.Status
                    };

                    if (param.role == "Admin")
                    {
                        usersByRoutesModel.Exists = true;
                        usersByRoutes.Add(usersByRoutesModel);
                    }
                    else
                    {
                        if (usersByRoutesModel.Users.Exists(x => x.Id == param.user))
                        {
                            usersByRoutesModel.Exists = true;
                        }
                        else
                        {
                            usersByRoutesModel.Exists = false;
                        }
                        usersByRoutes.Add(usersByRoutesModel);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            var usersByRoutesQuery = usersByRoutes.AsQueryable();

            if (!string.IsNullOrEmpty(param.sortOrder))
            {
                if (param.sortOrder == "VisitCount")
                {
                    if (param.sortAsc)
                    {
                        usersByRoutesQuery = usersByRoutesQuery.OrderBy(x => x.RouteVisit.Count);
                    }
                    else
                    {
                        usersByRoutesQuery = usersByRoutesQuery.OrderByDescending(x => x.RouteVisit.Count);
                    }
                }
            }

            return(usersByRoutesQuery);
        }
 public long GetCount(PagingParameters param)
 {
     return((_context.Users == null || _context.Users.Count() == 0)? 0 : _context.Users.Count());
 }