Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Id,BadgeId,UserId,DateofRequest,DateofGrant,IsRejected,RejectCount,RejectReason,IsGranted")] UserBadge userBadge)
        {
            //if (ModelState.IsValid)
            //{
            var Badge = _context.Badges
                        .SingleOrDefault(m => m.Id == userBadge.BadgeId);

            var UserDetails = _context.Users
                              .SingleOrDefault(m => m.Id == userBadge.UserId);

            userBadge.DateofGrant   = DateTime.Now;
            userBadge.DateofGrant   = DateTime.Now;
            userBadge.DateofRequest = DateTime.Now;
            userBadge.IsGranted     = true;
            userBadge.RejectCount   = 0;
            userBadge.IsRejected    = false;
            //userBadge.UserId = id;
            _context.Add(userBadge);

            EmailContent content = _context.EmailContents.Where(m => m.Id == Badge.EmailContentId).SingleOrDefault();
            await _emailSender.SendEmailAsync(UserDetails.Email, content.ArSubject, content.ArContent);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));

            //ViewData["BadgeId"] = new SelectList(_context.Badges, "Id", "ArBadgeName", userBadge.BadgeId);
            ////ViewData["UserId"] = new SelectList(_context.Users, "Id", "ArName", userBadge.UserId);
            //ViewData["UserId"] = new SelectList(_userManager.Users.Where(u => u.Id == userBadge.UserId), "Id", "ArName");
            //return View(userBadge);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,BadgeId,UserId,DateofRequest,DateofGrant,IsRejected,RejectCount,RejectReason,IsGranted")] UserBadge userBadge)
        {
            if (id != userBadge.Id)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                try
                {
                    var Badge = _context.Badges
                                .SingleOrDefault(m => m.Id == userBadge.BadgeId);
                    var UserDetails = _context.Users
                                      .SingleOrDefault(m => m.Id == userBadge.UserId);
                    if (userBadge.IsGranted == true)
                    {
                        userBadge.DateofGrant = DateTime.Now;
                        //  EmailContent content = _context.EmailContents.Where(m => m.Id == 1).SingleOrDefault();
                        EmailContent content = _context.EmailContents.Where(m => m.Id == Badge.EmailContentId).SingleOrDefault();
                        await _emailSender.SendEmailAsync(UserDetails.Email, content.ArSubject, content.ArContent);
                    }


                    if (userBadge.IsRejected == true)
                    {
                        userBadge.RejectCount++;
                        if (userBadge.BadgeId == 1)
                        {
                            EmailContent content = _context.EmailContents.Where(m => m.UniqueName.ToString() == "979f0462-f293-4223-909e-6b5c51bb91f5").SingleOrDefault();
                            await _emailSender.SendEmailAsync(UserDetails.Email, content.ArSubject, content.ArContent);
                        }
                        else
                        {
                            EmailContent content = _context.EmailContents.Where(m => m.UniqueName.ToString() == "21b9d8a1-1833-49a7-9d7e-c734c8d59529").SingleOrDefault();
                            //var Badge = _context.Badges.SingleOrDefault(a => a.Id == userBadge.BadgeId);
                            string BadgeName = "�� ��� �������� ��� ����� " + Badge.ArBadgeName;
                            await _emailSender.SendEmailAsync(UserDetails.Email, BadgeName, content.ArContent + userBadge.RejectReason);
                        }
                    }
                    _context.Update(userBadge);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserBadgeExists(userBadge.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Unapproved"));
            }
            //ViewData["BadgeId"] = new SelectList(_context.Badges, "Id", "ArBadgeName", userBadge.BadgeId);
            //ViewData["UserId"] = new SelectList(_context.Users, "Id", "ArName", userBadge.UserId);
            //ViewData["UserId"] = new SelectList(_userManager.Users.Where(u => u.Id == _userManager.GetUserId(User)), "Id", "ArName");
            return(View(userBadge));
        }
Exemplo n.º 3
0
        // PUT api/UserBadge/5
        public IHttpActionResult PutUserBadge(long id, UserBadge userBadge)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != userBadge.Id)
            {
                return(BadRequest());
            }

            try
            {
                _userBadgeService.UpdateUserBadge(userBadge);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserBadgeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 4
0
        public static void GeneraterNewBadges(Guid userId)
        {
            using (var dbContext = new ActivityTrackerDbContext())
            {
                var badges     = dbContext.Badges.ToList();
                var userBadges = dbContext.UserBadges.Where(x => x.UserID == userId).ToList();

                foreach (var badge in badges)
                {
                    if (userBadges.FirstOrDefault(x => x.BadgeID == badge.BadgeID) == null)
                    {
                        if (CheckIfUserWonThisBadge(userId, badge))
                        {
                            var userBadge = new UserBadge();
                            userBadge.UserID  = userId;
                            userBadge.BadgeID = badge.BadgeID;
                            userBadge.Date    = DateTime.UtcNow;

                            dbContext.UserBadges.Add(userBadge);
                        }
                    }
                }
                dbContext.SaveChanges();
            }
        }
Exemplo n.º 5
0
        public bool AwardBadgeByType(Guid multiId, string awardType, Guid userId)
        {
            // Check for the params we need to award the badge
            if (String.IsNullOrEmpty(awardType) || multiId == null || userId == null)
            {
                return(false);
            }

            // Got them? Great, now that's see what we're awarding the badge for
            Badge Award = new Badge();

            switch (awardType.ToLower())
            {
            case "challenge":
                // Look for a quiz / challenge ID
                Award = WmData.Badges.FirstOrDefault(b => b.ChallengeId == multiId);
                break;

            case "lesson":
                // Look for a Lesson ID
                Award = WmData.Badges.FirstOrDefault(b => b.LessonId == multiId);
                break;

            case "journey":
                // Look for a Journey ID
                Award = WmData.Badges.FirstOrDefault(b => b.JourneyId == multiId);
                break;

            default:
                break;
            }

            // Did we get a badge there?
            if (Award == null)
            {
                return(false);
            }

            // We did? Awesome, lets give it to the user
            try
            {
                UserBadge Awarded = new UserBadge();
                Awarded.UserBadgeId = Guid.NewGuid();
                Awarded.BadgeId     = Award.BadgeId;
                Awarded.UserId      = userId;
                WmData.UserBadges.Add(Awarded);
                WmData.SaveChanges();
            }
            // Uh-oh - something went wrong, we should let our other function know!
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);

                throw;
            }

            // Cool, all added!
            return(true);
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Put(int id, [FromBody] UserBadge userBadge)
        {
            if (id != userBadge.UserID)
            {
                return(BadRequest());
            }

            _context.Entry(userBadge).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserBadgeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 7
0
        public bool SetBadgeOccurence(Guid userId, BadgeType type, Guid referenceId)
        {
            IQueryable <UserBadge> items = userBadgeRepository.Get(x => x.UserId == userId && x.Badge == type);

            UserBadge model = items.FirstOrDefault();

            if (model == null)
            {
                model = new UserBadge
                {
                    UserId = userId,
                    Badge  = type
                };
            }

            if (model.References.Any(x => x == referenceId))
            {
                return(false);
            }
            else
            {
                model.References.Add(referenceId);

                if (model.Id == Guid.Empty)
                {
                    userBadgeRepository.Add(model);
                }
                else
                {
                    userBadgeRepository.Update(model);
                }

                return(true);
            }
        }
Exemplo n.º 8
0
        public void CheckActivityBadgeProgress(string userId, Activity activity)
        {
            var badgeIds = GetUserBadges(userId).Select(b => b.Id).ToList();

            if (activity.Type == ActivityType.Walking)
            {
                if (!badgeIds.Contains(2) && activity.Steps > 100)
                {
                    var userBadge = new UserBadge {
                        UserId = userId, BadgeId = 2
                    };
                    _userBadgeRepository.Create(userBadge);
                }
                if (!badgeIds.Contains(7) && activity.Distance > 10)
                {
                    var userBadge = new UserBadge {
                        UserId = userId, BadgeId = 7
                    };
                    _userBadgeRepository.Create(userBadge);
                }
            }
            else if (activity.Type == ActivityType.Running)
            {
                if (!badgeIds.Contains(3) && activity.Distance > 26)
                {
                    var userBadge = new UserBadge {
                        UserId = userId, BadgeId = 3
                    };
                    _userBadgeRepository.Create(userBadge);
                }
            }
            else if (activity.Type == ActivityType.Biking)
            {
                if (!badgeIds.Contains(5) && activity.Distance > 10)
                {
                    var userBadge = new UserBadge {
                        UserId = userId, BadgeId = 5
                    };
                    _userBadgeRepository.Create(userBadge);
                }
                else if (!badgeIds.Contains(6) && activity.Distance > 100)
                {
                    var userBadge = new UserBadge {
                        UserId = userId, BadgeId = 6
                    };
                    _userBadgeRepository.Create(userBadge);
                }
            }
            else if (activity.Type == ActivityType.Jogging)
            {
                if (!badgeIds.Contains(8) && activity.Distance > 10)
                {
                    var userBadge = new UserBadge {
                        UserId = userId, BadgeId = 8
                    };
                    _userBadgeRepository.Create(userBadge);
                }
            }
        }
Exemplo n.º 9
0
        public UserBadge GetUserBadge(long badgeId, string userId)
        {
            UserBadge ub = db.UserBadges
                           .Where(u => u.BadgeID == badgeId && u.UserID == userId)
                           .FirstOrDefault();

            return(ub);
        }
Exemplo n.º 10
0
        public UserBadge GetUserBadge(long ID)
        {
            UserBadge b = db.UserBadges
                          .Where(a => a.ID == ID)
                          .FirstOrDefault();

            return(b);
        }
        public IHttpActionResult AddScore(ItemScore score)
        {
            Question question   = db.Questions.Find(score.id);
            string   userId     = User.Identity.GetUserId();
            var      scoreitems = question.ScoreItems.Where(x => x.UserID == userId).ToList();

            if (scoreitems.Count == 0)
            {
                int addedScore = Math.Sign(score.score);
                question.ScoreItems.Add(new ScoreItem()
                {
                    Added  = DateTime.Now,
                    Score  = addedScore,
                    UserID = userId,
                    ItemID = question.ID,
                });

                question.Score += addedScore;
            }
            else
            {
                int addedScore = Math.Sign(score.score);
                var scoreItem  = scoreitems.First();
                if (scoreItem.Score != addedScore)
                {
                    question.Score           -= scoreItem.Score;
                    scoreItem.Score           = addedScore;
                    question.Score           += addedScore;
                    db.Entry(scoreItem).State = EntityState.Modified;
                }
                else
                {
                    question.Score           -= scoreItem.Score;
                    db.Entry(scoreItem).State = EntityState.Deleted;
                }
            }
            db.Entry(question).State = EntityState.Modified;
            db.SaveChanges();

            int currentScore       = question.Score;
            int currentBadgeRarity = PointsUtils.getCurrentScoreBadgeRarity(currentScore);

            UserLevel userLevel = question.AspNetUser.UserLevels.Single(x => x.CategoryID == question.CategoryID);
            UserBadge userBadge = userLevel.UserBadges.SingleOrDefault(x => x.ItemID == question.ID);

            if (userBadge == null)
            {
                PointsUtils.giveBadge(question.AspNetUser.Id, question.CategoryID, "score", currentBadgeRarity, question.ID);
            }
            else if (userBadge.Badge.Rarity < currentBadgeRarity)
            {
                db.UserBadges.Remove(userBadge);
                PointsUtils.giveBadge(question.AspNetUser.Id, question.CategoryID, "score", currentBadgeRarity, question.ID);
            }

            db.SaveChanges();
            return(Ok(new QuestionDTO(question)));
        }
Exemplo n.º 12
0
        public static bool EqualValues(this UserBadgeDto ubDto, UserBadge ub)
        {
            bool equal = ubDto.BadgeID == ub.BadgeID &&
                         ubDto.DateCompleted == ub.DateCompleted &&
                         ubDto.ID == ub.ID &&
                         ubDto.UserID == ub.UserID;

            return(equal);
        }
Exemplo n.º 13
0
 public static UserBadgeDTO ToDto(this UserBadge badge)
 {
     return(new UserBadgeDTO
     {
         Id = badge.Id,
         Name = badge.Name,
         Type = badge.Type
     });
 }
Exemplo n.º 14
0
        public async Task CreateUserBadge()
        {
            UserBadge b = new UserBadge {
                Id = 5, UserId = "qwerty", BadgeId = 2
            };
            long bID = await _userBadgeService.CreateUserBadge(b);

            Assert.AreEqual(b.Id, bID);
        }
Exemplo n.º 15
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            UserBadge userBadge = await this.Context.UserBadges.FindAsync(id);

            this.Context.UserBadges.Remove(userBadge);
            await this.Context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Exemplo n.º 16
0
 public static UserBadgeDto FromUserBadge(UserBadge ub)
 {
     return(new UserBadgeDto()
     {
         BadgeID = ub.BadgeID,
         DateCompleted = ub.DateCompleted,
         ID = ub.ID,
         UserID = ub.UserID,
     });
 }
Exemplo n.º 17
0
        private void CreateBadge(string userId, int badgeid, ApplicationDbContext db)
        {
            UserBadge userBadge = new UserBadge();

            userBadge.AspNetUserId = userId;
            userBadge.BadgeId      = badgeid;

            db.UserBadges.Add(userBadge);
            db.SaveChanges();
        }
Exemplo n.º 18
0
        public async Task <IHttpActionResult> PostUserBadge(UserBadge userBadge)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            userBadge.Id = await _userBadgeService.CreateUserBadge(userBadge);

            return(CreatedAtRoute("DefaultApi", new { id = userBadge.Id }, userBadge));
        }
