Пример #1
0
        /// <summary>
        /// Admin Dashboard Page
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            var userId                 = User.Claims.FirstOrDefault(x => x.Type == "UserId").Value;
            var roleClaims             = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value;
            List <RoleViewModel> roles = null;
            bool admin                 = false;

            if (roleClaims != null)
            {
                roles = JsonConvert.DeserializeObject <List <RoleViewModel> >(roleClaims);
            }
            else
            {
                roles = new List <RoleViewModel>
                {
                    new RoleViewModel
                    {
                        UserID = userId == null ? 0 : long.Parse(userId),
                        Name   = "ROLE_USER"
                    }
                };
            }

            //get new adverts
            Expression <Func <Advert, bool> > adPredicate = a => a.Status == EnumTypes.AdvertStatus.SUBMITTED.ToString();

            Expression <Func <Advert, object> >[] adInclude = { a => a.Detail };
            var newAds = mapper.Map <IEnumerable <AdvertViewModel> >(advertService.FindAll(adPredicate, adInclude));

            var likes = mapper.Map <IEnumerable <LikeViewModel> >
                            (likeService.FindByUser(long.Parse(userId)));

            List <CountPercentSummary> advertSummary = CountAdvertByStatus();

            //Admin Analytics
            if (roles.Any(r => r.Name == "ROLE_ADMIN"))
            {
                admin = true;

                //Advert and User Smmaries
                ViewBag.CountUsers      = userService.CountAllUsers();
                ViewBag.CountAdverts    = advertService.CountAdverts(string.Empty);
                ViewBag.CountNewAdverts = advertService.CountAdverts("submitted");

                ViewBag.AdvertSummary = advertSummary;

                //Get New Users
                //Expression<Func<User, bool>> userPredicate = a => a.IsVerified == 0;
                Expression <Func <User, object> >[] userInclude = { a => a.UserDetail };
                var newUsers = mapper.Map <IEnumerable <UserViewModel> >(userService.FindAll(null, userInclude));

                ViewBag.Adverts = newAds.OrderBy(x => x.Days).Take(4);
                ViewBag.Users   = newUsers.OrderBy(x => x.Days).Take(4);
            }
            else if (roles.Any(r => r.UserID > 0))
            {
                //User Analytics
                ViewBag.CountAdverts     = advertService.CountAdvertsByUser(long.Parse(userId));
                ViewBag.CountApprovedAds = advertService.CountAdvertsByUserByStatus(long.Parse(userId), "approved");
                ViewBag.CountFavourites  = likeService.CountLikesByUser(long.Parse(userId));

                ViewBag.AdvertSummary = advertSummary;

                ViewBag.Adverts = newAds.OrderBy(x => x.Days).Take(4); //latest ads
                ViewBag.Likes   = likes.OrderBy(x => x.Days).Take(4);  //user's likes
            }

            return(admin ? View("Index") : View("Dashboard"));
        }