Exemple #1
0
        // Gets games from Rawg Database
        public async Task <IGameRootObject> GetGames(string search, HomePage.ErrorHandling errorMessage)
        {
            // Checks connection
            bool connection = _checkConnection.hasConnection(errorMessage);

            if (connection)
            {
                // This is short hand for the below code, this makes it more difficult for me and reduces the amount of If statements greatly
                // Could be shortened even more but it would look much more confusing after removing yet another if statement
                return(!string.IsNullOrEmpty(search) ? await _gameProxy.GetGameBySearch(search, errorMessage) ?? null : await _gameProxy.GetAllGameInfo(errorMessage) ?? null);

                // Original Code

                // If it has something to search for
                //if (!string.IsNullOrEmpty(search))
                //{
                //    // Gets games by search
                //    var games = await _gameProxy.GetGameBySearch(search, errorMessage);
                //    return games ?? null;

                //}
                //else // If their is no input it should grab all games
                //{
                //    // Gets all games
                //    var games = await _gameProxy.GetAllGameInfo(errorMessage);
                //    return games ?? null;
                //}
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        // This method checks if the item they are trying to add is already in the Played table
        // Used to switch buttons from add to remove
        public async Task <bool> IsOnPlayed(HomePage.ErrorHandling errorMessage, Game item, string id)
        {
            bool connection = _checkConnection.hasConnection(errorMessage);

            if (connection)
            {
                var items = await _wishlistPlayedProxy.IsOnT <Played>("Played", id); // Grabs all items associated with a user id

                if (items != null)
                {
                    // Filters through the item to find if there are any items where the game id is already in there
                    var temp = items.Where(e => e.GameID == item.id).FirstOrDefault();
                    if (temp != null)
                    {
                        // Sets the ID so the user can delete the item later on
                        GameCatalogueApp.Pages.DetailedItem.DetailedPage.playedID = temp.Id;
                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                return(false);
            }
        }
        // read a text file from the app's local folder
        public static async Task <string> ReadTextFileAsync(string _filename, HomePage.ErrorHandling errorHandling)
        {
            // declare an empty variable to be filled later
            string result = null;

            try
            {
                // get hold of the file system
                IFolder rootFolder = FileSystem.Current.LocalStorage;

                // create a folder, if one does not exist already
                IFolder folder = await rootFolder.CreateFolderAsync("GameCatalogueStorage", CreationCollisionOption.OpenIfExists);

                // create a file, overwriting any existing file
                IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.OpenIfExists);

                // populate the file with some text
                result = await file.ReadAllTextAsync();
            }
            catch (Exception ex)
            {
                errorHandling(ex.Message);
            }

            // return the contents of the file
            return(result);
        }
        // Gets Games by search

        public async Task <List <Game> > GetGamesBySearch(HomePage.ErrorHandling errorMessage, string search)
        {
            try
            {
                var http = new HttpClient
                {
                    BaseAddress = new Uri(_baseAddress)
                };

                var url = $"Game/search={search}";
                HttpResponseMessage response = http.GetAsync(url).Result;

                if (CheckStatusCodes(response, errorMessage))
                {
                    var games = response.Content.ReadAsAsync <List <Game> >();
                    return(await games);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) // Any exception in the program
            {
                errorMessage(ex.Message);
                return(null);
            }
        }
Exemple #5
0
        // Gets the items on played table
        // Essentially mirrors the GetWishlist but instead gets the played items
        public async Task <List <Game> > GetPlayed(HomePage.ErrorHandling errorMessage, string id)
        {
            bool connection = _checkConnection.hasConnection(errorMessage);

            if (connection)
            {
                if (id != null)
                {
                    var items = await _wishlistPlayedProxy.GetItems <Played>(errorMessage, "Played", id);

                    if (items != null)
                    {
                        List <Game> games = new List <Game>();
                        foreach (var item in items)
                        {
                            games.Add((Game)await _customGameProxy.GetGameByID(errorMessage, item.GameID));
                        }
                        return(games);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    errorMessage("Must be logged in");
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        // Gets the user info (Logs them in)
        public async Task <IUser> GetUser(string uName, string password, HomePage.ErrorHandling errorMessage)
        {
            // Checks connection
            bool connection = _checkConnection.hasConnection(errorMessage);

            if (connection)
            {
                // Grabs the user info by their username and password
                IUser user = await _userProxy.GetUser(errorMessage, uName, password);

                if (user != null) // Checks that its not null
                {
                    // If they want their details to be remembered it saves the data locally
                    if (await Storage.ReadTextFileAsync(App.detailsLocation, errorMessage) == "true")
                    {
                        await Storage.WriteTextFileAsync(App.uNameLocation, user.UName, errorMessage);

                        await Storage.WriteTextFileAsync(App.pwrdLocation, user.Pwrd, errorMessage);
                    }

                    return(user); // Return the user
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        // GAME SEARCH PROXY
        // Gets games by a search
        public async Task <IGameRootObject> GetGameBySearch(string search, HomePage.ErrorHandling errorMessage)
        {
            try
            {
                var http = new HttpClient
                {
                    BaseAddress = new Uri(_baseAddress)
                };

                // The prebuilt API requires http headers or API requests can be blocked
                // The header just contains the app name and the version
                http.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue(AppInfo.Name, AppInfo.VersionString));

                var url = $"games?search={search}";
                HttpResponseMessage response = http.GetAsync(url).Result;
                if (CheckStatusCodes(response, errorMessage))
                {
                    //If theres a succesful response return the content
                    var games = response.Content.ReadAsAsync <GameRootObject>();
                    return(await games);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                errorMessage(ex.Message);
                return(null);
            }
        }
Exemple #8
0
        // Gets an item associated with the user ID, Returns a List<T> which can either be a List<Wishlist> or a List<Played>
        public async Task <List <T> > GetItems <T>(HomePage.ErrorHandling errorMessage, string itemChoice, string id)
        {
            try
            {
                if (itemChoice.ToLower() == "wishlist" || itemChoice.ToLower() == "played")
                {
                    var http = new HttpClient
                    {
                        BaseAddress = new Uri(_baseAddress)
                    };

                    var url = $"{itemChoice}/{id}";
                    HttpResponseMessage response = http.GetAsync(url).Result;
                    if (CheckStatusCodes(response, errorMessage))
                    {
                        var games = response.Content.ReadAsAsync <List <T> >();
                        return(await games);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                errorMessage(ex.Message);
                return(null);
            }
        }
Exemple #9
0
        // Used to retrieve a single user, for logging in, Returns an IUser
        public async Task <IUser> GetUser(HomePage.ErrorHandling errorMessage, string uName, string password)
        {
            try
            {
                var http = new HttpClient
                {
                    BaseAddress = new Uri(_baseAddress)
                };

                var url = $"user/user={uName}&pwrd={password}";
                HttpResponseMessage response = http.GetAsync(url).Result;
                if (await CheckStatusCodes(response, errorMessage))
                {
                    var user = response.Content.ReadAsAsync <User>();
                    return(await user);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                // Returns the error to the user so they can report it
                // More of so my app doesnt crash if anything unexpected happens while connecting to the API
                // (I havent found any errors in testing but better to be prepared then for it to crash)
                errorMessage(ex.Message);
                return(null);
            }
        }
        public Settings(HomePage.ErrorHandling displayError, WishlistFunction wishlistFunction, CompletedFunction completedFunction)
        {
            _displayError      = displayError;
            _wishlistFunction  = wishlistFunction;
            _completedFunction = completedFunction;

            InitializeComponent();
        }
Exemple #11
0
 public Search(string searchItem, HomePage.LoginFunction loginFunction, HomePage.UserFunction userFunction, HomePage.ErrorHandling errorHandling)
 {
     _searchItem    = searchItem;
     _loginFunction = loginFunction;
     _userFunction  = userFunction;
     _errorHandling = errorHandling;
     InitializeComponent();
 }
        public WishlistPage(HomePage.UserFunction userFunction, HomePage.ErrorHandling errorHandling, HomePage.GameList gameList)
        {
            _userFunction  = userFunction;
            _errorHandling = errorHandling;
            _gameList      = gameList;

            InitializeComponent();
        }
        // id is the unique id of the record from the database, in the Rawg API its in the form of a slug, in the custom API its in the form of an ObjectID
        // rawgOrCustom is a bool. Depending on where the item is being stored the app will do something different to retrieve it differently
        // It will be set to true if its from the Rawg db, and it will be true if its from my custom db
        public DetailedPage(string id, HomePage.LoginFunction loginFunction, HomePage.UserFunction userFunction, HomePage.ErrorHandling errorHandling)
        {
            _id            = id;
            _loginFunction = loginFunction;
            _userFunction  = userFunction;
            _errorHandling = errorHandling;

            InitializeComponent();
        }
        // This method updates the user details
        public async Task <bool> UpdateUser(User user, HomePage.ErrorHandling errorMessage)
        {
            // This is a short hand form of the code below, I learnt to do it this way to shorten down code and challenge myself to minimize my code
            return(_checkConnection.hasConnection(errorMessage) ? await _userProxy.PutUser(errorMessage, user) : false);

            // Original Code

            //bool connection = _checkConnection.hasConnection(errorMessage);
            //if (connection)
            //    return await _userProxy.PutUser(errorMessage, user);
            //else
            //    return false;
        }
Exemple #15
0
        // Gets games from my Custom API
        // Works the same as above but it gets it from my custom API instead of the Rawg API
        public async Task <List <Game> > GetCustomGames(string search, HomePage.ErrorHandling errorMessage)
        {
            bool connection = _checkConnection.hasConnection(errorMessage);

            if (connection)
            {
                // Shorthand code
                return(!string.IsNullOrEmpty(search) ? await _customGameProxy.GetGamesBySearch(errorMessage, search) ?? null : await _customGameProxy.GetAllGames(errorMessage) ?? null);
            }
            else
            {
                return(null);
            }
        }
Exemple #16
0
        // This class checks the users internet connection
        public bool hasConnection(HomePage.ErrorHandling connectionAlert)
        {
            var current = Connectivity.NetworkAccess;

            if (current != NetworkAccess.Internet)
            {
                // Returns an error if there is no internet
                connectionAlert("No Connection, Please connect to the internet");
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemple #17
0
        // Updates user info, used in settings page, Returns a bool depending on success
        public async Task <bool> PutUser(HomePage.ErrorHandling errorMessage, User user)
        {
            try
            {
                var http = new HttpClient();

                var response = await http.PutAsJsonAsync($"{_baseAddress}User", user);

                return(await CheckStatusCodes(response, errorMessage));
            }
            catch (Exception ex)
            {
                errorMessage(ex.Message);
                return(false);
            }
        }
Exemple #18
0
        // Get the wishlist
        public async Task <List <Game> > GetWishlist(HomePage.ErrorHandling errorMessage, string id)
        {
            // Checks the internet connection
            bool connection = _checkConnection.hasConnection(errorMessage);

            if (connection)
            {
                // Checks the id isnt null
                if (id != null)
                {
                    // Gets the wishlist
                    var items = await _wishlistPlayedProxy.GetItems <Wishlist>(errorMessage, "Wishlist", id);

                    // If it was able to get items back
                    if (items != null)
                    {
                        List <Game> games = new List <Game>();

                        // Gets all the games asociated with the wishlist gameID
                        // Since MongoDB cant do inner joins this was the closest thing i could do
                        foreach (var item in items)
                        {
                            // Each item in the wishlist a search will be made that grabs the game matching its ID
                            games.Add((Game)await _customGameProxy.GetGameByID(errorMessage, item.GameID));
                        }
                        return(games);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    // Using my delegate it will return the error message that the user must be logged in
                    errorMessage("Must be logged in");
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Exemple #19
0
        // Checks the status codes for the HttpResponse and returns true or false and a error message if needed
        private async Task <bool> CheckStatusCodes(HttpResponseMessage response, HomePage.ErrorHandling errorMessage)
        {
            // This handles all status errors
            if (response.IsSuccessStatusCode)
            {
                return(true);
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                errorMessage("No User was found with those details, Please try again");
            }
            else // Any unprepared status code
            {
                errorMessage($"\nStatus Code: {response.StatusCode} \nError Message: {response.ReasonPhrase}");

                // Below is how i tested getting advanced error info to give to Sam
                errorMessage($"Advanced Info: {await response.Content.ReadAsStringAsync()}");
            }
            return(false);
        }
 // Checks the status codes for the HttpResponse and returns true or false and a error message if needed
 private bool CheckStatusCodes(HttpResponseMessage response, HomePage.ErrorHandling errorMessage)
 {
     // This handles all status errors
     if (response.IsSuccessStatusCode)
     {
         return(true);
     }
     else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) // If the API data is somehow null
     {
         errorMessage($"Oops, Try again later, something went wrong: {response.ReasonPhrase}");
     }
     else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) // If the amount of items retrieved is 0 (No games were found in the database)
     {
         errorMessage("No Games were found, Try again later");
     }
     else // Any unprepared status code
     {
         errorMessage($"Something went wrong! \nStatus Code: {response.StatusCode} \nError Message{response.ReasonPhrase}");
     }
     return(false);
 }
Exemple #21
0
        // Used to delete an item from Wishlist or Played Tables, Returns a bool based on success
        public async Task <bool> DeleteItem(HomePage.ErrorHandling errorMessage, string itemChoice, string id)
        {
            try
            {
                if (itemChoice.ToLower() == "played" || itemChoice.ToLower() == "wishlist")
                {
                    var http = new HttpClient();

                    HttpResponseMessage response = await http.DeleteAsync($"{_baseAddress}{itemChoice}/{id}");

                    return(CheckStatusCodes(response, errorMessage));
                }
                else
                {
                    errorMessage($"Unable to delete from {itemChoice}");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                errorMessage(ex.Message);
                return(false);
            }
        }
Exemple #22
0
        // Used to post an item to Wishlist or Played Tables, Returns a bool based on success
        public async Task <bool> PostItem <T>(HomePage.ErrorHandling errorMessage, T item, string itemChoice)
        {
            try
            {
                if (itemChoice == "Played" || itemChoice == "Wishlist")
                {
                    var http = new HttpClient();

                    HttpResponseMessage response = await http.PostAsJsonAsync($"{_baseAddress}{itemChoice}", item);

                    return(CheckStatusCodes(response, errorMessage));
                }
                else
                {
                    errorMessage($"Unable to Add to {itemChoice}");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                errorMessage(ex.Message);
                return(false);
            }
        }
Exemple #23
0
 // A generic Delete function that returns true or false based on success //
 public async Task <bool> DeleteFromWishlistPlayed(HomePage.ErrorHandling errorMessage, string itemChoice, string id) =>
 _checkConnection.hasConnection(errorMessage) ? await _wishlistPlayedProxy.DeleteItem(errorMessage, itemChoice, id) : false;
 public Login(HomePage.ErrorHandling errorHandling)
 {
     InitializeComponent();
     _errorHandling = errorHandling;
 }
Exemple #25
0
 // Registers the user
 public async Task <bool> RegisterUser(HomePage.ErrorHandling errorMessage, User user) =>
 // Checks if there is a connection
 // If there is then it will try to post a user and return true or false based wether the user was able to be posted or not
 // If there is no connection then it returns false
 _checkConnection.hasConnection(errorMessage) ? await _userProxy.PostUser(errorMessage, user) : false;
Exemple #26
0
 // Retrieves a single game from the RAWG Api //
 public async Task <ISingleGameRootObject> GetGame(string slug, HomePage.ErrorHandling errorMessage) =>
 // First checks if there is a connection
 // If there is a connection it will Get the Game info from the API
 // The ?? checks if the item is null, if its null then it will return null, if its not null then it will return the ISingleGameRoot Object
 // Finally if the there is no connection then this method returns null
 _checkConnection.hasConnection(errorMessage) ? await _gameProxy.GetSinlgeGameInfo(slug, errorMessage) ?? null : null;
Exemple #27
0
 // Retrieves a single game from custom API //
 public async Task <IGame> GetCustomGame(string id, HomePage.ErrorHandling errorMessage) =>
 _checkConnection.hasConnection(errorMessage) ? await _customGameProxy.GetGameByID(errorMessage, id) ?? null : null;
Exemple #28
0
 // A generic method that handles adding to wishlist or completed games and returns true or false based on success //
 public async Task <bool> AddToWishlistPlayed <T>(HomePage.ErrorHandling errorMessage, T item, string itemChoice) =>
 // Checks if there is a connection
 // If there is then it will try to post a an item and return true or false based wether the item was able to be posted or not
 // If there is no connection then it returns false
 _checkConnection.hasConnection(errorMessage) ? await _wishlistPlayedProxy.PostItem(errorMessage, item, itemChoice) : false;