Skip to content

Commit 89a4b48

Browse files
committed
Fix datetime conversion, Add get all school years method and school years
1 parent f216650 commit 89a4b48

6 files changed

Lines changed: 145 additions & 7 deletions

File tree

API.Test/TimeTableTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using WebUntisAPI.Client;
78
using WebUntisAPI.Client.Models;
89
using static API.Test.AuthentificationTests;
910

@@ -37,4 +38,17 @@ public void GetTimeGrid()
3738
else
3839
Assert.Fail();
3940
}
41+
42+
[Test]
43+
public void GetSchoolYears()
44+
{
45+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
46+
47+
Task<SchoolYear[]> schoolYears = Client.GetAllSchoolYearsAsync();
48+
schoolYears.Wait();
49+
if (schoolYears.Result.Length > 0)
50+
Assert.Pass();
51+
else
52+
Assert.Fail();
53+
}
4054
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using WebUntisAPI.Client.Extensions;
9+
10+
namespace WebUntisAPI.Client.Converter
11+
{
12+
internal class SchoolYearJsonConverter : JsonConverter<SchoolYear>
13+
{
14+
public override SchoolYear ReadJson(JsonReader reader, Type objectType, SchoolYear existingValue, bool hasExistingValue, JsonSerializer serializer)
15+
{
16+
JObject obj = JObject.Load(reader);
17+
return new SchoolYear()
18+
{
19+
Id = obj["id"].Value<int>(),
20+
Name = obj["name"].Value<string>(),
21+
StartDate = new DateTime().FromWebUntisTimeFormat(obj["startDate"].Value<string>(), "000"),
22+
EndDate = new DateTime().FromWebUntisTimeFormat(obj["endDate"].Value<string>(), "000"),
23+
};
24+
}
25+
26+
public override void WriteJson(JsonWriter writer, SchoolYear value, JsonSerializer serializer)
27+
{
28+
writer.WriteStartObject();
29+
30+
writer.WritePropertyName("id");
31+
writer.WriteValue(value.Id);
32+
33+
writer.WritePropertyName("name");
34+
writer.WriteValue(value.Name);
35+
36+
value.StartDate.ToWebUntisTimeFormat(out string startDateString, out _);
37+
writer.WritePropertyName("startDate");
38+
writer.WriteValue(startDateString);
39+
40+
value.StartDate.ToWebUntisTimeFormat(out string endDateString, out _);
41+
writer.WritePropertyName("endDate");
42+
writer.WriteValue(endDateString);
43+
44+
writer.WriteEndObject();
45+
}
46+
}
47+
}
48+

