-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseManager.cs
140 lines (102 loc) · 4.64 KB
/
CourseManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Xml;
using CourseView.Shared;
using Microsoft.AspNetCore.Components.Forms;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace CourseView;
public enum EndPointType
{
All,
Query,
QueryMatch,
TermGet
}
public class CourseManager
{
protected static HttpClient WebClient;
public static CourseViewConfig Config;
public static Course[] Courses;
public static Term[] Terms;
public static string DefaultConfigFilePath = "config.json";
public static string BaseURL = "http://localhost:5157";
public static string[] APIUrls = new string[] {
$"{CourseManager.BaseURL}/courses",
$"{CourseManager.BaseURL}/courses/query",
$"{CourseManager.BaseURL}/courses/querymatch",
$"{CourseManager.BaseURL}/terms",
};
public static void LoadConfig()
{
// CourseManager.Config = CourseViewConfig.LoadFromFile(CourseManager.DefaultConfigFilePath);
}
protected static List<Course> GetCoursesFromJson(string[] json)
{
return new List<Course>();
}
public static Course GetCourseBySubjectCode(string subject, int courseNumber)
{
for (int x = 0; x < CourseManager.Courses.Length; x++)
if (CourseManager.Courses[x].Subject == subject && CourseManager.Courses[x].CourseNumber == courseNumber)
return CourseManager.Courses[x];
return null;
}
public static async Task<Term[]> GetTerms()
{
string[] termStrings = JsonSerializer.Deserialize<string[]>(JsonSerializer.Deserialize<Hashtable>(await CourseManager.WebClient.GetStringAsync(CourseManager.APIUrls[(int)EndPointType.TermGet]))["Payload"].ToString());
Term[] terms = new Term[termStrings.Length];
for (int x = 0; x < termStrings.Length; x++)
terms[x] = new Term(termStrings[x]);
return (CourseManager.Terms = terms);
}
public static async Task<Course[]> FetchCoursesByQuery(Hashtable queryHash)
{
CourseManager.WebClient = new HttpClient();
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, CourseManager.APIUrls[(int)EndPointType.Query]);
Hashtable response;
requestMessage.Headers.Add("query", JsonConvert.SerializeObject(queryHash));
Console.WriteLine(JsonConvert.SerializeObject(requestMessage));
response = JsonSerializer.Deserialize<Hashtable>(await (await CourseManager.WebClient.SendAsync(requestMessage)).Content.ReadAsStringAsync());
if (response["Type"].ToString() == "0")
return null;
CourseManager.Courses = JsonSerializer.Deserialize<Course[]>(response["Payload"].ToString());
return CourseManager.Courses;
}
public static async Task<Course[]> FetchCoursesByQueryMatch(Hashtable queryHash)
{
CourseManager.WebClient = new HttpClient();
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, CourseManager.APIUrls[(int)EndPointType.QueryMatch]);
Hashtable response, queryHashFormatted = new Hashtable();
foreach (string key in queryHash.Keys)
if (queryHash[key] != null)
{
if ((queryHash[key] is int && (int)queryHash[key] == 0))
continue;
queryHashFormatted.Add(key, queryHash[key]);
}
requestMessage.Headers.Add("query", JsonConvert.SerializeObject(queryHashFormatted));
Console.WriteLine(JsonConvert.SerializeObject(requestMessage));
response = JsonSerializer.Deserialize<Hashtable>(await (await CourseManager.WebClient.SendAsync(requestMessage)).Content.ReadAsStringAsync());
if (response["Type"].ToString() == "0")
return null;
if (response["Payload"] != null)
CourseManager.Courses = JsonSerializer.Deserialize<Course[]>(response["Payload"].ToString());
return CourseManager.Courses;
}
public static async Task<Course[]> FetchCourses()
{
CourseManager.WebClient = new HttpClient();
return await Task.Run(async ()=>
{
return JsonSerializer.Deserialize<Course[]>(
(await JsonSerializer.DeserializeAsync<Hashtable>((await CourseManager.WebClient.GetStreamAsync($"http://localhost:5157/courses"))))["Payload"].ToString());
});
}
public static void Initialize()
{
CourseManager.WebClient = new HttpClient();
}
}