예제 #1
0
 public DataStructure DeleteAccount(int id)
 {
     using (var context = new LoLEsportsDbContext())
     {
         using (var dbContextTransaction = context.Database.BeginTransaction())
         {
             try
             {
                 DataStructure data = new DataStructure();
                 Account       acc  = context.Account.Where(i => i.UserId == id).FirstOrDefault();
                 if (acc == null)
                 {
                     data.error   = 1;
                     data.message = "Account does not exist.";
                     return(data);
                 }
                 context.Remove(acc);
                 context.SaveChanges();
                 data.error   = 0;
                 data.message = "Account successfully deleted.";
                 dbContextTransaction.Commit();
                 return(data);
             }
             catch (Exception e)
             {
                 DataStructure data = new DataStructure();
                 data.error   = 1;
                 data.message = e.ToString();
                 dbContextTransaction.Rollback();
                 return(data);
             }
         }
     }
 }
예제 #2
0
 public DataStructure AddAccount(Account account)
 {
     using (var context = new LoLEsportsDbContext())
     {
         using (var dbContextTransaction = context.Database.BeginTransaction())
         {
             try
             {
                 DataStructure data = new DataStructure();
                 Account       acc  = context.Account.Where(i => i.UserName.Equals(account.UserName)).FirstOrDefault();
                 if (acc != null)
                 {
                     data.error   = 1;
                     data.message = "That username is not available.";
                     return(data);
                 }
                 account.LevelAccess = 3;
                 account.ChampionId  = 1;
                 context.Add(account);
                 context.SaveChanges();
                 dbContextTransaction.Commit();
                 data.error = 0;
                 return(data);
             }
             catch (Exception)
             {
                 dbContextTransaction.Rollback();
                 DataStructure data = new DataStructure();
                 data.error   = 1;
                 data.message = "Database could not commit transaction.";
                 return(data);
             }
         }
     }
 }
예제 #3
0
        public PlayerStructure GetPlayer(int PlayerId)
        {
            using (var context = new LoLEsportsDbContext())
            {
                using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        PlayerStructure data   = new PlayerStructure();
                        Player          player = context.Player.Where(i => i.PlayerId == PlayerId).FirstOrDefault();
                        if (player == null)
                        {
                            data.error   = 1;
                            data.message = "Player does not exist in the current database.";
                            return(data);
                        }
                        Person person = context.Person.Where(i => i.PersonId == player.PersonId).FirstOrDefault();
                        if (person == null)
                        {
                            data.error   = 1;
                            data.message = "Player does not have an existing PersonID.";
                            return(data);
                        }
                        data.error      = 0;
                        data.Age        = person.Age;
                        data.Biography  = person.Biography;
                        data.Hometown   = person.Hometown;
                        data.PersonName = person.PersonName;
                        data.PlayerID   = player.PlayerId;
                        data.PlayerIGN  = player.PlayerIgn;
                        data.PlayerRole = player.PlayerRole;
                        data.Quote      = player.Quote;
                        data.Salary     = person.Salary;

                        //Calculate scores
                        int TotalKills   = context.PlayerMatchRef.Where(i => i.PlayerId == player.PlayerId).Sum(x => x.PlayerKills);
                        int TotalAssists = context.PlayerMatchRef.Where(i => i.PlayerId == player.PlayerId).Sum(x => x.PlayerAssists);
                        int TotalScore   = context.PlayerMatchRef.Where(i => i.PlayerId == player.PlayerId).Sum(x => x.PlayerScore);
                        int TotalDeaths  = context.PlayerMatchRef.Where(i => i.PlayerId == player.PlayerId).Sum(x => x.PlayerDeaths);

                        player.TotalKills   = TotalKills;
                        player.TotalAssists = TotalAssists;
                        player.TotalScore   = TotalScore;
                        player.TotalDeaths  = TotalDeaths;
                        context.Update(player);
                        context.SaveChanges();

                        data.TotalAssists = TotalAssists;
                        data.TotalDeaths  = TotalDeaths;
                        data.TotalKills   = TotalKills;
                        data.TotalScore   = TotalScore;
                        data.PlayerImage  = player.PlayerImage;

                        int TeamId = context.CompetitorTeamRef.Where(i => i.PlayerId == PlayerId).Select(x => x.TeamId).FirstOrDefault();
                        data.TeamName  = context.Team.Where(i => i.TeamId == TeamId).Select(x => x.TeamName).FirstOrDefault();
                        data.TeamImage = context.Team.Where(i => i.TeamId == TeamId).Select(x => x.TeamLogo).FirstOrDefault();
                        data.TeamID    = TeamId;

                        dbContextTransaction.Commit();
                        return(data);
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback();
                        PlayerStructure data = new PlayerStructure();
                        data.error   = 1;
                        data.message = "Database could not commit the transaction.";
                        return(data);
                    }
                }
            }
        }