Compare commits

..

2 Commits

Author SHA1 Message Date
7b97640ce5 Добавил больше информации в ICS
All checks were successful
Create and publish a Docker image / Publish image (push) Successful in 7m9s
2025-09-25 18:34:21 +03:00
03ee564dc3 Небольшой рефактор 2025-09-25 18:23:34 +03:00

View File

@@ -25,8 +25,10 @@ namespace SfeduSchedule.Services
public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr) public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr)
{ {
var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}"); var request = new HttpRequestMessage(HttpMethod.Post,
request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalVariables.jsonSerializerOptions), System.Text.Encoding.UTF8, "application/json"); $"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}");
request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalVariables.jsonSerializerOptions),
System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.SendAsync(request); var response = await _httpClient.SendAsync(request);
_logger.LogInformation("GetScheduleAsync: Ответ получен: {StatusCode}", response.StatusCode); _logger.LogInformation("GetScheduleAsync: Ответ получен: {StatusCode}", response.StatusCode);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
@@ -36,7 +38,9 @@ namespace SfeduSchedule.Services
public async Task<string?> SearchRoomsAsync(RoomSearchRequest requestDto) public async Task<string?> SearchRoomsAsync(RoomSearchRequest requestDto)
{ {
var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/campus/rooms/search"); var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/campus/rooms/search");
request.Content = new StringContent(JsonSerializer.Serialize(requestDto, GlobalVariables.jsonSerializerOptions), System.Text.Encoding.UTF8, "application/json"); request.Content =
new StringContent(JsonSerializer.Serialize(requestDto, GlobalVariables.jsonSerializerOptions),
System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.SendAsync(request); var response = await _httpClient.SendAsync(request);
_logger.LogInformation("SearchRoomsAsync: Ответ получен: {StatusCode}", response.StatusCode); _logger.LogInformation("SearchRoomsAsync: Ответ получен: {StatusCode}", response.StatusCode);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
@@ -71,13 +75,15 @@ namespace SfeduSchedule.Services
} }
catch catch
{ {
_logger.LogWarning("GetGuidAsync: Не удалось получить идентификатор пользователя, {FullName}, json: {Json}", fullName, json); _logger.LogWarning(
"GetGuidAsync: Не удалось получить идентификатор пользователя, {FullName}, json: {Json}", fullName,
json);
return null; return null;
} }
return personId; return personId;
} }
public async Task<string?> GetIcsAsync(ModeusScheduleRequest msr) public async Task<string?> GetIcsAsync(ModeusScheduleRequest msr)
{ {
var schedule = await GetScheduleAsync(msr); var schedule = await GetScheduleAsync(msr);
@@ -103,11 +109,14 @@ namespace SfeduSchedule.Services
if (scheduleJson == null) if (scheduleJson == null)
_logger.LogError("GetIcsAsync: scheduleJson is null. Schedule: {Schedule}", schedule); _logger.LogError("GetIcsAsync: scheduleJson is null. Schedule: {Schedule}", schedule);
else if (scheduleJson.Embedded == null) else if (scheduleJson.Embedded == null)
_logger.LogError("GetIcsAsync: scheduleJson.Embedded is null. scheduleJson: {@scheduleJson}", scheduleJson); _logger.LogError("GetIcsAsync: scheduleJson.Embedded is null. scheduleJson: {@scheduleJson}",
scheduleJson);
else if (scheduleJson.Embedded.Events == null) else if (scheduleJson.Embedded.Events == null)
_logger.LogError("GetIcsAsync: scheduleJson.Embedded.Events is null. Embedded: {@Embedded}", scheduleJson.Embedded); _logger.LogError("GetIcsAsync: scheduleJson.Embedded.Events is null. Embedded: {@Embedded}",
scheduleJson.Embedded);
else else
_logger.LogWarning("GetIcsAsync: scheduleJson.Embedded.Events is empty. Embedded: {@Embedded}", scheduleJson.Embedded); _logger.LogWarning("GetIcsAsync: scheduleJson.Embedded.Events is empty. Embedded: {@Embedded}",
scheduleJson.Embedded);
return null; return null;
} }
@@ -116,20 +125,112 @@ namespace SfeduSchedule.Services
foreach (var e in scheduleJson.Embedded.Events) foreach (var e in scheduleJson.Embedded.Events)
{ {
// Получение названия аудитории для события
string? roomName = null;
if (scheduleJson.Embedded.EventLocations != null && scheduleJson.Embedded.Rooms != null &&
scheduleJson.Embedded.EventRooms != null)
{
var eventLocation = scheduleJson.Embedded.EventLocations.FirstOrDefault(el => el.EventId == e.Id);
if (eventLocation != null
&& eventLocation.Links != null
&& eventLocation.Links.EventRooms != null
&& eventLocation.Links.EventRooms.Href != null)
{
var eventRoomId = eventLocation.Links.EventRooms.Href.Split('/').Last();
var EventRoom =
scheduleJson.Embedded.EventRooms.FirstOrDefault(er =>
er.Id.ToString().ToLower() == eventRoomId);
if (EventRoom != null)
{
var roomId = EventRoom.Links.Room.Href.Split('/').Last();
var room = scheduleJson.Embedded.Rooms.FirstOrDefault(r =>
r.Id.ToString().ToLower() == roomId);
if (room != null)
roomName = room.Name;
}
}
}
// Получение преподавателей для события
string teachersNames = "";
if (scheduleJson.Embedded.EventOrganizers != null && scheduleJson.Embedded.EventAttendees != null &&
scheduleJson.Embedded.Persons != null)
{
// Получаем eventOrganizer
var eventOrganizers =
scheduleJson.Embedded.EventOrganizers.FirstOrDefault(eo => eo.EventId == e.Id);
if (eventOrganizers != null &&
eventOrganizers.Links.EventAttendees != null)
{
// Получаем eventAttendee id
// Тут может прийти массив или 1 объект
Self[] eventAttendeeIds = Array.Empty<Self>();
if (eventOrganizers.Links.EventAttendees.Value.Self != null)
eventAttendeeIds = new[] { eventOrganizers.Links.EventAttendees.Value.Self };
else if (eventOrganizers.Links.EventAttendees.Value.SelfArray != null)
eventAttendeeIds = eventOrganizers.Links.EventAttendees.Value.SelfArray;
if (eventAttendeeIds.Length > 0)
{
foreach (var eventAttendeeId in eventAttendeeIds)
{
var attendeeId = eventAttendeeId.Href.Split('/').Last();
// Получаем eventAttendee
var eventAttendee =
scheduleJson.Embedded.EventAttendees.FirstOrDefault(ea =>
ea.Id.ToString().ToLower() == attendeeId);
if (eventAttendee != null)
{
var personId = eventAttendee.Links.Person.Href.Split('/').Last();
// Получаем person
var teacher = scheduleJson.Embedded.Persons.FirstOrDefault(p =>
p.Id.ToString().ToLower() == personId);
if (teacher != null)
teachersNames += (string.IsNullOrEmpty(teachersNames) ? "" : ", ") +
teacher.FullName;
}
}
}
}
}
// Получение короткого названия для события
string shortNameCourse = "";
if (scheduleJson.Embedded.CourseUnitRealizations != null)
{
try
{
var courseUnitRealizationsLinks = e.Links["course-unit-realization"];
var courseUnitRealizationId = courseUnitRealizationsLinks.Href.Split('/').Last();
if (!string.IsNullOrEmpty(courseUnitRealizationId))
{
var courseUnitRealization = scheduleJson.Embedded.CourseUnitRealizations
.FirstOrDefault(cu => cu.Id.ToString().ToLower() == courseUnitRealizationId);
if (courseUnitRealization != null)
shortNameCourse = courseUnitRealization.NameShort ?? "";
}
}
catch (Exception ex)
{
// Ignored
}
}
calendar.Events.Add(new CalendarEvent calendar.Events.Add(new CalendarEvent
{ {
Summary = e.Name, Summary = (string.IsNullOrEmpty(shortNameCourse) ? "" : shortNameCourse + " / ") + e.Name,
Description = e.NameShort, Description = e.NameShort + (string.IsNullOrEmpty(roomName) ? "" : $"\nАудитория: {roomName}") +
(string.IsNullOrEmpty(teachersNames) ? "" : $"\nПреподаватели: {teachersNames}"),
Start = new CalDateTime(e.StartsAtLocal, _configuration["TZ"]!), Start = new CalDateTime(e.StartsAtLocal, _configuration["TZ"]!),
End = new CalDateTime(e.EndsAtLocal, _configuration["TZ"]!), End = new CalDateTime(e.EndsAtLocal, _configuration["TZ"]!),
}); });
} }
var serializer = new CalendarSerializer(); var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar); var serializedCalendar = serializer.SerializeToString(calendar);
_logger.LogInformation("GetIcsAsync: Serialized calendar created. Length: {Length}", serializedCalendar?.Length ?? 0); _logger.LogInformation("GetIcsAsync: Serialized calendar created. Length: {Length}",
serializedCalendar?.Length ?? 0);
return serializedCalendar; return serializedCalendar;
} }
} }
} }