Skip to content

Commit 8fc6a39

Browse files
committed
Add timegrid
1 parent 8815a66 commit 8fc6a39

6 files changed

Lines changed: 250 additions & 0 deletions

File tree

API.Test/TimeTableTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using WebUntisAPI.Client.Models;
8+
using static API.Test.AuthentificationTests;
9+
10+
namespace API.Test;
11+
12+
[TestFixture]
13+
internal class TimeTableTests
14+
{
15+
[Test]
16+
public void GetTimeGrid()
17+
{
18+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
19+
20+
Task<Timegrid> timegrid = Client.GetTimegridAsync();
21+
timegrid.Wait();
22+
if (timegrid.Result.SchoolDays > 0)
23+
Assert.Pass();
24+
else
25+
Assert.Fail();
26+
}
27+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
using WebUntisAPI.Client.Models;
10+
11+
namespace WebUntisAPI.Client.Converter
12+
{
13+
internal sealed class TimegridJsonConverter : JsonConverter<Timegrid>
14+
{
15+
/// <inheritdoc/>
16+
public override Timegrid ReadJson(JsonReader reader, Type objectType, Timegrid existingValue, bool hasExistingValue, JsonSerializer serializer)
17+
{
18+
JArray dayTimegrids = JArray.Load(reader);
19+
Timegrid timegrid = new Timegrid();
20+
21+
foreach (JObject dayTimegrid in dayTimegrids.Cast<JObject>())
22+
{
23+
List<SchoolHour> hours = new List<SchoolHour>();
24+
foreach (JObject hour in dayTimegrid["timeUnits"].Cast<JObject>())
25+
hours.Add(new SchoolHour()
26+
{
27+
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>())
30+
});
31+
32+
timegrid[(Day)dayTimegrid["day"].Value<int>()] = hours.ToArray();
33+
}
34+
return timegrid;
35+
}
36+
37+
/// <inheritdoc/>
38+
public override void WriteJson(JsonWriter writer, Timegrid value, JsonSerializer serializer)
39+
{
40+
writer.WriteStartArray(); // Write the diferent days
41+
foreach ((Day day, SchoolHour[] schoolHours) in value)
42+
{
43+
writer.WriteStartObject();
44+
45+
writer.WritePropertyName("day");
46+
writer.WriteValue((int)day);
47+
48+
writer.WritePropertyName("timeUnits");
49+
writer.WriteStartArray(); // Write all school hours
50+
foreach (SchoolHour hour in schoolHours)
51+
{
52+
writer.WriteStartObject();
53+
54+
writer.WritePropertyName("name");
55+
writer.WriteValue(hour.Name);
56+
57+
hour.StartTime.ToWebUntisTimeFormat(out string _, out string startTimeString);
58+
writer.WritePropertyName("startTime");
59+
writer.WriteValue(startTimeString);
60+
61+
hour.EndTime.ToWebUntisTimeFormat(out string _, out string endTimeString);
62+
writer.WritePropertyName("endTime");
63+
writer.WriteValue(endTimeString);
64+
65+
writer.WriteEndObject();
66+
}
67+
writer.WriteEndArray();
68+
writer.WriteEndObject();
69+
}
70+
writer.WriteEndArray();
71+
}
72+
}
73+
}

