public async Task <IActionResult> Index()
        {
            // Information from Identity through the user manager
            string       id   = _userManager.GetUserId(User);          // reportedly does not need to hit the db
            IdentityUser user = await _userManager.GetUserAsync(User); // does go to the db

            FujiUser fu = null;

            if (id != null)
            {
                //fu = _fujiDbContext.FujiUsers.Where(u => u.AspnetIdentityId == id).FirstOrDefault();
                fu = _fuRepo.GetFujiUserByIdentityId(id);
            }

            //var appleList = _fujiDbContext.Apples.ToList();
            var        appleList = _appleRepo.GetAll().ToList();
            MainPageVM vm        = new MainPageVM {
                TheIdentityUser = user, TheFujiUser = fu, Apples = appleList
            };

            ViewBag.TotalConsumed = _appleRepo.GetTotalConsumed(_appleRepo.GetAll());

            return(View(vm));
        }
        public JsonResult Eaten()
        {
            // Find the current user
            string aspNetUserID = _userManager.GetUserId(User);

            if (aspNetUserID == null)
            {
                return(Json(new { success = false, message = "user not logged in" }));
            }
            FujiUser fu = null;

            if (aspNetUserID != null)
            {
                //fu = _context.FujiUsers.Where(u => u.AspnetIdentityId == aspNetUserID).FirstOrDefault();
                fu = _fuRepo.GetFujiUserByIdentityId(aspNetUserID);
                if (fu == null)
                {
                    return(Json(new { success = false, message = "user not found" }));
                }
            }
            // We need to use var here since we're selecting a list of a dynamic object type (by using select new {}).  This is a complex LINQ query.  Use LinqPad to break it down and see what each step does.  I'm not sure it needs both select statements but it was easiest that way.  A join, groupby and then select would have worked.

            /*
             * var apples = _context.ApplesConsumeds
             *  .Where(ac => ac.FujiUser == fu)
             *  .Select(ac => new
             *  {
             *      VarietyName = ac.Apple.VarietyName,
             *      Count = ac.Count
             *  })
             *  .GroupBy(ac => ac.VarietyName)
             *  .Select(g => new
             *  {
             *      VarietyName = g.Key,
             *      Total = g.Sum(x => x.Count)
             *  });
             */
            // refactored to ...
            Dictionary <Apple, int> values = _fuRepo.GetCountOfSpecificApplesEaten(_appleRepo.GetAll(), fu);
            //Total and VarietyName
            var apples = values.Select(v => new { VarietyName = v.Key.VarietyName, Total = v.Value });

            return(Json(new { success = true, message = "ok", apples = apples }));
        }
示例#3
0
        public IList <AppleDto> GetApples()
        {
            var apples = appleRepository.GetAll();

            return(appleMapping.MapToDto(apples));
        }