WebUntisAPI.Client/Converter/TimegridJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public override Timegrid ReadJson(JsonReader reader, Type objectType, Timegrid e
2525
hours.Add(new SchoolHour()
2626
{
2727
Name = hour["name"].Value<string>(),
28-
StartTime = new DateTime().FromWebUntisTimeFormat("2020-01-01", hour["startTime"].Value<string>()),
29-
EndTime = new DateTime().FromWebUntisTimeFormat("2020-01-01", hour["endTime"].Value<string>())
28+
StartTime = new DateTime().FromWebUntisTimeFormat("20200101", hour["startTime"].Value<string>()),
29+
EndTime = new DateTime().FromWebUntisTimeFormat("20200101", hour["endTime"].Value<string>())
3030
});
3131

3232
timegrid[(Day)dayTimegrid["day"].Value<int>()] = hours.ToArray();

WebUntisAPI.Client/Extensions/DateTimeExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static class DateTimeExtensions
2424
/// <param name="timeString">Time string</param>
2525
public static void ToWebUntisTimeFormat(this DateTime dateTime, out string dateString, out string timeString)
2626
{
27-
dateString = dateTime.ToString("yyyy-MM-dd");
27+
dateString = dateTime.ToString("yyyyMMdd");
2828
timeString = dateTime.Hour + dateTime.ToString("mm");
2929
}
3030

@@ -46,8 +46,8 @@ public static void ToWebUntisTimeFormat(this DateTime dateTime, out string dateS
4646
/// <exception cref="FormatException">Thrown when one of the given strings isn't in the right format</exception>
4747
public static DateTime FromWebUntisTimeFormat(this DateTime dateTime, string dateString, string timeString)
4848
{
49-
Regex dateRegex = new Regex(@"^\d{4}-(0\d|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$"); // Regex for the WebUntis date format
50-
Regex timeRegex = new Regex(@"^(\d|1\d|2[0-3])[0-5]\d$"); // Regex for the WebUntis time format
49+
Regex dateRegex = new Regex(@"^\d{4}(?:0\d|1[0-2])(?:0[1-9]|[1-2]\d|3[0-1])$"); // Regex for the WebUntis date format
50+
Regex timeRegex = new Regex(@"^(?:\d|1\d|2[0-3])[0-5]\d$"); // Regex for the WebUntis time format
5151

5252
// Check if the date- and time strings are valid
5353
bool isDateValid = dateRegex.IsMatch(dateString);
@@ -59,8 +59,8 @@ public static DateTime FromWebUntisTimeFormat(this DateTime dateTime, string dat
5959

6060
// Parse the numbers in the string to value
6161
int year = int.Parse(dateString.Substring(0, 4));
62-
int month = int.Parse(dateString.Substring(5, 2));
63-
int day = int.Parse(dateString.Substring(8, 2));
62+
int month = int.Parse(dateString.Substring(4, 2));
63+
int day = int.Parse(dateString.Substring(6, 2));
6464

6565
bool is4Letters = timeString.Length == 4;
6666
int hour = int.Parse(is4Letters ? timeString.Substring(0, 2) : timeString[0].ToString());

WebUntisAPI.Client/SchoolYear.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Text.RegularExpressions;
8+
using System.Threading.Tasks;
9+
using WebUntisAPI.Client.Converter;
10+
11+
namespace WebUntisAPI.Client
12+
{
13+
/// <summary>
14+
/// A school year
15+
/// </summary>
16+
[DebuggerDisplay("{Name}")]
17+
[JsonConverter(typeof(SchoolYearJsonConverter))]
18+
public class SchoolYear : IEquatable<SchoolYear>, IComparable<SchoolYear>
19+
{
20+
/// <summary>
21+
/// Id of the school year
22+
/// </summary>
23+
public int Id { get; set; }
24+
25+
/// <summary>
26+
/// Name of the school year
27+
/// </summary>
28+
/// <remarks>
29+
/// Example value: 2022/2023
30+
/// </remarks>
31+
public string Name { get; set; }
32+
33+
/// <summary>
34+
/// The date where the school year begins
35+
/// </summary>
36+
public DateTime StartDate { get; set; }
37+
38+
/// <summary>
39+
/// The date where the school year ends
40+
/// </summary>
41+
public DateTime EndDate { get; set; }
42+
43+
/// <inheritdoc/>
44+
public override string ToString() => Name;
45+
46+
#region IEquatable<SchoolYear>
47+
/// <inheritdoc/>
48+
public bool Equals(SchoolYear other) => Id.Equals(other.Id);
49+
#endregion
50+
51+
#region IComparable<SchoolYear>
52+
/// <inheritdoc/>
53+
public int CompareTo(SchoolYear other)
54+
{
55+
if (StartDate.Year > other.StartDate.Year)
56+
return 1;
57+
else if (StartDate.Year < other.StartDate.Year)
58+
return -1;
59+
else
60+
return 0;
61+
}
62+
#endregion
63+
}
64+
}

WebUntisAPI.Client/WebUntisClient.TimeTable.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,17 @@ public async Task<Timegrid> GetTimegridAsync(string id = "getTimegrid", Cancella
4343
Timegrid timeGrid = await MakeRequestAsync<object, Timegrid>(id, "getTimegridUnits", new object(), ct);
4444
return timeGrid;
4545
}
46+
47+
/// <summary>
48+
/// Get all available school years
49+
/// </summary>
50+
/// <param name="id">Identifier for the request</param>
51+
/// <param name="ct">Cancellation token</param>
52+
/// <returns>All school years</returns>
53+
public async Task<SchoolYear[]> GetAllSchoolYearsAsync(string id = "getSchoolyears", CancellationToken ct = default)
54+
{
55+
List<SchoolYear> schoolYears = await MakeRequestAsync<object, List<SchoolYear>>(id, "getSchoolyears", new object(), ct);
56+
return schoolYears.ToArray();
57+
}
4658
}
4759
}

0 commit comments

Comments
 (0)