Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hinho1106 committed Dec 1, 2024
0 parents commit cbe2c59
Show file tree
Hide file tree
Showing 11 changed files with 1,047 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.vs
/bin
/obj
*.csproj
*.user
*.sln
139 changes: 139 additions & 0 deletions DataHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportInfoUI
{
public static class DataHandler
{


public static async Task<List<SportInfo>> processInput(string sport, string week, string day, string exAge, string exWord)
{
//General variables
bool controlFlag = true;
List<SportInfo> infoTable = new List<SportInfo>();
string[] exAges = exAge.Split(';');
string[] exWords = exAge.Split(";");

string generalResponse = await HTTPHandler.GETGeneralResponse();

RootObj rootObj;

//Convert response to JSON format
try
{
rootObj = JsonConvert.DeserializeObject<RootObj>(generalResponse);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return infoTable;
}

foreach (var commCentre in rootObj.features)
{
List<string?> sportCat = new List<string?>();
//string? sportlist = commCentre.attributes.sports_activities_e_p;
sportCat.Add(commCentre.attributes.sports_activities_a_d);
sportCat.Add(commCentre.attributes.sports_activities_e_p);
sportCat.Add(commCentre.attributes.sports_activities_s_z);


string? centreName = commCentre.attributes.complexname;
int id = commCentre.attributes.locationid;

foreach(string? sportlist in sportCat)
{
if (!string.IsNullOrEmpty(sportlist) ? sportlist.Contains(sport) : false)
{


string info = await HTTPHandler.GETCommSportInfo(id, week);

RootPrograms? infoObj;

//Convert response to JSON format
try
{
infoObj = JsonConvert.DeserializeObject<RootPrograms>(info);
if (infoObj == null)
{
continue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return infoTable;
}


try
{

List<Day>? programs = infoObj.programs[0].days;
foreach (Day program in programs)
{
//Set up controlFlag
controlFlag = program.title.Contains(sport);
if (!exAge.Equals(""))
foreach (string age in exAges)
{
controlFlag &= !program.age.Contains(age);
}
if (!exWord.Equals(""))
foreach (string word in exWords)
{
controlFlag &= !program.title.Contains(word);
}

if (controlFlag)
{

foreach (Time time in program.times)
{
if (time.day.Contains(day))
{
//commCentre, sportName, weekday, time
infoTable.Add(new SportInfo()
{
commCentre = centreName,
sport = program.title + " " + program.age,
day = time.day,
time = time.title
});
//Console.WriteLine(program.title);


//Console.WriteLine(time.day + "\t" + time.title);
}


}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return new List<SportInfo>();
}

}
}




}

return infoTable;
}


}
}
64 changes: 64 additions & 0 deletions HTTPHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace SportInfoUI
{
internal static class HTTPHandler
{


public static async Task<string> GETGeneralResponse()
{
HttpClient client = new HttpClient();

const string url = "https://services3.arcgis.com/b9WvedVPoizGfvfD/arcgis/rest/services/COT_Sports_DropIn_View/FeatureServer/0/query?f=json&where=show_on_sports_map%20=%20%27Yes%27&returnGeometry=true&spatialRel=esriSpatialRelIntersects&outFields=*&outSR=102100&resultOffset=0&resultRecordCount=4000";

HttpResponseMessage response = await client.GetAsync(url);


if (response.IsSuccessStatusCode)
{
//Console.WriteLine("Request Success");
return await response.Content.ReadAsStringAsync();

}
else
{
MessageBox.Show("Request failed with status code: " + response.StatusCode);
return "";

}

}

public static async Task<string> GETCommSportInfo(int centreID, string week)
{
HttpClient client = new HttpClient();


string url = "https://www.toronto.ca/data/parks/live/locations/" + centreID + "/sports/week" + week + ".json";

HttpResponseMessage response = await client.GetAsync(url);


if (response.IsSuccessStatusCode)
{
//Console.WriteLine("Request Success");
//Console.WriteLine();
return await response.Content.ReadAsStringAsync();

}
else
{
MessageBox.Show("Request failed with status code: " + response.StatusCode);
return "";

}
}
}
}
108 changes: 108 additions & 0 deletions JSONClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportInfoUI
{
//JSON format for macro info
public class RootObj
{
public string? objectIdFieldName { get; set; }
public UniqueIdField? uniqueIdField { get; set; }
public string? globalIdFieldName { get; set; }
public string? geometryType { get; set; }
public SpatialReference? spatialReference { get; set; }
public List<Field>? fields { get; set; }
public List<Feature>? features { get; set; }
}

public class Attributes
{
public int objectid_1 { get; set; }
public int? objectid { get; set; }
public int locationid { get; set; }
public int facility_master_id { get; set; }
public string? complexname { get; set; }
public string? address { get; set; }
public string? website { get; set; }
public string? location_type { get; set; }
public string? show_on_sports_map { get; set; }
public string? sports_activities_a_d { get; set; }
public string? sports_activities_e_p { get; set; }
public string? sports_activities_s_z { get; set; }
public double x { get; set; }
public double y { get; set; }
public string? globalid { get; set; }
}

public class Feature
{
public Attributes? attributes { get; set; }
public Geometry? geometry { get; set; }
}

public class Field
{
public string? name { get; set; }
public string? type { get; set; }
public string? alias { get; set; }
public string? sqlType { get; set; }
public object? domain { get; set; }
public object? defaultValue { get; set; }
public int? length { get; set; }
}

public class Geometry
{
public double x { get; set; }
public double y { get; set; }
}

public class SpatialReference
{
public int wkid { get; set; }
public int latestWkid { get; set; }
}

public class UniqueIdField
{
public string? name { get; set; }
public bool? isSystemMaintained { get; set; }
}

//JSON format for particular sport info of a comm centre
public class Day
{
public int id { get; set; }
public string? day { get; set; }
public string? title { get; set; }
public string? age { get; set; }
public string? status { get; set; }
public string? comment { get; set; }
public string? link { get; set; }
public List<Time>? times { get; set; }
}

public class Program
{
public string? program { get; set; }
public List<Day>? days { get; set; }
}

public class RootPrograms
{
public List<Program>? programs { get; set; }
}

public class Time
{
public string? id { get; set; }
public string? day { get; set; }
public string? title { get; set; }
public string? status { get; set; }
public string? comment { get; set; }
public string? link { get; set; }
}
}
46 changes: 46 additions & 0 deletions LoadingMessageBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportInfoUI
{
internal class LoadingMessageBox : Form
{
//public string Text { get; set; }

public LoadingMessageBox()
{
InitializeComponent();
}



private void InitializeComponent()
{
label1 = new Label();
SuspendLayout();
//
// label1
//
label1.AutoSize = true;
label1.Font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
label1.Location = new Point(42, 23);
label1.Name = "label1";
label1.Size = new Size(114, 32);
label1.TabIndex = 0;
label1.Text = "Loading...";
//
// LoadingMessageBox
//
ClientSize = new Size(212, 83);
Controls.Add(label1);
Name = "LoadingMessageBox";
ResumeLayout(false);
PerformLayout();
}

private Label label1;
}
}
Loading

0 comments on commit cbe2c59

Please sign in to comment.