public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent : false);

                    using (var context = new RsbContext())
                    {
                        var id = User.Identity.GetUserId();
                        context.Users.Add(new User {
                            UserId = new Guid(user.Id), Name = model.UserName, CurrentScore = 0
                        });
                        context.SaveChanges();
                    }

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 2
0
 public bool GetLeagueLeave(Guid userId, Guid leagueId)
 {
     using (var context = new RsbContext())
     {
         var league = context.Leagues.Single(o => o.Id == leagueId);
         league.Users.RemoveAll(o => o.UserId == userId);
         context.SaveChanges();
         return(true);
     }
 }
Exemplo n.º 3
0
        public ActionResult Contestants()
        {
            var viewModel = new BakerViewModel();


            ViewBag.Message = "The GBBO Contestants 2015";
            using (var context = new RsbContext())
            {
                viewModel.Bakers = context.Bakers.ToList();
                viewModel.Weeks  = context.Weeks.ToList();
            }

            return(View(viewModel));
        }
Exemplo n.º 4
0
 public bool GetLeagueJoin(Guid userId, string code)
 {
     using (var context = new RsbContext())
     {
         var league = context.Leagues.SingleOrDefault(o => o.Code == code);
         if (league == null)
         {
             return(false);
         }
         league.Users.Add(context.Users.Single(o => o.UserId == userId));
         context.SaveChanges();
         return(true);
     }
 }
Exemplo n.º 5
0
 public bool GetLeagueCreate(Guid userId, string name)
 {
     using (var context = new RsbContext())
     {
         var code   = new Random().Next(0, 9999).ToString(); // TODO: Generate code util
         var league = new League {
             Code = code, Name = name, Users = new List <User>(), Id = Guid.NewGuid()
         };
         league.Users.Add(context.Users.Single(o => o.UserId == userId));
         context.Leagues.Add(league);
         context.SaveChanges();
         return(true);
     }
 }
Exemplo n.º 6
0
        public bool GetStar(Guid weekId, Guid bakerId)
        {
            using (var context = new RsbContext())
            {
                var week      = context.Weeks.SingleOrDefault(o => o.Id == weekId);
                var starBaker = context.Bakers.Single(o => o.Id == bakerId);

                if (week != null && starBaker != null)
                {
                    week.StarBaker = starBaker;
                    context.SaveChanges();
                    return(true);
                }
                return(false);
            }
        }
Exemplo n.º 7
0
        private void ScoreWeek(RsbContext context, Week week)
        {
            var users = context.Users.Include("Predictions");

            foreach (var user in users)
            {
                var currentScore   = user.CurrentScore;
                var newScore       = user.CurrentScore;
                var weekPrediction = user.Predictions.SingleOrDefault(o => o.WeekId == week.Id);
                if (weekPrediction == null)
                {
                    break;
                }

                if (week.ExitBaker.Id == weekPrediction.ExitBakerId)
                {
                    newScore++;
                }
                if (week.ExitBaker.Id == weekPrediction.StarBakerId)
                {
                    newScore--;
                }
                if (week.StarBaker.Id == weekPrediction.StarBakerId)
                {
                    newScore++;
                }
                if (week.StarBaker.Id == weekPrediction.ExitBakerId)
                {
                    newScore--;
                }

                if (user.PreviousScores == null)
                {
                    user.PreviousScores = new List <int>();
                }

                user.PreviousScores.Add(currentScore);
                user.CurrentScore = newScore;
            }

            week.Scored = true;
            context.SaveChanges();
        }
Exemplo n.º 8
0
        public bool GetRunScoring()
        {
            bool returnValue;

            try
            {
                using (var context = new RsbContext())
                {
                    var week = context.Weeks.Include("StarBaker").Include("ExitBaker").OrderBy(o => o.Order).First(o => o.Scored == false && o.EndDateTime < DateTime.UtcNow);
                    ScoreWeek(context, week);

                    returnValue = true;
                }
            }
            catch
            {
                returnValue = false;
            }
            return(returnValue);
        }
