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;
|
||||
StateHasChanged();
|
||||
|
||||
var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList();
|
||||
var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", shiftsWithTransportList);
|
||||
var request = new { Year = SelectedDate.Year, Month = SelectedDate.Month };
|
||||
var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
|
|
@ -148,8 +148,8 @@ namespace ShiftScheduler.Client.Pages
|
|||
_successMessage = string.Empty;
|
||||
StateHasChanged();
|
||||
|
||||
var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList();
|
||||
var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", shiftsWithTransportList);
|
||||
var request = new { Year = SelectedDate.Year, Month = SelectedDate.Month };
|
||||
var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
|
|
@ -232,11 +232,11 @@ namespace ShiftScheduler.Client.Pages
|
|||
_isSyncing = true;
|
||||
StateHasChanged();
|
||||
|
||||
var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList();
|
||||
var request = new
|
||||
{
|
||||
CalendarId = calendarId,
|
||||
ShiftsWithTransport = shiftsWithTransportList
|
||||
Year = SelectedDate.Year,
|
||||
Month = SelectedDate.Month
|
||||
};
|
||||
|
||||
var response = await HttpClient.PostAsJsonAsync("api/shift/sync_to_google_calendar", request);
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@
|
|||
/* Desktop shift buttons - 3 columns */
|
||||
@media (min-width: 769px) {
|
||||
.shifts {
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -97,15 +97,17 @@ namespace ShiftScheduler.Server.Controllers
|
|||
}
|
||||
|
||||
[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);
|
||||
return File(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar", "schedule.ics");
|
||||
}
|
||||
|
||||
[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);
|
||||
return File(pdf, "application/pdf", "schedule.pdf");
|
||||
}
|
||||
|
|
@ -171,7 +173,8 @@ namespace ShiftScheduler.Server.Controllers
|
|||
{
|
||||
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!" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -179,6 +182,61 @@ namespace ShiftScheduler.Server.Controllers
|
|||
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
|
||||
|
|
@ -194,9 +252,16 @@ namespace ShiftScheduler.Server.Controllers
|
|||
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 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