Esempio n. 1
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name,ImageURL")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(user));
        }
        public ActionResult Index(HttpPostedFileBase file)
        {
            using (var context = new TippeContext())
            {
                // Verify that the user selected a file
                if (file != null && file.ContentLength > 0)
                {
                    BinaryReader b       = new BinaryReader(file.InputStream);
                    byte[]       binData = b.ReadBytes(Convert.ToInt32(file.InputStream.Length));

                    string result = System.Text.Encoding.UTF8.GetString(binData);

                    var list = JsonConvert.DeserializeObject <List <User> >(result);
                    if (!context.Rounds.Any())
                    {
                        CreateRounds(context);
                    }
                    foreach (var user in list)
                    {
                        //Add the user with the correct user name
                        if (!context.Users.Any(u => u.Name == user.Name))
                        {
                            context.Users.Add(new User
                            {
                                Name = user.Name
                            });
                        }
                        context.SaveChanges();
                        //Add matchpredictions
                        foreach (var matchPrediction in user.MatchPredictions)
                        {
                            //Add matches if they do not exist
                            if (!context.Matches.Any(m => m.Id == matchPrediction.MatchId))
                            {
                                //Possibly add Teams first
                                if (!context.Teams.Any(t => t.Name == matchPrediction.HomeTeam.Name))
                                {
                                    context.Teams.Add(new Team
                                    {
                                        Name = matchPrediction.HomeTeam.Name
                                    });
                                }
                                if (!context.Teams.Any(t => t.Name == matchPrediction.AwayTeam.Name))
                                {
                                    context.Teams.Add(new Team
                                    {
                                        Name = matchPrediction.AwayTeam.Name
                                    });
                                }
                                context.SaveChanges();
                                var hometeam = context.Teams.First(t => t.Name == matchPrediction.HomeTeam.Name);
                                var awayteam = context.Teams.First(t => t.Name == matchPrediction.AwayTeam.Name);

                                context.Matches.Add(new Game
                                {
                                    Id         = matchPrediction.MatchId,
                                    HomeTeamId = hometeam.Id,
                                    AwayTeamId = awayteam.Id
                                });
                                context.SaveChanges();
                            }
                            //add matchprediction if it does not exist
                            if (
                                !context.MatchPredictions.Any(
                                    mp => mp.MatchId == matchPrediction.MatchId && mp.User.Name == user.Name))
                            {
                                var hometeam         = context.Teams.First(t => t.Name == matchPrediction.HomeTeam.Name);
                                var awayteam         = context.Teams.First(t => t.Name == matchPrediction.AwayTeam.Name);
                                var userfromcontext  = context.Users.First(u => u.Name == user.Name);
                                var matchFromContext = context.Matches.First(m => m.Id == matchPrediction.MatchId);
                                var entry            = context.MatchPredictions.Attach(new MatchPrediction
                                {
                                    MatchId    = matchFromContext.Id,
                                    HomeGoals  = matchPrediction.HomeGoals,
                                    AwayGoals  = matchPrediction.AwayGoals,
                                    HomeTeamId = hometeam.Id,
                                    AwayTeamId = awayteam.Id,
                                    UserId     = userfromcontext.Id
                                });
                                context.Entry(entry).State = EntityState.Added;
                                context.SaveChanges();
                            }
                        }
                        //round of 16
                        var roundId = context.Rounds.First(r => r.JsonName == "RoundOf16").Id;
                        if (!context.RoundPredictions.Any(rp => rp.RoundId == roundId && rp.User.Name == user.Name))
                        {
                            //Add roundpredictions
                            var userfromcontext = context.Users.First(u => u.Name == user.Name);
                            var roundPrediction = new RoundPrediction
                            {
                                RoundId = roundId,
                                UserId  = userfromcontext.Id
                            };
                            context.RoundPredictions.Add(roundPrediction);
                            context.SaveChanges();
                            foreach (var team in user.RoundOf16)
                            {
                                var teamFromContext = context.Teams.ToList().First(t => t.Name.Substring(0, 3).ToLower() == team.Name.Substring(0, 3).ToLower());
                                context.RoundPredictionTeams.Add(new RoundPredictionTeam
                                {
                                    Rank = 0,
                                    RoundPredictionId = roundPrediction.Id,
                                    TeamId            = teamFromContext.Id
                                });
                            }
                            context.SaveChanges();
                        }
                        //round of 8
                        roundId = context.Rounds.First(r => r.JsonName == "RoundOf8").Id;
                        if (!context.RoundPredictions.Any(rp => rp.RoundId == roundId && rp.User.Name == user.Name))
                        {
                            //Add roundpredictions
                            var userfromcontext = context.Users.First(u => u.Name == user.Name);
                            var roundPrediction = new RoundPrediction
                            {
                                RoundId = roundId,
                                UserId  = userfromcontext.Id
                            };
                            context.RoundPredictions.Add(roundPrediction);
                            context.SaveChanges();
                            foreach (var team in user.RoundOf8)
                            {
                                var teamFromContext = context.Teams.ToList().First(t => t.Name.Substring(0, 3).ToLower() == team.Name.Substring(0, 3).ToLower());
                                context.RoundPredictionTeams.Add(new RoundPredictionTeam
                                {
                                    Rank = 0,
                                    RoundPredictionId = roundPrediction.Id,
                                    TeamId            = teamFromContext.Id
                                });
                            }
                            context.SaveChanges();
                        }
                        //round of 4
                        roundId = context.Rounds.First(r => r.JsonName == "RoundOf4").Id;
                        if (!context.RoundPredictions.Any(rp => rp.RoundId == roundId && rp.User.Name == user.Name))
                        {
                            //Add roundpredictions
                            var userfromcontext = context.Users.First(u => u.Name == user.Name);
                            var roundPrediction = new RoundPrediction
                            {
                                RoundId = roundId,
                                UserId  = userfromcontext.Id
                            };
                            context.RoundPredictions.Add(roundPrediction);
                            context.SaveChanges();
                            foreach (var team in user.RoundOf4)
                            {
                                var teamFromContext = context.Teams.ToList().First(t => t.Name.Substring(0, 3).ToLower() == team.Name.Substring(0, 3).ToLower());
                                context.RoundPredictionTeams.Add(new RoundPredictionTeam
                                {
                                    Rank = 0,
                                    RoundPredictionId = roundPrediction.Id,
                                    TeamId            = teamFromContext.Id
                                });
                            }
                            context.SaveChanges();
                        }
                        //ranking
                        roundId = context.Rounds.First(r => r.JsonName == "Ranking").Id;
                        if (!context.RoundPredictions.Any(rp => rp.RoundId == roundId && rp.User.Name == user.Name))
                        {
                            //Add roundpredictions
                            var userfromcontext = context.Users.First(u => u.Name == user.Name);
                            var roundPrediction = new RoundPrediction
                            {
                                RoundId = roundId,
                                UserId  = userfromcontext.Id
                            };
                            context.RoundPredictions.Add(roundPrediction);
                            context.SaveChanges();
                            var i = 1;
                            foreach (var team in user.Ranking)
                            {
                                var teamFromContext = context.Teams.ToList().First(t => t.Name.Substring(0, 3).ToLower() == team.Name.Substring(0, 3).ToLower());
                                context.RoundPredictionTeams.Add(new RoundPredictionTeam
                                {
                                    Rank = i,
                                    RoundPredictionId = roundPrediction.Id,
                                    TeamId            = teamFromContext.Id
                                });
                                i++;
                            }
                            context.SaveChanges();
                        }
                    }
                }
            }
            // redirect back to the index action to show the form once again
            return(RedirectToAction("Index"));
        }