diff --git a/Client/Pages/Index.razor.cs b/Client/Pages/Index.razor.cs index 5868049..8b95a79 100644 --- a/Client/Pages/Index.razor.cs +++ b/Client/Pages/Index.razor.cs @@ -91,7 +91,7 @@ namespace ShiftScheduler.Client.Pages private async Task ExportToIcs() { var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList(); - var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics_with_transport", shiftsWithTransportList); + var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", shiftsWithTransportList); var bytes = await response.Content.ReadAsByteArrayAsync(); var base64 = Convert.ToBase64String(bytes); var fileUrl = $"data:text/calendar;base64,{base64}"; @@ -101,7 +101,7 @@ namespace ShiftScheduler.Client.Pages private async Task ExportToPdf() { var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList(); - var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf_with_transport", shiftsWithTransportList); + var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", shiftsWithTransportList); var bytes = await response.Content.ReadAsByteArrayAsync(); var base64 = Convert.ToBase64String(bytes); var fileUrl = $"data:application/octet-stream;base64,{base64}"; diff --git a/Server/Controllers/ShiftController.cs b/Server/Controllers/ShiftController.cs index 32808f6..5c8ca09 100644 --- a/Server/Controllers/ShiftController.cs +++ b/Server/Controllers/ShiftController.cs @@ -102,31 +102,13 @@ namespace ShiftScheduler.Server.Controllers } [HttpPost("export_ics")] - public async Task ExportIcs([FromBody] Dictionary schedule) - { - var shifts = _shiftService.GetShifts(); - var shiftTransports = await _enrichmentService.EnrichShiftsWithTransportAsync(shifts, schedule); - var ics = _icsService.GenerateIcs(schedule, shifts, shiftTransports); - return File(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar", "schedule.ics"); - } - - [HttpPost("export_ics_with_transport")] public IActionResult ExportIcsWithTransport([FromBody] List shiftsWithTransport) { var ics = _icsService.GenerateIcs(shiftsWithTransport); return File(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar", "schedule.ics"); } - [HttpPost("export_pdf")] - public async Task ExportPdf([FromBody] Dictionary schedule) - { - var shifts = _shiftService.GetShifts(); - var shiftTransports = await _enrichmentService.EnrichShiftsWithTransportAsync(shifts, schedule); - var pdf = _pdfExportService.GenerateMonthlySchedulePdf(schedule, shifts, shiftTransports); - return File(pdf, "application/pdf", "schedule.pdf"); - } - - [HttpPost("export_pdf_with_transport")] + [HttpPost("export_pdf")] public IActionResult ExportPdfWithTransport([FromBody] List shiftsWithTransport) { var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport); diff --git a/Services/IcsExportService.cs b/Services/IcsExportService.cs index e6b7a7a..98217f9 100644 --- a/Services/IcsExportService.cs +++ b/Services/IcsExportService.cs @@ -8,66 +8,6 @@ namespace ShiftScheduler.Services { public class IcsExportService { - public string GenerateIcs(Dictionary schedule, List shifts, List? shiftTransports = null) - { - var calendar = new Calendar(); - - foreach (var kvp in schedule) - { - var date = kvp.Key; - var shiftName = kvp.Value; - var shift = shifts.FirstOrDefault(s => s.Name == shiftName); - var transport = shiftTransports?.FirstOrDefault(t => t.ShiftName == shiftName); - - if (shift == null || (string.IsNullOrEmpty(shift.MorningTime) && string.IsNullOrEmpty(shift.AfternoonTime))) - continue; - - if (!string.IsNullOrEmpty(shift.MorningTime)) - { - var times = shift.MorningTime.Split('-'); - var summary = $"{shift.Name} (Morning)"; - var description = ""; - - if (transport?.MorningTransport != null && !string.IsNullOrEmpty(transport.MorningTransport.DepartureTime)) - { - var transportSummary = FormatTransportInfo(transport.MorningTransport); - description = $"Transport: {transportSummary}"; - } - - calendar.Events.Add(new CalendarEvent - { - Summary = summary, - Description = description, - Start = new CalDateTime(date.Add(TimeSpan.Parse(times[0]))), - End = new CalDateTime(date.Add(TimeSpan.Parse(times[1]))) - }); - } - - if (!string.IsNullOrEmpty(shift.AfternoonTime)) - { - var times = shift.AfternoonTime.Split('-'); - var summary = $"{shift.Name} (Afternoon)"; - var description = ""; - - if (transport?.AfternoonTransport != null && !string.IsNullOrEmpty(transport.AfternoonTransport.DepartureTime)) - { - var transportSummary = FormatTransportInfo(transport.AfternoonTransport); - description = $"Transport: {transportSummary}"; - } - - calendar.Events.Add(new CalendarEvent - { - Summary = summary, - Description = description, - Start = new CalDateTime(date.Add(TimeSpan.Parse(times[0]))), - End = new CalDateTime(date.Add(TimeSpan.Parse(times[1]))) - }); - } - } - - return new CalendarSerializer().SerializeToString(calendar); - } - public string GenerateIcs(List shiftsWithTransport) { var calendar = new Calendar(); diff --git a/Services/PdfExportService.cs b/Services/PdfExportService.cs index 511d9be..607fabe 100644 --- a/Services/PdfExportService.cs +++ b/Services/PdfExportService.cs @@ -16,30 +16,6 @@ namespace ShiftScheduler.Services QuestPDF.Settings.License = LicenseType.Community; } - public byte[] GenerateMonthlySchedulePdf(Dictionary schedule, IReadOnlyList shifts, IReadOnlyList? shiftTransports = null) - { - var year = schedule.First().Key.Year; - var month = schedule.First().Key.Month; - - var document = Document.Create(container => - { - container.Page(page => - { - page.Margin(30); - page.Size(PageSizes.A4.Landscape()); - page.DefaultTextStyle(x => x.FontSize(10)); - - page.Header() - .Text($"Shift Schedule for {CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month)} {year}") - .FontSize(16).Bold().AlignCenter(); - - page.Content().Element(e => ComposeTable(e, schedule, shifts, shiftTransports)); - }); - }); - - return document.GeneratePdf(); - } - public byte[] GenerateMonthlySchedulePdf(List shiftsWithTransport) { if (!shiftsWithTransport.Any()) @@ -66,38 +42,6 @@ namespace ShiftScheduler.Services return document.GeneratePdf(); } - - void ComposeTable(IContainer container, IReadOnlyDictionary schedule, IReadOnlyList shifts, IReadOnlyList? shiftTransports) - { - container.Table(table => - { - table.ColumnsDefinition(columns => - { - columns.RelativeColumn(); - columns.RelativeColumn(); - columns.RelativeColumn(); - columns.RelativeColumn(); - columns.RelativeColumn(); - columns.RelativeColumn(); - columns.RelativeColumn(); - }); - - var year = schedule.First().Key.Year; - var month = schedule.First().Key.Month; - var daysInMonth = DateTime.DaysInMonth(year, month); - var monthDates = Enumerable.Range(1, daysInMonth) - .Select(day => new DateTime(year, month, day)) - .ToList(); - var weeks = monthDates - .GroupBy(d => CultureInfo.InvariantCulture.Calendar.GetWeekOfYear( - d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)) - .ToList(); - foreach (var week in weeks) - { - RenderShiftForWeek(table, week, schedule, shifts, shiftTransports); - } - }); - } void ComposeTable(IContainer container, List shiftsWithTransport) { @@ -132,74 +76,6 @@ namespace ShiftScheduler.Services }); } - private static void RenderShiftForWeek(TableDescriptor table, IGrouping week, - IReadOnlyDictionary schedule, IReadOnlyList shifts, IReadOnlyList? shiftTransports) - { - var weekDates = week.ToList(); - var dayInWeek = weekDates.Count; - foreach (var day in Enumerable.Range(0, dayInWeek)) - { - var dddd = weekDates.ElementAtOrDefault(day).ToString("dd.MM.yyyy (ddd)"); - table.Cell().Element(CellStyleFirst).Text(dddd); - } - foreach (var day in Enumerable.Range(dayInWeek, 7 - dayInWeek)) - { - RenderEmptyCell(table); - } - - foreach (var day in Enumerable.Range(0, dayInWeek)) - { - var shiftName = shifts.Single(s => s.Name == schedule[weekDates.ElementAtOrDefault(day)]).Name; - table.Cell().Element(CellStyleMiddle).Text(shiftName); - } - foreach (var day in Enumerable.Range(dayInWeek, 7 - dayInWeek)) - { - RenderEmptyCell(table); - } - - foreach (var day in Enumerable.Range(0, dayInWeek)) - { - var morningTime = shifts.Single(s => s.Name == schedule[weekDates.ElementAtOrDefault(day)]).MorningTime; - table.Cell().Element(CellStyleMiddle).Text(morningTime); - } - foreach (var day in Enumerable.Range(dayInWeek, 7 - dayInWeek)) - { - RenderEmptyCell(table); - } - - foreach (var day in Enumerable.Range(0, dayInWeek)) - { - var afternoonTime = shifts.Single(s => s.Name == schedule[weekDates.ElementAtOrDefault(day)]).AfternoonTime; - table.Cell().Element(CellStyleMiddle).Text(afternoonTime); - } - foreach (var day in Enumerable.Range(dayInWeek, 7 - dayInWeek)) - { - RenderEmptyCell(table); - } - - foreach (var day in Enumerable.Range(0, dayInWeek)) - { - var icon = shifts.Single(s => s.Name == schedule[weekDates.ElementAtOrDefault(day)]).Icon; - table.Cell().Element(CellStyleMiddle).Text(icon); - } - foreach (var day in Enumerable.Range(dayInWeek, 7 - dayInWeek)) - { - RenderEmptyCell(table); - } - - foreach (var day in Enumerable.Range(0, dayInWeek)) - { - var shiftName = shifts.Single(s => s.Name == schedule[weekDates.ElementAtOrDefault(day)]).Name; - var transport = shiftTransports?.FirstOrDefault(t => t.ShiftName == shiftName); - var transportInfo = GetTransportSummary(transport); - table.Cell().Element(CellStyleLast).Text(transportInfo); - } - foreach (var day in Enumerable.Range(dayInWeek, 7 - dayInWeek)) - { - RenderEmptyCell(table); - } - } - private static void RenderShiftForWeek(TableDescriptor table, IGrouping week, IReadOnlyDictionary shiftsWithTransport) { @@ -271,25 +147,6 @@ namespace ShiftScheduler.Services } } - private static string GetTransportSummary(ShiftTransport? transport) - { - var transportLines = new List(); - - if (transport?.MorningTransport != null && !string.IsNullOrEmpty(transport.MorningTransport.DepartureTime)) - { - var morningInfo = FormatTransportConnection(transport.MorningTransport, "Morning"); - transportLines.Add(morningInfo); - } - - if (transport?.AfternoonTransport != null && !string.IsNullOrEmpty(transport.AfternoonTransport.DepartureTime)) - { - var afternoonInfo = FormatTransportConnection(transport.AfternoonTransport, "Afternoon"); - transportLines.Add(afternoonInfo); - } - - return transportLines.Count > 0 ? string.Join("\n", transportLines) : "-"; - } - private static string GetTransportSummary(ShiftWithTransport? shiftWithTransport) { if (shiftWithTransport == null) return "-";