示例#1
0
        public override async Task <Trigger> AddSaveAsync(int userId, Trigger trigger)
        {
            var newTrigger = await base.AddSaveAsync(userId, trigger);

            foreach (var badgeId in trigger.BadgeIds)
            {
                var triggerBadge = new GRA.Data.Model.TriggerBadge()
                {
                    BadgeId   = badgeId,
                    TriggerId = newTrigger.Id
                };
                await _context.TriggerBadges.AddAsync(triggerBadge);
            }
            foreach (var challengeId in trigger.ChallengeIds)
            {
                var triggerChallenge = new GRA.Data.Model.TriggerChallenge()
                {
                    ChallengeId = challengeId,
                    TriggerId   = newTrigger.Id
                };
                await _context.TriggerChallenges.AddAsync(triggerChallenge);
            }
            await _context.SaveChangesAsync();

            return(newTrigger);
        }
示例#2
0
        public override async Task <Trigger> UpdateSaveAsync(int userId, Trigger trigger)
        {
            var updatedTrigger = await base.UpdateSaveAsync(userId, trigger);

            // update TriggerBadge list
            var thisTriggerBadges = _context.TriggerBadges.Where(_ => _.TriggerId == trigger.Id);
            var badgesToAdd       = trigger.BadgeIds.Where(_ =>
                                                           !thisTriggerBadges.Select(b => b.BadgeId).Contains(_));
            var badgesToRemove = thisTriggerBadges.Where(_ => !trigger.BadgeIds.Contains(_.BadgeId));

            foreach (var badgeId in badgesToAdd)
            {
                var triggerBadge = new GRA.Data.Model.TriggerBadge()
                {
                    BadgeId   = badgeId,
                    TriggerId = trigger.Id
                };
                await _context.TriggerBadges.AddAsync(triggerBadge);
            }
            _context.TriggerBadges.RemoveRange(badgesToRemove);

            // update TriggerChallenge list
            var thisTriggerChallenges = _context.TriggerChallenges
                                        .Where(_ => _.TriggerId == trigger.Id);
            var challengesToAdd = trigger.ChallengeIds.Where(_ =>
                                                             !thisTriggerChallenges.Select(c => c.ChallengeId).Contains(_));
            var challengesToRemove = thisTriggerChallenges
                                     .Where(_ => !trigger.ChallengeIds.Contains(_.ChallengeId));

            foreach (var challengeId in challengesToAdd)
            {
                var triggerChallenge = new GRA.Data.Model.TriggerChallenge()
                {
                    ChallengeId = challengeId,
                    TriggerId   = trigger.Id
                };
                await _context.TriggerChallenges.AddAsync(triggerChallenge);
            }
            _context.TriggerChallenges.RemoveRange(challengesToRemove);

            await _context.SaveChangesAsync();

            return(updatedTrigger);
        }