Refactor export API calls to pass only month/year instead of full data (#56)
* Initial plan * Implement cleaner API calls for export methods Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com> * 4 icons per row --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com> Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
This commit is contained in:
parent
f2b0051183
commit
252e588375
3 changed files with 76 additions and 11 deletions
|
|
@ -115,8 +115,8 @@ namespace ShiftScheduler.Client.Pages
|
||||||
_successMessage = string.Empty;
|
_successMessage = string.Empty;
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList();
|
var request = new { Year = SelectedDate.Year, Month = SelectedDate.Month };
|
||||||
var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", shiftsWithTransportList);
|
var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", request);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
|
|
@ -148,8 +148,8 @@ namespace ShiftScheduler.Client.Pages
|
||||||
_successMessage = string.Empty;
|
_successMessage = string.Empty;
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList();
|
var request = new { Year = SelectedDate.Year, Month = SelectedDate.Month };
|
||||||
var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", shiftsWithTransportList);
|
var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", request);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
|
|
@ -232,11 +232,11 @@ namespace ShiftScheduler.Client.Pages
|
||||||
_isSyncing = true;
|
_isSyncing = true;
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList();
|
|
||||||
var request = new
|
var request = new
|
||||||
{
|
{
|
||||||
CalendarId = calendarId,
|
CalendarId = calendarId,
|
||||||
ShiftsWithTransport = shiftsWithTransportList
|
Year = SelectedDate.Year,
|
||||||
|
Month = SelectedDate.Month
|
||||||
};
|
};
|
||||||
|
|
||||||
var response = await HttpClient.PostAsJsonAsync("api/shift/sync_to_google_calendar", request);
|
var response = await HttpClient.PostAsJsonAsync("api/shift/sync_to_google_calendar", request);
|
||||||
|
|
|
||||||
|
|
@ -169,7 +169,7 @@
|
||||||
/* Desktop shift buttons - 3 columns */
|
/* Desktop shift buttons - 3 columns */
|
||||||
@media (min-width: 769px) {
|
@media (min-width: 769px) {
|
||||||
.shifts {
|
.shifts {
|
||||||
grid-template-columns: repeat(3, 1fr);
|
grid-template-columns: repeat(4, 1fr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -97,15 +97,17 @@ namespace ShiftScheduler.Server.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("export_ics")]
|
[HttpPost("export_ics")]
|
||||||
public IActionResult ExportIcsWithTransport([FromBody] List<ShiftWithTransport> shiftsWithTransport)
|
public async Task<IActionResult> ExportIcs([FromBody] ExportRequest request)
|
||||||
{
|
{
|
||||||
|
var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month);
|
||||||
var ics = _icsService.GenerateIcs(shiftsWithTransport);
|
var ics = _icsService.GenerateIcs(shiftsWithTransport);
|
||||||
return File(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar", "schedule.ics");
|
return File(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar", "schedule.ics");
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("export_pdf")]
|
[HttpPost("export_pdf")]
|
||||||
public IActionResult ExportPdfWithTransport([FromBody] List<ShiftWithTransport> shiftsWithTransport)
|
public async Task<IActionResult> ExportPdf([FromBody] ExportRequest request)
|
||||||
{
|
{
|
||||||
|
var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month);
|
||||||
var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport);
|
var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport);
|
||||||
return File(pdf, "application/pdf", "schedule.pdf");
|
return File(pdf, "application/pdf", "schedule.pdf");
|
||||||
}
|
}
|
||||||
|
|
@ -171,7 +173,8 @@ namespace ShiftScheduler.Server.Controllers
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _googleCalendarService.SyncShiftsToCalendarAsync(request.CalendarId, request.ShiftsWithTransport);
|
var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month);
|
||||||
|
await _googleCalendarService.SyncShiftsToCalendarAsync(request.CalendarId, shiftsWithTransport);
|
||||||
return Ok(new { message = "Shifts synced to Google Calendar successfully!" });
|
return Ok(new { message = "Shifts synced to Google Calendar successfully!" });
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -179,6 +182,61 @@ namespace ShiftScheduler.Server.Controllers
|
||||||
return StatusCode(500, $"Error syncing to Google Calendar: {ex.Message}");
|
return StatusCode(500, $"Error syncing to Google Calendar: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<List<ShiftWithTransport>> BuildShiftsWithTransportAsync(int year, int month)
|
||||||
|
{
|
||||||
|
var schedule = await _configurationService.LoadScheduleAsync(year, month);
|
||||||
|
var shiftsWithTransport = new List<ShiftWithTransport>();
|
||||||
|
var transportConfig = _configurationService.GetTransportConfiguration();
|
||||||
|
|
||||||
|
foreach (var kvp in schedule)
|
||||||
|
{
|
||||||
|
var date = kvp.Key;
|
||||||
|
var shiftName = kvp.Value;
|
||||||
|
|
||||||
|
var shift = _configurationService.GetShifts().FirstOrDefault(s => s.Name == shiftName);
|
||||||
|
if (shift == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var shiftTimes = _configurationService.ParseShiftTimes(date, shift);
|
||||||
|
TransportConnection? morningTransport = null;
|
||||||
|
TransportConnection? afternoonTransport = null;
|
||||||
|
|
||||||
|
// Get transport for morning shift if it has morning time
|
||||||
|
if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningStart.HasValue)
|
||||||
|
{
|
||||||
|
morningTransport = await _transportService.GetConnectionAsync(shiftTimes.MorningStart.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get transport for afternoon shift if it has afternoon time
|
||||||
|
if (!string.IsNullOrEmpty(shift.AfternoonTime))
|
||||||
|
{
|
||||||
|
var shouldLoadAfternoonTransport = true;
|
||||||
|
|
||||||
|
// If both morning and afternoon shifts exist, check break duration
|
||||||
|
if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningEnd.HasValue && shiftTimes.AfternoonStart.HasValue)
|
||||||
|
{
|
||||||
|
var breakDurationMinutes = (shiftTimes.AfternoonStart.Value - shiftTimes.MorningEnd.Value).TotalMinutes;
|
||||||
|
shouldLoadAfternoonTransport = breakDurationMinutes >= transportConfig.MinBreakMinutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldLoadAfternoonTransport && shiftTimes.AfternoonStart.HasValue)
|
||||||
|
{
|
||||||
|
afternoonTransport = await _transportService.GetConnectionAsync(shiftTimes.AfternoonStart.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shiftsWithTransport.Add(new ShiftWithTransport
|
||||||
|
{
|
||||||
|
Date = date,
|
||||||
|
Shift = shift,
|
||||||
|
MorningTransport = morningTransport,
|
||||||
|
AfternoonTransport = afternoonTransport
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return shiftsWithTransport;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ShiftTransportRequest
|
public class ShiftTransportRequest
|
||||||
|
|
@ -194,9 +252,16 @@ namespace ShiftScheduler.Server.Controllers
|
||||||
public Dictionary<DateTime, string> Schedule { get; set; } = new();
|
public Dictionary<DateTime, string> Schedule { get; set; } = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ExportRequest
|
||||||
|
{
|
||||||
|
public int Year { get; set; }
|
||||||
|
public int Month { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class SyncToGoogleCalendarRequest
|
public class SyncToGoogleCalendarRequest
|
||||||
{
|
{
|
||||||
public string CalendarId { get; set; } = string.Empty;
|
public string CalendarId { get; set; } = string.Empty;
|
||||||
public List<ShiftWithTransport> ShiftsWithTransport { get; set; } = [];
|
public int Year { get; set; }
|
||||||
|
public int Month { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue