Beispiel #1
0
        // ANY CREATED JSON CAN BE FOUND IN "SimplyRugby\bin\Debug" UNLESS IN RELEASE VERSION

        // Gets the details of the player or coach and serializes it into a JSON string
        // I have to retrieve both the Player and Coach details but cannot package them into a generic object so I need to request these 2 and just send null in one of them
        public void ConvertToJSON(string filePath, Player playerDetails, Coach coachDetails)
        {
            // Sets up the options for the formatting of the JSON file
            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            // Checks if the supplied JSONPath exists
            if (File.Exists(filePath))
            {
                // Reads the whole JSON file
                var jsonString = File.ReadAllText(filePath);

                if (filePath == "Players.json")
                {
                    // Deserializes the JSON and turns it into a list writing all current info into the list
                    var newList = JsonSerializer.Deserialize <List <Player> >(jsonString) ?? new List <Player>();

                    // Package all the Player Information into the List
                    newList.Add(new Player()
                    {
                        name    = playerDetails.name,
                        dob     = playerDetails.dob,
                        email   = playerDetails.email,
                        phoneNo = playerDetails.phoneNo,
                        sru     = playerDetails.sru,
                        squad   = playerDetails.squad,

                        lastChanged = playerDetails.lastChanged,
                        standard    = playerDetails.standard,
                        spin        = playerDetails.spin,
                        pop         = playerDetails.pop,

                        front    = playerDetails.front,
                        rear     = playerDetails.rear,
                        side     = playerDetails.side,
                        scrabble = playerDetails.scrabble,

                        drop    = playerDetails.drop,
                        punt    = playerDetails.punt,
                        grubber = playerDetails.grubber,
                        goal    = playerDetails.goal,

                        passingComments  = playerDetails.passingComments,
                        kickingComments  = playerDetails.kickingComments,
                        tacklingComments = playerDetails.tacklingComments
                    });

                    // Turns the list into a JSON
                    jsonString = JsonSerializer.Serialize(newList, options);
                }
                else
                {
                    // Deserializes the JSON and turns it into a list writing all current info into the list
                    var newList = JsonSerializer.Deserialize <List <Coach> >(jsonString) ?? new List <Coach>();

                    // Package all the Coach Information into the List
                    newList.Add(new Coach()
                    {
                        name  = coachDetails.name,
                        email = coachDetails.email
                    });

                    // Turns the list into a JSON
                    jsonString = JsonSerializer.Serialize(newList, options);
                }

                // Overwrites the file
                File.WriteAllText(filePath, jsonString);
            }
            else
            {
                if (filePath == "Players.json")
                {
                    // Create a list with all the Player Details
                    List <Player> List = new List <Player>();

                    List.Add(new Player()
                    {
                        name    = playerDetails.name,
                        dob     = playerDetails.dob,
                        email   = playerDetails.email,
                        phoneNo = playerDetails.phoneNo,
                        sru     = playerDetails.sru,
                        squad   = playerDetails.squad,

                        lastChanged = playerDetails.lastChanged,
                        standard    = playerDetails.standard,
                        spin        = playerDetails.spin,
                        pop         = playerDetails.pop,

                        front    = playerDetails.front,
                        rear     = playerDetails.rear,
                        side     = playerDetails.side,
                        scrabble = playerDetails.scrabble,

                        drop    = playerDetails.drop,
                        punt    = playerDetails.punt,
                        grubber = playerDetails.grubber,
                        goal    = playerDetails.goal,

                        passingComments  = playerDetails.passingComments,
                        kickingComments  = playerDetails.kickingComments,
                        tacklingComments = playerDetails.tacklingComments
                    });

                    // Turns the list into a JSON
                    string jsonString = JsonSerializer.Serialize(List, options);

                    // Adds text to the file, if no File exists then creates it
                    File.AppendAllText(filePath, jsonString);
                }
                else
                {
                    // Create a list with all the Player Details
                    List <Coach> List = new List <Coach>();

                    List.Add(new Coach()
                    {
                        name  = coachDetails.name,
                        email = coachDetails.email
                    });

                    // Turns the list into a JSON
                    string jsonString = JsonSerializer.Serialize(List, options);

                    // Adds text to the file, if no File exists then creates it
                    File.AppendAllText(filePath, jsonString);
                }
            }
        }
Beispiel #2
0
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (playersDisplayed)
            {
                Player  players = new Player();
                dynamic JSON    = json.ConvertFromJSON("Players.json");

                foreach (var player in JSON)
                {
                    if (player.name == lstDisplay.SelectedItem.ToString())
                    {
                        // I save the edited information of the player before deleting them
                        // I have to delete the player from the JSON otherwise they will be duplicated
                        player.name    = txtPlayerName.Text;
                        player.dob     = playerDOB.SelectedDate.Value.Date;
                        player.phoneNo = long.Parse(txtPlayerPhone.Text);
                        player.sru     = int.Parse(txtPlayerSRU.Text);
                        player.email   = txtPlayerEmail.Text;

                        player.lastChanged = DateTime.UtcNow;
                        players.squad      = player.squad;

                        players.standard = player.standard;
                        players.spin     = player.spin;
                        players.pop      = player.pop;

                        players.front    = player.front;
                        players.rear     = player.rear;
                        players.side     = player.side;
                        players.scrabble = player.scrabble;

                        players.drop    = player.drop;
                        players.punt    = player.punt;
                        players.grubber = player.grubber;
                        players.goal    = player.goal;

                        players.passingComments  = player.passingComments;
                        players.kickingComments  = player.kickingComments;
                        players.tacklingComments = player.tacklingComments;

                        json.DeleteFromJSON(lstDisplay.SelectedItem.ToString(), "Players.json");

                        // This is a bit hacky but basically I removed the old player details from the JSON and am now overwriting the file with the non edited players as to not get any duplicate Players
                        File.WriteAllText("Players.json", "[]");
                        foreach (var thePlayer in JSON)
                        {
                            json.ConvertToJSON("Players.json", thePlayer, null);
                        }

                        MessageBox.Show("Edited Player");
                    }
                }
            }
            else
            {
                Coach   coach = new Coach();
                dynamic JSON  = json.ConvertFromJSON("Coaches.json");
                foreach (var coaches in JSON)
                {
                    if (coaches.name == lstDisplay.SelectedItem.ToString())
                    {
                        coaches.name  = txtCoachName.Text;
                        coaches.email = txtCoachEmail.Text;
                        json.DeleteFromJSON(lstDisplay.SelectedItem.ToString(), "Coaches.json");
                        MessageBox.Show("Edited Coach");

                        // This is a bit hacky but basically I removed the old coach details from the JSON and am now overwriting the file with the non edited coaches as to not get any duplicate Coaches
                        File.WriteAllText("Coaches.json", "[]");
                        foreach (var theCoaches in JSON)
                        {
                            json.ConvertToJSON("Coaches.json", null, theCoaches);
                        }
                    }
                }
            }
        }