Example #1
0
        public async Task <IActionResult> AddEventAsync(int rewardId, int eventId)
        {
            var reward = _rewardService.GetReward(rewardId);

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

            var authResult = await _authorizationService.AuthorizeAsync(User, reward, Policy.CanAddQualifyingEvent);

            if (!authResult.Succeeded)
            {
                return(Forbid());
            }

            var rewardEvent = _rewardService.GetRewardEvent(rewardId, eventId);

            if (rewardEvent != null)
            {
                return(BadRequest());
            }

            _rewardService.AddRewardEvent(new RewardEvent
            {
                RewardId = rewardId,
                EventId  = eventId
            });
            _rewardService.SaveChanges();
            _logger.LogInformation("{user} added event {event} to reward {reward}",
                                   User.Identity.Name, eventId, rewardId);

            return(Ok());
        }