Skip to content

Commit ec096a9

Browse files
committed
Added 'student' endpoint
1 parent 94c3f4f commit ec096a9

File tree

8 files changed

+163
-1
lines changed

8 files changed

+163
-1
lines changed

HAC.API/Controllers/ControllerUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public static Response GetResponse<T>(HttpContext context, ILogger<T> logger)
4444
{
4545
result = hac.GetAll(container, response.ResponseUri, hacLink);
4646
}
47+
else if (type == typeof(StudentController))
48+
{
49+
result = hac.GetStudentInfo(container, response.ResponseUri, hacLink);
50+
}
4751
else if (type == typeof(CourseController))
4852
{
4953
result = hac.GetCourses(container, response.ResponseUri, hacLink);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using HAC.API.Data.Objects;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace HAC.API.Controllers
6+
{
7+
[Route("api/student")]
8+
[ApiController]
9+
public class StudentController : ControllerBase
10+
{
11+
private readonly ILogger<StudentController> _logger;
12+
13+
public StudentController(ILogger<StudentController> logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
19+
// GET: api/<HacController>
20+
[HttpGet]
21+
public Response Get()
22+
{
23+
// Retrieves the username, password, and hacLink values from the URL Request.
24+
// Then logs in and fetches the result
25+
var result = Utils.GetResponse(HttpContext, _logger);
26+
27+
return result;
28+
}
29+
}
30+
}

HAC.API/Data/Hac.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,19 @@ public HttpWebResponse Login(string link, string username, string password, out
4949

5050
public Response GetAll(CookieContainer cookies, Uri requestUri, string link)
5151
{
52+
var studentInfo = new Student();
5253
var oldAssignmentList = new List<List<TranscriptCourse>>();
5354
var currentAssignmentList = new List<List<AssignmentCourse>>();
5455
var reportCardList = new List<List<Course>>();
5556
var iprList = new List<List<Course>>();
5657
try
5758
{
59+
//student info
60+
string studentData = Utils.GetData(cookies, requestUri, link, ResponseType.Registration);
61+
var studentDataDocument = new HtmlDocument();
62+
studentDataDocument.LoadHtml(studentData);
63+
studentInfo = StudentInfo.GetAllStudentInfo(studentDataDocument);
64+
5865
//report card
5966
string reportCardData = Utils.GetData(cookies, requestUri, link, ResponseType.ReportCards);
6067
var reportCardHtmlDocument = new HtmlDocument();
@@ -85,12 +92,41 @@ public Response GetAll(CookieContainer cookies, Uri requestUri, string link)
8592
return new Response
8693
{
8794
Message = "Success",
95+
StudentInfo = studentInfo,
8896
AssignmentList = currentAssignmentList,
8997
TranscriptList = oldAssignmentList,
9098
ReportCardList = reportCardList,
9199
IprList = iprList
92100
};
93101
}
102+
103+
public Response GetStudentInfo(CookieContainer cookies, Uri requestUri, string link)
104+
{
105+
var studentInfo = new Student();
106+
var reportCardCourses = new List<List<Course>>();
107+
try
108+
{
109+
string studentData = Utils.GetData(cookies, requestUri, link, ResponseType.Registration);
110+
var studentDataDocument = new HtmlDocument();
111+
studentDataDocument.LoadHtml(studentData);
112+
studentInfo = StudentInfo.GetAllStudentInfo(studentDataDocument);
113+
}
114+
catch (Exception e)
115+
{
116+
Console.WriteLine(e);
117+
return new Response
118+
{
119+
Message = $"Error 404: Could not fetch information. Exception: {e}"
120+
};
121+
}
122+
123+
return new Response
124+
{
125+
Message = "Success",
126+
StudentInfo = studentInfo
127+
};
128+
}
129+
94130

95131
public Response GetCourses(CookieContainer cookies, Uri requestUri, string link)
96132
{

HAC.API/Data/Objects/Response.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace HAC.API.Data.Objects
55
public class Response
66
{
77
public string Message { get; set; }
8+
public Student StudentInfo { get; set; }
89
public IEnumerable<IEnumerable<AssignmentCourse>> AssignmentList { get; set; }
910
public IEnumerable<IEnumerable<Course>> IprList { get; set; }
1011
public IEnumerable<IEnumerable<Course>> ReportCardList { get; set; }

HAC.API/Data/Objects/Student.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace HAC.API.Data.Objects
2+
{
3+
public class Student
4+
{
5+
public string StudentName { get; set; }
6+
public string BirthDate { get; set; }
7+
public string HouseTeam { get; set; }
8+
public string CounselorName { get; set; }
9+
public string CounselorEmail { get; set; }
10+
public string Building { get; set; }
11+
public string Gender { get; set; }
12+
public string Calendar { get; set; }
13+
public string Homeroom { get; set; }
14+
public string Grade { get; set; }
15+
public string Language { get; set; }
16+
public string HomeroomTeacher { get; set; }
17+
}
18+
}

HAC.API/Data/StudentInfo.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using HAC.API.Data.Objects;
2+
using HtmlAgilityPack;
3+
4+
namespace HAC.API.Data
5+
{
6+
public static class StudentInfo
7+
{
8+
public static Student GetAllStudentInfo(HtmlDocument registrationDoc)
9+
{
10+
var studentName = Utils.FormatName(registrationDoc.GetElementbyId("plnMain_lblRegStudentName").InnerText);
11+
var birthDate = registrationDoc.GetElementbyId("plnMain_lblBirthDate").InnerText;
12+
var houseTeam = registrationDoc.GetElementbyId("plnMain_lblHouseTeam").InnerText;
13+
var counselorName = Utils.FormatName(registrationDoc.GetElementbyId("plnMain_lblCounselor").InnerText);
14+
var counselorEmail = registrationDoc.GetElementbyId("plnMain_lblCounselor").FirstChild.Attributes[0].Value
15+
.Substring(7);
16+
var buildingName = registrationDoc.GetElementbyId("plnMain_lblBuildingName").InnerText;
17+
var gender = registrationDoc.GetElementbyId("plnMain_lblGender").InnerText;
18+
var calender = registrationDoc.GetElementbyId("plnMain_lblCalendar").InnerText;
19+
var homeroom = registrationDoc.GetElementbyId("plnMain_lblHomeroom").InnerText;
20+
var grade = registrationDoc.GetElementbyId("plnMain_lblGrade").InnerText;
21+
var language = registrationDoc.GetElementbyId("plnMain_lblLanguage").InnerText;
22+
var homeroomTeacher = registrationDoc.GetElementbyId("plnMain_lblHomeroomTeacher").InnerText;
23+
24+
return new Student
25+
{
26+
StudentName = studentName,
27+
BirthDate = birthDate,
28+
HouseTeam = houseTeam,
29+
CounselorName = counselorName,
30+
CounselorEmail = counselorEmail,
31+
Building = buildingName,
32+
Gender = gender,
33+
Calendar = calender,
34+
Homeroom = homeroom,
35+
Grade = grade,
36+
Language = language,
37+
HomeroomTeacher = homeroomTeacher
38+
};
39+
}
40+
41+
// public static Student GetStudentInfo(HtmlDocument registrationDoc)
42+
// {
43+
// var studentName = registrationDoc.GetElementbyId("plnMain_lblRegStudentName").InnerText;
44+
// var language = registrationDoc.GetElementbyId("plnMain_lblLanguage").InnerText;
45+
//
46+
// return new Student
47+
// {
48+
// StudentName = studentName,
49+
// Language = language
50+
// };
51+
// }
52+
}
53+
}

HAC.API/Data/Utils.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,32 @@ public static Tuple<string, string> BeautifyCourseInfo(string courseName = null,
181181

182182
return new Tuple<string, string>(courseName, courseId);
183183
}
184+
185+
public static string FormatName(string fullName)
186+
{
187+
var firstMiddleName = fullName.Split(',')[1].Trim().ToLower();
188+
var fmName = firstMiddleName.Split(' ');
189+
var firstNameBuilder = new StringBuilder();
190+
foreach (var name in fmName)
191+
{
192+
firstNameBuilder.Append(char.ToUpper(name[0]) + name.Substring(1) + " ");
193+
}
194+
195+
var firstName = firstNameBuilder.ToString().TrimEnd(' ');
196+
var lastName = fullName.Split(',')[0].Trim().ToLower();
197+
lastName = char.ToUpper(lastName[0]) + lastName.Substring(1);
198+
fullName = firstName + " " + lastName;
199+
200+
return fullName;
201+
}
184202
}
185203

186204
public enum ResponseType
187205
{
188206
Transcript,
189207
ReportCards,
190208
Assignments,
191-
InterimProgress
209+
InterimProgress,
210+
Registration
192211
}
193212
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ To access a student's information, add the parameter **hacLink**, **username**,
1919
### Endpoints
2020

2121
- `api/hac` - returns all course information for a student
22+
- `api/student` - returns student registration information
2223
- `api/courses` - returns course information for present courses
2324
- `api/ipr` - returns grade information from all interm progress reports
2425
- `api/reportCard` - returns grade information from all report cards

0 commit comments

Comments
 (0)