Exemplo n.º 19
0
        public async Task <IHttpActionResult> GetUserBadge(long id)
        {
            UserBadge userBadge = await _userBadgeService.FindUserBadge(id);

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

            return(Ok(userBadge));
        }
Exemplo n.º 20
0
        public async Task <long> CreateUserBadge(UserBadge userBadge)
        {
            _userBadgeRepository.Create(userBadge);
            await _unit.Commit();

            await _feedEventService.GenerateFeedEvent(userBadge);

            await _unit.Commit();

            return(userBadge.Id);
        }
Exemplo n.º 21
0
        public IHttpActionResult PostUserBadge(UserBadge ub)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ubsvc.CreateUserBadge(ub);
            uow.Save();

            return(CreatedAtRoute("DefaultApi", new { id = ub.ID }, UserBadgeDto.FromUserBadge(ub)));
        }
Exemplo n.º 22
0
        public async Task <ActionResult> Edit([Bind(Include = "UserBadgeId,Created,UserId,BadgeId")] UserBadge userBadge)
        {
            if (ModelState.IsValid)
            {
                this.Context.Entry(userBadge).State = EntityState.Modified;
                await this.Context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.BadgeId = new SelectList(this.Context.Badges, "BadgeId", "Name", userBadge.BadgeId);
            ViewBag.UserId  = new SelectList(this.Context.Users, "Id", "UserName", userBadge.UserId);
            return(View(userBadge));
        }
Exemplo n.º 23
0
        // GET: UserBadges/Details/5
        public async Task <ActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserBadge userBadge = await this.Context.UserBadges.FindAsync(id);

            if (userBadge == null)
            {
                return(HttpNotFound());
            }
            return(View(userBadge));
        }
