added data access layers
This commit is contained in:
		@@ -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> RepoDetail { get; set; }
 | 
			
		||||
 | 
			
		||||
        public ApiDataService(string query)
 | 
			
		||||
        {
 | 
			
		||||
            _query = query;
 | 
			
		||||
            _apiEndpoint = DataConfig.dataConfString;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //public Request(string query, Object function)
 | 
			
		||||
        //{
 | 
			
		||||
        //    _query = query;
 | 
			
		||||
        //} 
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Calls API for repo and basic user data
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        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<ObservableCollection<RepoDetail>>(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";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Calls API for detailed user data
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        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<User>(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.";
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Calls API for detailed branch data
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        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<ObservableCollection<Branch>>(x);
 | 
			
		||||
 | 
			
		||||
                RequestList.branchDetail = deserailized;
 | 
			
		||||
            }
 | 
			
		||||
            catch (NullReferenceException)
 | 
			
		||||
            {
 | 
			
		||||
                ErrorWindow err = new ErrorWindow();
 | 
			
		||||
                err.txtblk_error.Text = "We couldn't gather user data. Please try again.";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,10 @@
 | 
			
		||||
namespace DeskHubSharpRevised.DAL;
 | 
			
		||||
 | 
			
		||||
public interface IDataService
 | 
			
		||||
{
 | 
			
		||||
    //List<Search> ReadAll();
 | 
			
		||||
    //void WriteAll(List<Search> user);
 | 
			
		||||
    void SearchRequest();
 | 
			
		||||
    void UserRequest();
 | 
			
		||||
    void BranchRequest();
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// reads all the things from the json string/data
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <returns></returns>
 | 
			
		||||
    public IEnumerable<Search> ReadAll()
 | 
			
		||||
    {
 | 
			
		||||
        List<Search> user = new List<Search>();
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            using (StreamReader sr = new StreamReader(_dataConfig))
 | 
			
		||||
            {
 | 
			
		||||
                string jsonString = sr.ReadToEnd();
 | 
			
		||||
 | 
			
		||||
                Search.ItemsItem users = JsonConvert.DeserializeObject<Search.ItemsItem>(_dataConfig);
 | 
			
		||||
                //user = users.items;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception e)
 | 
			
		||||
        {
 | 
			
		||||
            throw;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void WriteAll(IEnumerable<User> characters)
 | 
			
		||||
    {
 | 
			
		||||
        //RootObject rootObject = new RootObject();
 | 
			
		||||
        //rootObject.Characters = new Characters();
 | 
			
		||||
        //rootObject.Characters.Character = characters as List<Character>;
 | 
			
		||||
        //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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user