Esempio n. 1
0
        public IEnumerable<string> UpdateBadgeForEmployee(int employeeId)
        {
            var badge =
                _mongoDb.GetCollection<Badge>(CollectionName)
                        .FirstOrDefault(x => x.Ansattnummer == employeeId);
            if (badge == null)
            {
                badge = new Badge
                {
                    Ansattnummer = employeeId, 
                    BadgesForUser = new List<string>()
                };
                _mongoDb.Insert(badge, CollectionName);
            }

            var badgesForUser = badge.BadgesForUser.Select(x => x).ToList();
            var badgesGranted = new List<string>();
            var purchases =
                _mongoDb.GetCollection<Purchase>("Purchases")
                        .Where(x => x.Buyer == employeeId)
                        .ToList();
            foreach (var badgeToGrant in
                AvailableBadges.Where(x => badgesForUser.Contains(x.Key) == false)
                            .Where(badgeToGrant => badgeToGrant.Value.HasBadge(purchases)))
            {
                badge.BadgesForUser.Add(badgeToGrant.Key);
                badgesGranted.Add(badgeToGrant.Key);
            }

            if (badgesGranted.Any())
            {
                _mongoDb.Save(badge, CollectionName);
            }

            return badgesGranted;
        }
 private static void AddFirstBadge(IMongoDb mongoDb, int buyer, string badgeName, string collectionName)
 {
     var badge = new Badge { Ansattnummer = buyer, BadgesForUser = new List<string>() };
     badge.BadgesForUser.Add(badgeName);
     mongoDb.Insert(badge, collectionName); // First badge awarded
 }