Exemplo n.º 24
0
        public void CheckGroupBadgeProgress(string userId)
        {
            var badges          = GetUserBadges(userId);
            var has10GroupBadge = badges.Select(b => b.Id).Contains(9);
            var memberships     = _userRepository.GetById(userId).GroupMembership;

            if (!has10GroupBadge && memberships.Count >= 10)
            {
                var userBadge = new UserBadge {
                    UserId = userId, BadgeId = 9
                };
                _userBadgeRepository.Create(userBadge);
            }
        }
Exemplo n.º 25
0
 public ActionResult Index()
 {
     ViewBag.isShow = 0;
     if (LoginUser != null)
     {
         UserBadge userBadge = UserBadgeService.GetEntities(u => u.UserInfoID == LoginUser.UserInfoID && u.BadgeID == 1).FirstOrDefault();
         if (userBadge != null && LoginUser.UserInfoLastTime < userBadge.CreateTime)
         {
             ViewBag.isShow = 1;
         }
         LoginUser.UserInfoLastTime = DateTime.Now;
         UserInfoService.Update(LoginUser);
         UpdateCaching(LoginUser);
     }
     return(View());
 }
Exemplo n.º 26
0
        // GET: UserBadges/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserBadge userBadge = await this.Context.UserBadges.FindAsync(id);

            if (userBadge == null)
            {
                return(HttpNotFound());
            }
            ViewBag.BadgeId = new SelectList(this.Context.Badges, "BadgeId", "Name", userBadge.BadgeId);
            ViewBag.UserId  = new SelectList(this.Context.Users, "Id", "UserName", userBadge.UserId);
            return(View(userBadge));
        }