Exemplo n.º 9
0
        public bool RunAllScoring()
        {
            try
            {
                List <Week> weeks;
                using (RsbContext context = new RsbContext())
                {
                    weeks = context.Weeks.Include("StarBaker").Include("ExitBaker").OrderBy(o => o.Order).Where(o => o.StarBaker != null && o.ExitBaker != null && !o.Scored).ToList();
                }

                foreach (var week in weeks)
                {
                    var context = new RsbContext();
                    ScoreWeek(context, week);
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemplo n.º 10
0
        public bool GetExit(Guid userId, Guid weekId, Guid bakerId)
        {
            using (var context = new RsbContext())
            {
                var week = context.Weeks.SingleOrDefault(o => o.Id == weekId);

                if (week == null || week.EndDateTime < DateTime.UtcNow)
                {
                    return(false);
                }

                var user = context.Users.Include("Predictions").Single(o => o.UserId == userId);
                var pred = user.Predictions.SingleOrDefault(o => o.WeekId == weekId);

                if (pred == null)
                {
                    pred = new Prediction
                    {
                        ExitBakerId  = bakerId,
                        PredictionId = Guid.NewGuid(),
                        WeekId       = weekId
                    };
                    user.Predictions.Add(pred);
                }
                else
                {
                    pred.ExitBakerId = bakerId;

                    if (pred.ExitBakerId == pred.StarBakerId)
                    {
                        pred.StarBakerId = Guid.Empty;
                    }
                }
                context.SaveChanges();
                return(true);
            }
        }
Exemplo n.º 11
0
        public ActionResult Home()
        {
            ViewBag.Message = "Ready, Set, Bake!";

            var userId    = GetId();
            var viewModel = new ViewModel {
                UserId = userId.Value
            };

            using (var context = new RsbContext())
            {
                viewModel.Leagues = context.Leagues.Include("Users").Where(o => o.Users.Any(x => x.UserId == userId)).ToList();
                viewModel.Bakers  = context.Bakers.ToList();

                var pastWeeks = context.Weeks.Include("StarBaker").Include("ExitBaker").ToList();
                var lastWeek  = pastWeeks.OrderByDescending(o => o.Order).Where(o => o.StarBaker != null && o.ExitBaker != null).FirstOrDefault(o => o.EndDateTime < DateTime.UtcNow);
                viewModel.LastWeekStarBaker = lastWeek.StarBaker?.Name;
                viewModel.LastWeekExitBaker = lastWeek.ExitBaker?.Name;

                var weeks = context.Weeks.ToList();

                viewModel.Weeks = new List <WeekViewModel>();
                int iterator = 1;
                foreach (var week in weeks.OrderBy(o => o.Order))
                {
                    var weekVM = new WeekViewModel {
                        Week = week
                    };
                    weekVM.ActualStarBaker = week.StarBaker != null ? week.StarBaker.Id : (Guid?)null;
                    weekVM.ActualExitBaker = week.ExitBaker != null ? week.ExitBaker.Id : (Guid?)null;
                    weekVM.Order           = iterator;

                    viewModel.Weeks.Add(weekVM);
                }

                viewModel.Leagues.Add(new League {
                    Id = Guid.NewGuid(), Users = context.Users.OrderByDescending(o => o.CurrentScore).ToList(), Name = "All Users"
                });

                if (lastWeek.Order == 1)
                {
                    return(View(viewModel));
                }

                var user        = context.Users.Include("Predictions").Single(o => o.UserId == userId);
                var predictions = user.Predictions;

                viewModel.Score = user.CurrentScore;

                if (user.PreviousScores != null)
                {
                    viewModel.PreviousScore = user.PreviousScores.LastOrDefault();
                }

                iterator = 1;
                foreach (var week in viewModel.Weeks.OrderBy(o => o.Order))
                {
                    if (predictions != null && predictions.Count != 0)
                    {
                        var prediction = predictions.SingleOrDefault(o => o.WeekId == week.Week.Id);
                        if (prediction != null)
                        {
                            if (prediction.StarBakerId != null && prediction.StarBakerId != Guid.Empty)
                            {
                                week.PredictedStarBakerId = prediction.StarBakerId;
                            }
                            if (prediction.ExitBakerId != null && prediction.ExitBakerId != Guid.Empty)
                            {
                                week.PredictedExitBakerId = prediction.ExitBakerId;
                            }
                        }
                    }
                }
            }
            return(View(viewModel));
        }