Code cleanup
This commit is contained in:
parent
abc26066ec
commit
e7621e462e
4 changed files with 3 additions and 224 deletions
|
|
@ -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}";
|
||||
|
|
|
|||
|
|
@ -102,15 +102,6 @@ namespace ShiftScheduler.Server.Controllers
|
|||
}
|
||||
|
||||
[HttpPost("export_ics")]
|
||||
public async Task<IActionResult> ExportIcs([FromBody] Dictionary<DateTime, string> 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<ShiftWithTransport> shiftsWithTransport)
|
||||
{
|
||||
var ics = _icsService.GenerateIcs(shiftsWithTransport);
|
||||
|
|
@ -118,15 +109,6 @@ namespace ShiftScheduler.Server.Controllers
|
|||
}
|
||||
|
||||
[HttpPost("export_pdf")]
|
||||
public async Task<IActionResult> ExportPdf([FromBody] Dictionary<DateTime, string> 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")]
|
||||
public IActionResult ExportPdfWithTransport([FromBody] List<ShiftWithTransport> shiftsWithTransport)
|
||||
{
|
||||
var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport);
|
||||
|
|
|
|||
|
|
@ -8,66 +8,6 @@ namespace ShiftScheduler.Services
|
|||
{
|
||||
public class IcsExportService
|
||||
{
|
||||
public string GenerateIcs(Dictionary<DateTime, string> schedule, List<Shift> shifts, List<ShiftTransport>? 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<ShiftWithTransport> shiftsWithTransport)
|
||||
{
|
||||
var calendar = new Calendar();
|
||||
|
|
|
|||
|
|
@ -16,30 +16,6 @@ namespace ShiftScheduler.Services
|
|||
QuestPDF.Settings.License = LicenseType.Community;
|
||||
}
|
||||
|
||||
public byte[] GenerateMonthlySchedulePdf(Dictionary<DateTime, string> schedule, IReadOnlyList<Shift> shifts, IReadOnlyList<ShiftTransport>? 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<ShiftWithTransport> shiftsWithTransport)
|
||||
{
|
||||
if (!shiftsWithTransport.Any())
|
||||
|
|
@ -67,38 +43,6 @@ namespace ShiftScheduler.Services
|
|||
return document.GeneratePdf();
|
||||
}
|
||||
|
||||
void ComposeTable(IContainer container, IReadOnlyDictionary<DateTime, string> schedule, IReadOnlyList<Shift> shifts, IReadOnlyList<ShiftTransport>? 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<ShiftWithTransport> shiftsWithTransport)
|
||||
{
|
||||
container.Table(table =>
|
||||
|
|
@ -132,74 +76,6 @@ namespace ShiftScheduler.Services
|
|||
});
|
||||
}
|
||||
|
||||
private static void RenderShiftForWeek(TableDescriptor table, IGrouping<int, DateTime> week,
|
||||
IReadOnlyDictionary<DateTime, string> schedule, IReadOnlyList<Shift> shifts, IReadOnlyList<ShiftTransport>? 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<int, DateTime> week,
|
||||
IReadOnlyDictionary<DateTime, ShiftWithTransport> shiftsWithTransport)
|
||||
{
|
||||
|
|
@ -271,25 +147,6 @@ namespace ShiftScheduler.Services
|
|||
}
|
||||
}
|
||||
|
||||
private static string GetTransportSummary(ShiftTransport? transport)
|
||||
{
|
||||
var transportLines = new List<string>();
|
||||
|
||||
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 "-";
|
||||
|
|
|
|||
Loading…
Reference in a new issue