Skip to content

Commit 311b7d0

Browse files
committed
Add periods and all methods to get the timetable with the periods
1 parent f32d37f commit 311b7d0

5 files changed

Lines changed: 341 additions & 0 deletions

File tree

API.Test/TimeTableTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,17 @@ public void GetHolidays()
7676
else
7777
Assert.Fail();
7878
}
79+
80+
[Test]
81+
public void GetTimetable()
82+
{
83+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
84+
85+
Task<Period[]> timetable = Client.GetTimetableForStudentAsync(Client.User as Student, new DateTime(2023, 06, 09), new DateTime(2023, 06, 16));
86+
timetable.Wait();
87+
if (timetable.Result.Length > 0)
88+
Assert.Pass();
89+
else
90+
Assert.Fail();
91+
}
7992
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace WebUntisAPI.Client.Models
11+
{
12+
/// <summary>
13+
/// The id of an object and when its irregular the original id and the current id
14+
/// </summary>
15+
public struct ObjectId
16+
{
17+
/// <summary>
18+
/// The id
19+
/// </summary>
20+
[JsonProperty("id")]
21+
public int Id { get; set; }
22+
23+
/// <summary>
24+
/// The original id
25+
/// </summary>
26+
[DefaultValue(null)]
27+
[JsonProperty("orgid")]
28+
public int? OriginalId { get; set; }
29+
}
30+
}
31+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
using WebUntisAPI.Client.Converter;
9+
10+
namespace WebUntisAPI.Client.Models
11+
{
12+
/// <summary>
13+
/// A lesson period
14+
/// </summary>
15+
[DebuggerDisplay("On: {Date.Month, nq}.{Date.Day, nq}.{Date.Year, nq}, From {StartTime.Hour, nq}:{StartTime.Minute, nq} to {EndTime.Hour, nq}:{EndTime.Minute, nq}")]
16+
public class Period
17+
{
18+
/// <summary>
19+
/// The id of the period
20+
/// </summary>
21+
[JsonProperty("id")]
22+
public int Id { get; set; }
23+
24+
/// <summary>
25+
/// The date where the period is
26+
/// </summary>
27+
[JsonProperty("date")]
28+
[JsonConverter(typeof(DateJsonConverter))]
29+
public DateTime Date { get; set; }
30+
31+
/// <summary>
32+
/// The time where the period start
33+
/// </summary>
34+
/// <remarks>
35+
/// Date is default set on the 1.1.2020 and only the time is set
36+
/// </remarks>
37+
[JsonProperty("startTime")]
38+
[JsonConverter(typeof(TimeJsonConverter))]
39+
public DateTime StartTime { get; set; }
40+
41+
/// <summary>
42+
/// The time where the period ends
43+
/// </summary>
44+
/// <remarks>
45+
/// Date is default set on the 1.1.2020 and only the time is set
46+
/// </remarks>
47+
[JsonProperty("endTime")]
48+
[JsonConverter(typeof(TimeJsonConverter))]
49+
public DateTime EndTime { get; set; }
50+
51+
/// <summary>
52+
/// Ids of all involved classes
53+
/// </summary>
54+
[JsonProperty("kl")]
55+
public ObjectId[] ClassIds { get; set; }
56+
57+
/// <summary>
58+
/// Ids of all involved teachers
59+
/// </summary>
60+
[JsonProperty("te")]
61+
public ObjectId[] TeacherIds { get; set; }
62+
63+
/// <summary>
64+
/// Ids of all involved subjects
65+
/// </summary>
66+
[JsonProperty("su")]
67+
public ObjectId[] SubjectsIds { get; set; }
68+
69+
/// <summary>
70+
/// Ids of all involved room
71+
/// </summary>
72+
[JsonProperty("ro")]
73+
public ObjectId[] RoomIds { get; set; }
74+
75+
/// <summary>
76+
/// The activity type in the lesson
77+
/// </summary>
78+
[JsonProperty("activityType")]
79+
public string ActivityType { get; set; } = string.Empty;
80+
}
81+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.Converter;
8+
9+
namespace WebUntisAPI.Client.Models
10+
{
11+
internal struct TimetableRequestModel
12+
{
13+
/// <summary>
14+
/// Id of that what you want too request
15+
/// </summary>
16+
[JsonProperty("id")]
17+
public int Id { get; set; }
18+
19+
/// <summary>
20+
/// The type of what you want to request
21+
/// </summary>
22+
/// <remarks>
23+
/// The meanings:
24+
/// <list type="bullet">
25+
/// <item>1 = Class</item>
26+
/// <item>2 = teacher</item>
27+
/// <item>3 = subject</item>
28+
/// <item>4 = room</item>
29+
/// <item>5 = student</item>
30+
/// </list>
31+
/// </remarks>
32+
[JsonProperty("type")]
33+
public int Type { get; set; }
34+
35+
/// <summary>
36+
/// Start date of requested informations
37+
/// </summary>
38+
[JsonProperty("startDate")]
39+
[JsonConverter(typeof(DateJsonConverter))]
40+
public DateTime StartDate { get; set; }
41+
42+
/// <summary>
43+
/// End date of the requested informations
44+
/// </summary>
45+
[JsonProperty("endDate")]
46+
[JsonConverter(typeof(DateJsonConverter))]
47+
public DateTime EndDate { get; set; }
48+
}
49+
}

WebUntisAPI.Client/WebUntisClient.TimeTable.cs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,172 @@ public async Task<Holidays[]> GetAllHolidaysAsync(string id = "getHolidays", Can
9191
List<Holidays> holidays = await MakeRequestAsync<object, List<Holidays>>(id, "getHolidays", new object(), ct);
9292
return holidays.ToArray();
9393
}
94+
95+
#region Timetable
96+
/// <summary>
97+
/// Get the timetable for a class
98+
/// </summary>
99+
/// <param name="class">The class from the timetable</param>
100+
/// <param name="startDate">Start date of the timetable (default is the current date)</param>
101+
/// <param name="endDate">End date of the timetable (default is the current date)</param>
102+
/// <param name="id">Identier for the request</param>
103+
/// <param name="ct">Cancellation token</param>
104+
/// <returns>The periods for the class</returns>
105+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
106+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
107+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
108+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
109+
public async Task<Period[]> GetTimetableForClassAsync(Class @class, DateTime startDate = default, DateTime endDate = default, string id = "GetTimtableForClass", CancellationToken ct = default)
110+
{
111+
// Check for defaul time
112+
if (startDate == default)
113+
startDate = DateTime.Now;
114+
115+
if (endDate == default)
116+
endDate = DateTime.Now;
117+
118+
TimetableRequestModel requestModel = new TimetableRequestModel()
119+
{
120+
Id = @class.Id,
121+
Type = 1,
122+
StartDate = startDate,
123+
EndDate = endDate
124+
};
125+
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
126+
return timetable.ToArray();
127+
}
128+
129+
/// <summary>
130+
/// Get the timetable for a teacher
131+
/// </summary>
132+
/// <param name="teacher">The teacher from the timetable</param>
133+
/// <param name="startDate">Start date of the timetable (default is the current date)</param>
134+
/// <param name="endDate">End date of the timetable (default is the current date)</param>
135+
/// <param name="id">Identier for the request</param>
136+
/// <param name="ct">Cancellation token</param>
137+
/// <returns>The periods for the teacher</returns>
138+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
139+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
140+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
141+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
142+
public async Task<Period[]> GetTimetableForTeacherAsync(Teacher teacher, DateTime startDate = default, DateTime endDate = default, string id = "GetTimtableForTeacher", CancellationToken ct = default)
143+
{
144+
// Check for defaul time
145+
if (startDate == default)
146+
startDate = DateTime.Now;
147+
148+
if (endDate == default)
149+
endDate = DateTime.Now;
150+
151+
TimetableRequestModel requestModel = new TimetableRequestModel()
152+
{
153+
Id = teacher.Id,
154+
Type = 2,
155+
StartDate = startDate,
156+
EndDate = endDate
157+
};
158+
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
159+
return timetable.ToArray();
160+
}
161+
162+
/// <summary>
163+
/// Get the timetable for a subject
164+
/// </summary>
165+
/// <param name="subject">The subject from the timetable</param>
166+
/// <param name="startDate">Start date of the timetable (default is the current date)</param>
167+
/// <param name="endDate">End date of the timetable (default is the current date)</param>
168+
/// <param name="id">Identier for the request</param>
169+
/// <param name="ct">Cancellation token</param>
170+
/// <returns>The periods for the subject</returns>
171+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
172+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
173+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
174+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
175+
public async Task<Period[]> GetTimetableForSubjectAsync(Subject subject, DateTime startDate = default, DateTime endDate = default, string id = "GetTimtableForSubject", CancellationToken ct = default)
176+
{
177+
// Check for defaul time
178+
if (startDate == default)
179+
startDate = DateTime.Now;
180+
181+
if (endDate == default)
182+
endDate = DateTime.Now;
183+
184+
TimetableRequestModel requestModel = new TimetableRequestModel()
185+
{
186+
Id = subject.Id,
187+
Type = 3,
188+
StartDate = startDate,
189+
EndDate = endDate
190+
};
191+
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
192+
return timetable.ToArray();
193+
}
194+
195+
/// <summary>
196+
/// Get the timetable for a room
197+
/// </summary>
198+
/// <param name="room">The room from the timetable</param>
199+
/// <param name="startDate">Start date of the timetable (default is the current date)</param>
200+
/// <param name="endDate">End date of the timetable (default is the current date)</param>
201+
/// <param name="id">Identier for the request</param>
202+
/// <param name="ct">Cancellation token</param>
203+
/// <returns>The periods for the room</returns>
204+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
205+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
206+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
207+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
208+
public async Task<Period[]> GetTimetableForRoomAsync(Room room, DateTime startDate = default, DateTime endDate = default, string id = "GetTimtableForRoom", CancellationToken ct = default)
209+
{
210+
// Check for defaul time
211+
if (startDate == default)
212+
startDate = DateTime.Now;
213+
214+
if (endDate == default)
215+
endDate = DateTime.Now;
216+
217+
TimetableRequestModel requestModel = new TimetableRequestModel()
218+
{
219+
Id = room.Id,
220+
Type = 4,
221+
StartDate = startDate,
222+
EndDate = endDate
223+
};
224+
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
225+
return timetable.ToArray();
226+
}
227+
228+
/// <summary>
229+
/// Get the timetable for a student
230+
/// </summary>
231+
/// <param name="student">The student from the timetable</param>
232+
/// <param name="startDate">Start date of the timetable (default is the current date)</param>
233+
/// <param name="endDate">End date of the timetable (default is the current date)</param>
234+
/// <param name="id">Identier for the request</param>
235+
/// <param name="ct">Cancellation token</param>
236+
/// <returns>The periods for the student</returns>
237+
/// <exception cref="ObjectDisposedException">Thrown when thew instance was disposed</exception>
238+
/// <exception cref="UnauthorizedAccessException">Thrown when you're not logged in</exception>
239+
/// <exception cref="HttpRequestException">Thrown when an error happend while the http request</exception>
240+
/// <exception cref="WebUntisException">Thrown when the WebUntis API returned an error</exception>
241+
public async Task<Period[]> GetTimetableForStudentAsync(Student student, DateTime startDate = default, DateTime endDate = default, string id = "GetTimtableForStudent", CancellationToken ct = default)
242+
{
243+
// Check for defaul time
244+
if (startDate == default)
245+
startDate = DateTime.Now;
246+
247+
if (endDate == default)
248+
endDate = DateTime.Now;
249+
250+
TimetableRequestModel requestModel = new TimetableRequestModel()
251+
{
252+
Id = student.Id,
253+
Type = 5,
254+
StartDate = startDate,
255+
EndDate = endDate
256+
};
257+
List<Period> timetable = await MakeRequestAsync<TimetableRequestModel, List<Period>>(id, "getTimetable", requestModel, ct);
258+
return timetable.ToArray();
259+
}
260+
#endregion
94261
}
95262
}

0 commit comments

Comments
 (0)