WebUntisAPI.Client/Enums.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,45 @@ public enum Gender
3737
/// </summary>
3838
Female = 1,
3939
}
40+
41+
/// <summary>
42+
/// All day in a week
43+
/// </summary>
44+
public enum Day
45+
{
46+
/// <summary>
47+
/// Sunday
48+
/// </summary>
49+
Sunday = 1,
50+
51+
/// <summary>
52+
/// Monday
53+
/// </summary>
54+
Monday = 2,
55+
56+
/// <summary>
57+
/// Tuesday
58+
/// </summary>
59+
Tuesday = 3,
60+
61+
/// <summary>
62+
/// Wednesday
63+
/// </summary>
64+
Wednesday = 4,
65+
66+
/// <summary>
67+
/// Thursday
68+
/// </summary>
69+
Thursday = 5,
70+
71+
/// <summary>
72+
/// Friday
73+
/// </summary>
74+
Friday = 6,
75+
76+
/// <summary>
77+
/// Saturday
78+
/// </summary>
79+
Saturday = 7,
80+
}
4081
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.Threading.Tasks;
8+
9+
namespace WebUntisAPI.Client.Models
10+
{
11+
/// <summary>
12+
/// One school hour
13+
/// </summary>
14+
[DebuggerDisplay("Name = {Name}, from {StartTime.Hour}:{StartTime.Minute} to {EndTime.Hour}:{EndTime.Minute}")]
15+
public class SchoolHour
16+
{
17+
/// <summary>
18+
/// Name of the school hour
19+
/// </summary>
20+
public string Name { get; set; }
21+
22+
/// <summary>
23+
/// The start time of the school hour
24+
/// </summary>
25+
/// <remarks>
26+
/// Only <see cref="DateTime.Hour"/> and <see cref="DateTime.Minute"/> is set and the date is always set on the 1.1.2020
27+
/// </remarks>
28+
public DateTime StartTime { get; set; }
29+
30+
/// <summary>
31+
/// The end time of the school hour
32+
/// </summary>
33+
/// <remarks>
34+
/// Only <see cref="DateTime.Hour"/> and <see cref="DateTime.Minute"/> is set and the date is always set on the 1.1.2020
35+
/// </remarks>
36+
public DateTime EndTime { get; set; }
37+
}
38+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using WebUntisAPI.Client.Converter;
10+
11+
namespace WebUntisAPI.Client.Models
12+
{
13+
/// <summary>
14+
/// A timegrid for a school
15+
/// </summary>
16+
[DebuggerDisplay("Days = {SchoolDays}")]
17+
[JsonConverter(typeof(TimegridJsonConverter))]
18+
public class Timegrid : IEnumerable<(Day, SchoolHour[])>
19+
{
20+
private readonly Dictionary<Day, SchoolHour[]> _schoolDays = new Dictionary<Day, SchoolHour[]>();
21+
22+
/// <summary>
23+
/// The count of school hours (mostly 5)
24+
/// </summary>
25+
public int SchoolDays => _schoolDays.Count;
26+
27+
/// <summary>
28+
/// Get the school hours by the day
29+
/// </summary>
30+
/// <param name="day">The day</param>
31+
/// <returns>The school hours</returns>
32+
/// <exception cref="KeyNotFoundException">Thrown when the given day not found (mostly <see cref="Day.Saturday"/> and <see cref="Day.Sunday"/>)</exception>
33+
public SchoolHour[] this[Day day]
34+
{
35+
get => _schoolDays[day];
36+
set
37+
{
38+
if (_schoolDays.ContainsKey(day))
39+
_schoolDays[day] = value;
40+
else
41+
_schoolDays.Add(day, value);
42+
}
43+
}
44+
45+
#region IEnumerable<SchoolHour[]>
46+
/// <inheritdoc/>
47+
public IEnumerator<(Day, SchoolHour[])> GetEnumerator() => _schoolDays.Keys.Zip(_schoolDays.Values, (day, schoolHours) => (day, schoolHours)).GetEnumerator();
48+
49+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
50+
#endregion
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using WebUntisAPI.Client.Models;
8+
9+
namespace WebUntisAPI.Client
10+
{
11+
public partial class WebUntisClient
12+
{
13+
public async Task<Timegrid> GetTimegridAsync(string id = "getTimegrid", CancellationToken ct = default)
14+
{
15+
Timegrid timeGrid = await MakeRequestAsync<object, Timegrid>(id, "getTimegridUnits", new object(), ct);
16+
return timeGrid;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)