Beispiel #1
0
 public IHttpActionResult GetTeams(int tournamentId, int matchId)
 {
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
         {
             List <Team> allTeams = DatabaseAccessModel.GetTeamListByMatchId(matchId).ToList();
             if (allTeams.Any())
             {
                 return(Content(HttpStatusCode.OK, allTeams));
             }
             return(Content(HttpStatusCode.NotFound, new Error
             {
                 Code = HttpStatusCode.NotFound.ToString(),
                 Message = "Teams for specific tournament and match not found."
             }));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "Match with id " + matchId + " not found in tournament match list."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #2
0
        public IHttpActionResult Authenticate(User user)
        {
            var loginRequest = new User
            {
                Username = user.Username,
                Password = user.Password
            };

            User databaseUser;

            if (user != null)
            {
                databaseUser = DatabaseAccessModel.GetUserByUsername(loginRequest.Username);

                var isUsernamePasswordValid = SecurePasswordHasher.Verify(loginRequest.Password, databaseUser.Password);

                if (isUsernamePasswordValid)
                {
                    //loginRequest.id = databaseUser.id;
                    loginRequest.Role = databaseUser.Role;
                    //var token = CreateToken(loginRequest.Username);
                    var token = CreateToken(loginRequest.Role, loginRequest.Username);
                    return(Ok(token));
                }
            }
            return(Unauthorized());
        }
Beispiel #3
0
 public IHttpActionResult DeleteUser(int id)
 {
     if (DatabaseAccessModel.GetUserByUsername(Thread.CurrentPrincipal.Identity.Name).id != id && !Thread.CurrentPrincipal.IsInRole(UserRoles.Admin))
     {
         return(Content(HttpStatusCode.Forbidden, new Error
         {
             Message = "Your id doesn't match with this user.",
             Code = HttpStatusCode.Forbidden.ToString()
         }));
     }
     if (DatabaseAccessModel.CheckUserExists(id))
     {
         try
         {
             if (DatabaseAccessModel.DeleteUser(id))
             {
                 return(Ok());
             }
         }
         catch (MySql.Data.MySqlClient.MySqlException ex)
         {
             return(Content(HttpStatusCode.NotAcceptable, ex.Message));
         }
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Message = "User with id " + id + " not found.",
         Code = HttpStatusCode.NotFound.ToString()
     }));
 }
