diff --git a/DeskHubSharpRevised/DAL/ApiDataService.cs b/DeskHubSharpRevised/DAL/ApiDataService.cs index 9b39305..f615beb 100644 --- a/DeskHubSharpRevised/DAL/ApiDataService.cs +++ b/DeskHubSharpRevised/DAL/ApiDataService.cs @@ -1,6 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using DeskHubSharpRevised.Data; +using DeskHubSharpRevised.Models; +using Newtonsoft.Json; +using RestSharp; + namespace DeskHubSharpRevised.DAL; public class ApiDataService { - + private string _apiEndpoint; + private string _query; + + public List RepoDetail { get; set; } + + public ApiDataService(string query) + { + _query = query; + _apiEndpoint = DataConfig.dataConfString; + } + + //public Request(string query, Object function) + //{ + // _query = query; + //} + /// + /// Calls API for repo and basic user data + /// + /// + public void SearchRequest() + { + try + { + var client = new RestClient(_apiEndpoint); + RestRequest requestRepo = new RestRequest($"users/{_query}/repos", Method.Get); + + var response = client.Execute(requestRepo); + var x = response.Content; + var deserialized = JsonConvert.DeserializeObject>(x); + + if (deserialized.Count == 0) + { + throw new Exception(); + } + else + { + RequestList.repoDetail = deserialized; + } + } + catch (Exception) + { + ErrorWindow err = new ErrorWindow(); + err.txtblk_error.Text = "We couldn't gather repository data. Please try again"; + } + } + + + /// + /// Calls API for detailed user data + /// + public void UserRequest() + { + try + { + var client = new RestClient(_apiEndpoint); + + RestRequest requestUser = new RestRequest($"users/{_query}", Method.Get); + + var response = client.Execute(requestUser); + string x = response.Content; + var deserailized = JsonConvert.DeserializeObject(x); + + if (deserailized == null) + { + throw new Exception(); + } + else + { + RequestList.userDetail = deserailized; + } + + } + catch (NullReferenceException) + { + ErrorWindow err = new ErrorWindow(); + err.txtblk_error.Text = "We couldn't gather user data. Please try again."; + } + + } + + /// + /// Calls API for detailed branch data + /// + public void BranchRequest() + { + try + { + var client = new RestClient(_apiEndpoint); + + RestRequest requestUser = new RestRequest($"/repos/{RequestList.userDetail.login}/{_query}/branches", Method.Get); + + var response = client.Execute(requestUser); + string x = response.Content; + var deserailized = JsonConvert.DeserializeObject>(x); + + RequestList.branchDetail = deserailized; + } + catch (NullReferenceException) + { + ErrorWindow err = new ErrorWindow(); + err.txtblk_error.Text = "We couldn't gather user data. Please try again."; + } + } } \ No newline at end of file diff --git a/DeskHubSharpRevised/DAL/IDataService.cs b/DeskHubSharpRevised/DAL/IDataService.cs index e69de29..16d0299 100644 --- a/DeskHubSharpRevised/DAL/IDataService.cs +++ b/DeskHubSharpRevised/DAL/IDataService.cs @@ -0,0 +1,10 @@ +namespace DeskHubSharpRevised.DAL; + +public interface IDataService +{ + //List ReadAll(); + //void WriteAll(List user); + void SearchRequest(); + void UserRequest(); + void BranchRequest(); +} \ No newline at end of file diff --git a/DeskHubSharpRevised/DAL/JsonDataService.cs b/DeskHubSharpRevised/DAL/JsonDataService.cs index 177a463..4e9024f 100644 --- a/DeskHubSharpRevised/DAL/JsonDataService.cs +++ b/DeskHubSharpRevised/DAL/JsonDataService.cs @@ -1,6 +1,70 @@ +using System; +using System.Collections.Generic; +using System.IO; +using DeskHubSharpRevised.Models; +using Newtonsoft.Json; + namespace DeskHubSharpRevised.DAL; public class JsonDataService { - + private string _dataConfig; + + /// + /// reads all the things from the json string/data + /// + /// + public IEnumerable ReadAll() + { + List user = new List(); + + try + { + using (StreamReader sr = new StreamReader(_dataConfig)) + { + string jsonString = sr.ReadToEnd(); + + Search.ItemsItem users = JsonConvert.DeserializeObject(_dataConfig); + //user = users.items; + } + + } + catch (Exception e) + { + throw; + } + + return user; + } + + + public void WriteAll(IEnumerable characters) + { + //RootObject rootObject = new RootObject(); + //rootObject.Characters = new Characters(); + //rootObject.Characters.Character = characters as List; + //string jsonString = JsonConvert.SerializeObject(rootObject); + try + { + StreamWriter writer = new StreamWriter(_dataConfig); + using (writer) + { + //writer.WriteLine(jsonString); + } + } + catch (Exception e) + { + throw; + } + } + + public JsonDataService() + { + + } + + public JsonDataService(string dataFile) + { + _dataConfig = dataFile; + } } \ No newline at end of file