Exemplo n.º 27
0
        public ActionResult NameTerritory(NameTerritoryViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            // confirm territory is owned by this player,
            // so users cant hit this action and rename a territory
            var territory = this.Context.Territories.Single(x => x.TerritoryId == this.UserContext.TerritoryId &&
                                                            x.Players.Any(y => y.Id == this.UserContext.Id));

            // todo: profanity check

            this.UserContext.Territory.Name = vm.Name;

            // badge check (yeah, there's a badge for this)
            if (!this.Context.UserBadges.Any(x => x.UserId == this.UserContext.Id &&
                                             x.Badge.BadgeType == Pixel.Common.Data.BadgeTypes.TerritoryNamed))
            {
                var badge = this.Context.Badges.Where(x => x.BadgeType == Pixel.Common.Data.BadgeTypes.TerritoryNamed).FirstOrDefault();
                if (badge != null)
                {
                    this.UserContext.Experience += badge.ExperienceGain;

                    var userBadge = new UserBadge
                    {
                        BadgeId = badge.BadgeId,
                        Created = DateTime.Now,
                        UserId  = this.UserContext.Id,
                    };

                    this.Context.UserBadges.Add(userBadge);

                    var note = CommunicationService.CreateNotification(
                        this.UserContext.Id,
                        "You earned a badge!",
                        $"You've been conferred a new badge for: {badge.Name}. You earned {badge.ExperienceGain} experience!!");

                    this.Context.Notes.Add(note);
                }
            }

            this.Context.SaveChanges();

            return(RedirectToAction("Index"));
        }