Beispiel #4
0
 public IHttpActionResult GetMatchById(int tournamentId, int matchId)
 {
     //select from match sujungto su tournament kur match>fk_tourId = tourId
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
         {
             Match match = DatabaseAccessModel.GetMatchDataListFromSqlbyId(matchId);
             if (match.id != null)
             {
                 return(Content(HttpStatusCode.OK, match));
             }
             return(Content(HttpStatusCode.NotFound, new Error
             {
                 Code = HttpStatusCode.NotFound.ToString(),
                 Message = "Matches for specified tournament with id " + tournamentId + " not found."
             }));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "No match found with id " + matchId + " in tournament with id " + tournamentId + "."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #5
0
 public IHttpActionResult RegisterUser(int tournamentId, Match match)
 {
     //check if tournament by tournamentID exists and create a match with fk_tour = tourId
     if (!ModelState.IsValid)
     {
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid Data",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.AddNewMatchToDatabaseByTournament(match, tournamentId))
         {
             return(Ok(match));
         }
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid team data/could not insert.",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #6
0
 public IHttpActionResult DeleteUser(int tournamentId, int matchId)
 {
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
         {
             try
             {
                 if (DatabaseAccessModel.DeleteMatchFromTournament(matchId))
                 {
                     return(Ok());
                 }
             }
             catch (MySql.Data.MySqlClient.MySqlException ex)
             {
                 return(Content(HttpStatusCode.NotAcceptable, ex.Message));
             }
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "Match with id " + matchId + " not found in tournament match list."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #7
0
        public IHttpActionResult GetUserById(int id)
        {
            User user = DatabaseAccessModel.GetUserDataListFromSqlById(id);

            if (user.id != null)
            {
                return(Ok(user));
            }
            return(Content(HttpStatusCode.NotFound, new Error
            {
                Message = "User with id " + id + " not found.",
                Code = HttpStatusCode.NotFound.ToString()
            }));
        }
Beispiel #8
0
        public IHttpActionResult GetUsers()
        {
            List <User> allUsers = DatabaseAccessModel.GetUserDataListFromSql().ToList();

            if (allUsers.Any())
            {
                return(Content(HttpStatusCode.OK, allUsers));
            }
            return(Content(HttpStatusCode.NotFound, new Error
            {
                Message = "Users not found.",
                Code = HttpStatusCode.NotFound.ToString()
            }));
        }
Beispiel #9
0
        public IHttpActionResult GetTournaments()
        {
            List <Tournament> allTournaments = DatabaseAccessModel.GetTournamentListFromSql().ToList();

            if (allTournaments.Any())
            {
                return(Content(HttpStatusCode.OK, allTournaments));
            }
            return(Content(HttpStatusCode.NotFound, new Error
            {
                Message = "Tournaments not found.",
                Code = HttpStatusCode.NotFound.ToString()
            }));
        }
Beispiel #10
0
        public IHttpActionResult GetTournamentById(int id)
        {
            Tournament tournament = DatabaseAccessModel.GetTournamentListFromSqlbyId(id);

            if (tournament.id != null)
            {
                return(Ok(tournament));
            }
            return(Content(HttpStatusCode.NotFound, new Error
            {
                Message = "Tournament with id " + id + " not found.",
                Code = HttpStatusCode.NotFound.ToString()
            }));
        }
Beispiel #11
0
 public IHttpActionResult DeleteTournament(int tournamentId)
 {
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         try
         {
             if (DatabaseAccessModel.DeleteTournament(tournamentId))
             {
                 return(Ok());
             }
         }
         catch (MySql.Data.MySqlClient.MySqlException ex)
         {
             return(Content(HttpStatusCode.NotAcceptable, ex.Message));
         }
     }
     return(Ok());
 }
Beispiel #12
0
 public IHttpActionResult PutUser(int tournamentId, int matchId, int teamId, Team team)
 {
     //if (!ModelState.IsValid)
     //{
     //    return Content(HttpStatusCode.BadRequest, new Error
     //    {
     //        Message = "Invalid Data",
     //        Code = HttpStatusCode.BadRequest.ToString()
     //    });
     //}
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
         {
             if (DatabaseAccessModel.CheckTeamInMatchExists(matchId, teamId))
             {
                 if (DatabaseAccessModel.UpdateTeamToDatabase(teamId, team))
                 {
                     return(Ok());
                 }
                 return(Content(HttpStatusCode.BadRequest, new Error
                 {
                     Code = HttpStatusCode.NotFound.ToString(),
                     Message = "Invalid team data/could not update."
                 }));
             }
             return(Content(HttpStatusCode.NotFound, new Error
             {
                 Code = HttpStatusCode.NotFound.ToString(),
                 Message = "Team with id " + teamId + " not found in match " + matchId + " team list."
             }));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "Match with id " + matchId + " not found in tournament match list."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #13
0
 public IHttpActionResult UserTeamCreate(int userId, Team team)
 {
     if (DatabaseAccessModel.CheckUserExists(userId))
     {
         if (DatabaseAccessModel.AddTeamToDatabaseByUser(team, userId))
         {
             return(Ok(team));
         }
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Code = HttpStatusCode.BadRequest.ToString(),
             Message = "Invalid team data/could not insert."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "User with id " + userId + " not found."
     }));
 }
Beispiel #14
0
 public IHttpActionResult putTournament(Tournament tournament, int id)
 {
     if (DatabaseAccessModel.CheckTournamentExists(id))
     {
         if (DatabaseAccessModel.UpdateTournamentToDatabase(id, tournament))
         {
             return(Ok());
         }
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid tournament data/could not update.",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Message = "Tournament with id  " + id + " not found.",
         Code = HttpStatusCode.NotFound.ToString()
     }));
 }
Beispiel #15
0
 public IHttpActionResult AddTournament(Tournament tournament)
 {
     if (!ModelState.IsValid)
     {
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid data.",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     if (DatabaseAccessModel.AddTournamentToDatabase(tournament))
     {
         return(Ok(tournament));
     }
     return(Content(HttpStatusCode.BadRequest, new Error
     {
         Message = "Invalid tournament data/could not insert.",
         Code = HttpStatusCode.BadRequest.ToString()
     }));
 }
Beispiel #16
0
 public IHttpActionResult getUserTeam(int userId)
 {
     if (DatabaseAccessModel.CheckUserExists(userId))
     {
         Team userTeam = DatabaseAccessModel.GetTeamByOwner(userId);
         if (userTeam.id != null)
         {
             return(Ok(userTeam));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "No team ownership of user with id " + userId + " found."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "User with id " + userId + " not found."
     }));
 }
Beispiel #17
0
 public IHttpActionResult RegisterUser(User user)
 {
     if (!ModelState.IsValid)
     {
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid data.",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     user.Password = SecurePasswordHasher.Hash(user.Password);
     if (DatabaseAccessModel.AddUserToDatabase(user))
     {
         return(Ok(User));
     }
     return(Content(HttpStatusCode.BadRequest, new Error
     {
         Message = "Invalid user data/could not insert.",
         Code = HttpStatusCode.BadRequest.ToString()
     }));
 }
Beispiel #18
0
 public IHttpActionResult GetMatchesByTournamentId(int tournamentId)
 {
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         List <Match> allMatches = DatabaseAccessModel.GetMatchListByTournamentId(tournamentId).ToList();
         if (allMatches.Any())
         {
             return(Content(HttpStatusCode.OK, allMatches));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "Matches for tournament with id " + tournamentId + " not found."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #19
0
        public IHttpActionResult PutMatch(int tournamentId, int matchId, Match match)
        {
            //check if tournament with id exists
            //check if match with id exists
            //update match by id

            //if (!ModelState.IsValid)
            //{
            //    return Content(HttpStatusCode.BadRequest, new Error
            //    {
            //        Message = "Invalid Data",
            //        Code = HttpStatusCode.BadRequest.ToString()
            //    });
            //}
            if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
            {
                if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
                {
                    if (DatabaseAccessModel.UpdateMatchToDatabase(matchId, match))
                    {
                        return(Ok());
                    }
                    return(Content(HttpStatusCode.BadRequest, new Error
                    {
                        Code = HttpStatusCode.NotFound.ToString(),
                        Message = "Invalid team data/could not update."
                    }));
                }
                return(Content(HttpStatusCode.NotFound, new Error
                {
                    Code = HttpStatusCode.NotFound.ToString(),
                    Message = "Match with id " + matchId + " not found in tournament match list."
                }));
            }
            return(Content(HttpStatusCode.NotFound, new Error
            {
                Code = HttpStatusCode.NotFound.ToString(),
                Message = "Tournament with id " + tournamentId + " not found."
            }));
        }
Beispiel #20
0
 public IHttpActionResult PutUser(int id, User user)
 {
     //var a = Thread.CurrentPrincipal.Identity.Name;
     if (DatabaseAccessModel.GetUserByUsername(Thread.CurrentPrincipal.Identity.Name).id != id && !Thread.CurrentPrincipal.IsInRole(UserRoles.Admin))
     {
         return(Content(HttpStatusCode.Forbidden, new Error
         {
             Message = "Your id doesn't match with this user.",
             Code = HttpStatusCode.Forbidden.ToString()
         }));
     }
     if (!ModelState.IsValid)
     {
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid data.",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     if (DatabaseAccessModel.CheckUserExists(id))
     {
         //user.Password = SecurePasswordHasher.Hash(user.Password);
         if (DatabaseAccessModel.UpdateUserToDatabase(user, id))
         {
             return(Ok());
         }
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid user data/could not update.",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Message = "User with id " + id + " not found.",
         Code = HttpStatusCode.NotFound.ToString()
     }));
 }
Beispiel #21
0
 public IHttpActionResult AddUserToTeam(int userId, int teamId)
 {
     //teamid owner - principal user id
     if (DatabaseAccessModel.GetUserByUsername(Thread.CurrentPrincipal.Identity.Name).id != (DatabaseAccessModel.GetTeamById(teamId)).ownerId && !Thread.CurrentPrincipal.IsInRole(UserRoles.Admin))
     {
         return(Content(HttpStatusCode.Forbidden, new Error
         {
             Message = "Your id doesn't match with this user.",
             Code = HttpStatusCode.Forbidden.ToString()
         }));
     }
     if (DatabaseAccessModel.CheckTeamExists(teamId))
     {
         if (DatabaseAccessModel.CheckUserExists(userId))
         {
             if (DatabaseAccessModel.AddUserToTeam(userId, teamId))
             {
                 return(Ok());
             }
             return(Content(HttpStatusCode.BadRequest, new Error
             {
                 Code = HttpStatusCode.BadRequest.ToString(),
                 Message = "Bad data/could not update."
             }));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "User with id " + userId + " not found."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Team with id " + teamId + " not found."
     }));
 }
Beispiel #22
0
 public IHttpActionResult GetTeamById(int tournamentId, int matchId, int teamId)
 {
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
         {
             if (DatabaseAccessModel.CheckTeamInMatchExists(matchId, teamId))
             {
                 Team team = DatabaseAccessModel.GetTeamDataListFromSqlbyId(teamId);
                 if (team.id != null)
                 {
                     return(Content(HttpStatusCode.OK, team));
                 }
                 return(Content(HttpStatusCode.NotFound, new Error
                 {
                     Code = HttpStatusCode.NotFound.ToString(),
                     Message = "Teams for specific tournament and match not found."
                 }));
             }
             return(Content(HttpStatusCode.NotFound, new Error
             {
                 Code = HttpStatusCode.NotFound.ToString(),
                 Message = "Team with id " + teamId + " not found in match list."
             }));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "Match with id " + matchId + " not found in tournament match list."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }
Beispiel #23
0
 public IHttpActionResult AddTeam(int tournamentId, int matchId, Team team)
 {
     if (!ModelState.IsValid)
     {
         return(Content(HttpStatusCode.BadRequest, new Error
         {
             Message = "Invalid Data",
             Code = HttpStatusCode.BadRequest.ToString()
         }));
     }
     if (DatabaseAccessModel.CheckTournamentExists(tournamentId))
     {
         if (DatabaseAccessModel.CheckMatchInTournamentExists(tournamentId, matchId))
         {
             if (DatabaseAccessModel.AddTeamToDatabaseByMatch(team, matchId))
             {
                 return(Ok(team));
             }
             return(Content(HttpStatusCode.BadRequest, new Error
             {
                 Message = "Invalid team data/could not insert.",
                 Code = HttpStatusCode.BadRequest.ToString()
             }));
         }
         return(Content(HttpStatusCode.NotFound, new Error
         {
             Code = HttpStatusCode.NotFound.ToString(),
             Message = "Match with id " + matchId + " not found in tournament match list."
         }));
     }
     return(Content(HttpStatusCode.NotFound, new Error
     {
         Code = HttpStatusCode.NotFound.ToString(),
         Message = "Tournament with id " + tournamentId + " not found."
     }));
 }