示例#1
0
        public async Task <IActionResult> Redeemed([FromRoute] int id)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            try
            {
                Child child = await context.Child.Where(c => c.UserId == user.Id).SingleAsync();
            }
            catch
            {
                return(BadRequest(new { error = "The current user is not authorized to update this reward" }));
            }

            try
            {
                RewardEarned rewardEarned = await context.RewardEarned.Where(re => re.RewardEarnedId == id).SingleAsync();

                rewardEarned.IsRedeemed   = true;
                rewardEarned.DateRedeemed = DateTime.Now;

                context.Update(rewardEarned);
                await context.SaveChangesAsync();

                await context.Entry(rewardEarned).GetDatabaseValuesAsync();

                return(Json(new { success = "Reward earned has been marked as redeemed!", rewardEarned }));
            }
            catch
            {
                return(BadRequest(new { error = $"There is not a reward earned with the id #{id}.  Did you forget to add the rewardEarned id? ex: rewardearned/redeemed/{{id}}" }));
            }
        }
示例#2
0
        public async Task <IActionResult> Single([FromRoute] int id)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            try
            {
                RewardEarned rewardEarned = await context.RewardEarned.Where(re => re.RewardId == id).SingleAsync();

                return(Json(new { success = "Reward earned found!", rewardEarned }));
            }
            catch
            {
                return(BadRequest(new { error = $"No reward earned can be found for the reward id #{id}" }));
            }
        }
示例#3
0
        public async Task <IActionResult> Create([FromRoute] int id)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            RewardEarned rewardEarned = new RewardEarned();

            try
            {
                Child child = await context.Child.Where(c => c.UserId == user.Id).SingleAsync();

                Reward reward = await context.Reward.Where(r => r.RewardId == id).SingleAsync();

                rewardEarned.RewardId = id;
                context.Add(rewardEarned);
                await context.SaveChangesAsync();

                await context.Entry(rewardEarned).GetDatabaseValuesAsync();

                List <Event>      events      = new List <Event>();
                List <EventPoint> eventPoints = new List <EventPoint>();

                events = await context.Event.Where(e => e.RewardId == id).ToListAsync();

                foreach (var snglEvent in events)
                {
                    eventPoints = await context.EventPoint.Where(ep => ep.EventId == snglEvent.EventId).ToListAsync();

                    foreach (var point in eventPoints)
                    {
                        point.RewardEarnedId = rewardEarned.RewardEarnedId;
                        context.Update(point);
                    }
                    await context.SaveChangesAsync();
                }

                return(Json(new { success = "Reward earned saved!", rewardEarned }));
            }
            catch
            {
                return(BadRequest(new { error = "Unable to add reward earned" }));
            }
        }
示例#4
0
        public async Task <IActionResult> All([FromRoute] int id)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            List <Event>        events        = new List <Event>();
            List <RewardEarned> rewardsEarned = new List <RewardEarned>();
            Child child = new Child();

            try
            {
                child = await context.Child.Where(c => c.ChildId == id).SingleAsync();
            }
            catch
            {
                return(BadRequest(new { error = $"There is no child with the id #{id}" }));
            }

            if (child.UserId != user.Id)
            {
                return(BadRequest(new { error = $"This user is not authorized to view the child with id #{id}" }));
            }

            events = await context.Event.Where(e => e.ChildId == child.ChildId).ToListAsync();

            foreach (var snglEvent in events)
            {
                Reward reward = await context.Reward.Where(r => r.RewardId == snglEvent.RewardId).SingleOrDefaultAsync();

                if (reward != null)
                {
                    RewardEarned rewardEarned = await context.RewardEarned.Where(re => re.RewardId == reward.RewardId).SingleOrDefaultAsync();

                    if (rewardEarned != null && !rewardsEarned.Contains(rewardEarned))
                    {
                        rewardsEarned.Add(rewardEarned);
                    }
                }
            }
            return(Json(new { success = "Found rewards earned!", rewardsEarned }));
        }
示例#5
0
        public async Task <IActionResult> Remove([FromRoute] int id)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            List <Event>        events        = new List <Event>();
            List <Reward>       rewards       = new List <Reward>();
            List <EventPoint>   allPoints     = new List <EventPoint>();
            List <RewardEarned> rewardsEarned = new List <RewardEarned>();

            //checks that the logged in user is authorized to delete this child record
            try
            {
                Child child = await context.Child.Where(c => c.ChildId == id && c.UserId == user.Id).SingleAsync();

                //get all event ids attached to the passed in child
                events = await context.Event.Where(e => e.ChildId == id).ToListAsync();

                //get add the rewards and event points for the passed in child from the reward ids and event ids
                foreach (var singleEvent in events)
                {
                    List <EventPoint> tempList = new List <EventPoint>();

                    Reward reward = await context.Reward.Where(r => r.RewardId == singleEvent.RewardId).SingleOrDefaultAsync();

                    if (!rewards.Contains(reward) && reward != null)
                    {
                        rewards.Add(reward);
                    }

                    tempList = await context.EventPoint.Where(p => p.EventId == singleEvent.EventId).ToListAsync();

                    if (tempList.Any())
                    {
                        foreach (var item in tempList)
                        {
                            allPoints.Add(item);
                        }
                    }
                }
                //get all the rewards earned for the passed in child from the reward ids
                foreach (var reward in rewards)
                {
                    RewardEarned rewardEarned = await context.RewardEarned.Where(re => re.RewardId == reward.RewardId).SingleOrDefaultAsync();

                    if (rewardEarned != null)
                    {
                        rewardsEarned.Add(rewardEarned);
                    }
                }

                //attempt to remove all of the rewards, points, events, earned rewards and the child
                try
                {
                    ForEachContextRemove(allPoints.Cast <object>().ToList());
                    ForEachContextRemove(rewardsEarned.Cast <object>().ToList());
                    ForEachContextRemove(rewards.Cast <object>().ToList());
                    ForEachContextRemove(events.Cast <object>().ToList());
                    context.Remove(child);

                    await context.SaveChangesAsync();

                    return(Json(new { success = "The child was removed!" }));
                }
                catch
                {
                    return(BadRequest(new { error = "Not able to remove the child" }));
                }
            }
            //returns a bad request if logged in user cannot delete the child passed in
            catch
            {
                return(BadRequest(new { error = "This user is not authorized to delete this child or the child record does not exist" }));
            }
        }