Exemplo n.º 28
0
        public static UserModel FindOrCreateByEmailAndLogin(
            string email, string login, string password = null, UserModel referral = null
            )
        {
            password ??= Rand.RandomString();

            var user = UserModel.FindByEmail(email);

            var loginUser = UserModel.FindByLogin(login);

            var baseLogin  = login;
            int postfixNum = 0;

            while (loginUser != null)
            {
                postfixNum++;
                login     = $"{baseLogin}_{postfixNum}";
                loginUser = UserModel.FindByLogin(login);
            }

            user ??= Create(email, login, password);

            UserBadge.Create(user, "Early adopter");

            int tokenRegisterBonus = System.Convert.ToInt32(
                AppConfig.GetConfiguration("user:registration:token_bonus")
                );

            if (tokenRegisterBonus > 0)
            {
                UserBalance.Create(user, CurrencyType.GitComToken, tokenRegisterBonus);
                FundingTransaction.Create(
                    user, user.id, EntityType.User, null, tokenRegisterBonus, CurrencyType.GitComToken
                    );
            }

            if (referral != null)
            {
                UserReferralRepository.Create(user, referral);
            }

            return(user);
        }
Exemplo n.º 29
0
        public async Task <ActionResult <UserBadge> > Post([FromBody] UserBadge userBadge)
        {
            _context.UserBadges.Add(userBadge);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (UserBadgeExists(userBadge.UserID))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
Exemplo n.º 30
0
        private static void AddBadgeToContext(ApplicationDbContext db, ApplicationUser user, Badge badge)
        {
            user.Experience += badge.ExperienceGain;

            // todo: create badge service or something similar for this operation
            // create user badge
            var newBadge = new UserBadge
            {
                BadgeId = badge.BadgeId,
                UserId  = user.Id,
                Created = DateTime.Now,
            };

            db.UserBadges.Add(newBadge);

            // create notification
            var note = CommunicationService.CreateNotification(
                user.Id,
                "You earned a badge!",
                $"You've been conferred a new badge for: {badge.Name}. You earned {badge.ExperienceGain} experience!!");

            db.Notes.Add(note);
        }