I'm wondering if this code can be made more clear and fluent:
using System;
using System.Threading.Tasks;
using Windows.Data.Json;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using eTest.Space.Log;
using eTest.Space.Interface;
using eTest.Objects;
namespace eTest.Space.Services
{
public class WorkGroupDataService : IWorkgroupDataService
{
public string WebApiUri
{
get { return Constants.ServerAddress; }
}
private ObservableCollection<IWorkgroup> Groups;// = new ObservableCollection<IWorkgroup>();
public async Task<ObservableCollection<IWorkgroup>> GetAllWorkGroups()
{
Groups = new ObservableCollection<IWorkgroup>();
//if (Groups.Count == 0)
{
Uri dataUri = new Uri(WebApiUri + "api/workgroup/");
using (var client = new Windows.Web.Http.HttpClient())
{
var response = await client.GetAsync(dataUri).AsTask(); ;
string jsonText = response.Content.ReadAsStringAsync().GetResults();
JsonArray jsonArray = JsonArray.Parse(jsonText);
foreach (JsonValue groupValue in jsonArray)
{
JsonObject groupObject = groupValue.GetObject();
IWorkgroup group = new Workgroup();
group.GroupState = (int)groupObject["GroupState"].GetNumber();
group.WorkgroupName = groupObject["WorkgroupName"].GetString();
group.WorkgroupSecretary = groupObject["WorkgroupProba"].GetString();
group.ObjectID = groupObject["ObjectID"].GetString();
Uri dataUri1 = new Uri(WebApiUri + "api/etest/" + group.ObjectID);
using (var client1 = new Windows.Web.Http.HttpClient())
{
var response1 = await client.GetAsync(dataUri1).AsTask();
if (response1.StatusCode == Windows.Web.Http.HttpStatusCode.Ok)
{
string jsonText1 = response1.Content.ReadAsStringAsync().GetResults();
List<TestSession> Test = JsonConvert.DeserializeObject<List<TestSession>>(jsonText1);
group.Test = Test;
}
}
if (group.Test.Count > 0)
{
Groups.Add(group);
}
}
}
}
Groups = new ObservableCollection<IWorkgroup>(Groups.OrderByDescending((x => x.Test.Count)));
return Groups;
}
}
}