public async Task <IActionResult> AddReward(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var user = await _context.Users
                       .FirstOrDefaultAsync(m => m.Id == id);

            if (user == null)
            {
                return(NotFound());
            }

            var userRewards = user.Rewards.Where(x => x.UserId == user.Id).Select(x => x.Reward).ToList();
            var rewards     = _context.Rewards.ToList();

            foreach (var ur in userRewards.Where(ur => rewards.Exists(x => x.Id == ur.Id)))
            {
                rewards.Remove(ur);
            }

            var model = new AddRewardViewModel()
            {
                Rewards = rewards,
                UserId  = user.Id
            };

            return(View(model));
        }
        public async Task <IActionResult> AddReward(AddRewardViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            _context.UserRewards.Add(new UserReward()
            {
                UserId   = model.UserId,
                RewardId = model.RewardId
            });
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #3
0
        public ActionResult Create([Bind("CustomerId", "Points")] AddRewardViewModel addReward)
        {
            try
            {
                // TODO: Add insert logic here

                rewadsRepository.AddPoints(addReward.CustomerId, addReward.Points);
                return(RedirectToAction(nameof(Details)));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                ModelState.AddModelError("", "Unable to save changes. The Record was modified by another user after you got the original value.");

                return(View());
            }
        }