using Fluxor; using Koogle.Application.Interfaces; namespace Koogle.Web.Store.DayState; /// /// Side effects for day state management. /// Handles async operations like API calls. /// public class DayEffects { private readonly IDayService _dayService; private readonly IPersonService _personService; private readonly ILogger _logger; /// /// Initializes a new instance of the DayEffects class. /// public DayEffects(IDayService dayService, IPersonService personService, ILogger logger) { _dayService = dayService; _personService = personService; _logger = logger; } /// /// Handles LoadDaysAction - loads all days from service. /// [EffectMethod] public async Task HandleLoadDays(LoadDaysAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Loading days with filter {@Filter}", action.Filter); var result = await _dayService.GetAllAsync(action.Filter); dispatcher.Dispatch(new LoadDaysSuccessAction(result.Items)); _logger.LogInformation("Loaded {Count} days", result.Items.Count); } catch (Exception ex) { _logger.LogError(ex, "Failed to load days"); dispatcher.Dispatch(new LoadDaysFailureAction(ex.Message)); } } /// /// Handles LoadDayDetailsAction - loads day details from service. /// [EffectMethod] public async Task HandleLoadDayDetails(LoadDayDetailsAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Loading day details for {DayId}", action.DayId); var day = await _dayService.GetByIdAsync(action.DayId); if (day is null) { dispatcher.Dispatch(new LoadDayDetailsFailureAction("Spieltag nicht gefunden.")); return; } dispatcher.Dispatch(new LoadDayDetailsSuccessAction(day)); _logger.LogInformation("Loaded day details for {DayId}", action.DayId); } catch (Exception ex) { _logger.LogError(ex, "Failed to load day details for {DayId}", action.DayId); dispatcher.Dispatch(new LoadDayDetailsFailureAction(ex.Message)); } } /// /// Handles CreateDayAction - creates a new day. /// [EffectMethod] public async Task HandleCreateDay(CreateDayAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Creating day for {PostDate}", action.Dto.PostDate); var day = await _dayService.CreateAsync(action.Dto); dispatcher.Dispatch(new CreateDaySuccessAction(day)); _logger.LogInformation("Created day {Id} for {PostDate}", day.Id, day.PostDate); } catch (Exception ex) { _logger.LogError(ex, "Failed to create day for {PostDate}", action.Dto.PostDate); dispatcher.Dispatch(new CreateDayFailureAction(ex.Message)); } } /// /// Handles UpdateDayAction - updates an existing day. /// [EffectMethod] public async Task HandleUpdateDay(UpdateDayAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Updating day {Id}", action.Dto.Id); var day = await _dayService.UpdateAsync(action.Dto); dispatcher.Dispatch(new UpdateDaySuccessAction(day)); _logger.LogInformation("Updated day {Id}", day.Id); } catch (Exception ex) { _logger.LogError(ex, "Failed to update day {Id}", action.Dto.Id); dispatcher.Dispatch(new UpdateDayFailureAction(ex.Message)); } } /// /// Handles DeleteDayAction - deletes a day. /// [EffectMethod] public async Task HandleDeleteDay(DeleteDayAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Deleting day {Id}", action.Id); var success = await _dayService.DeleteAsync(action.Id); if (success) { dispatcher.Dispatch(new DeleteDaySuccessAction(action.Id)); _logger.LogInformation("Deleted day {Id}", action.Id); } else { dispatcher.Dispatch(new DeleteDayFailureAction("Spieltag konnte nicht gelöscht werden.")); _logger.LogWarning("Failed to delete day {Id} - not found or already deleted", action.Id); } } catch (Exception ex) { _logger.LogError(ex, "Failed to delete day {Id}", action.Id); dispatcher.Dispatch(new DeleteDayFailureAction(ex.Message)); } } /// /// Handles AddDayParticipantAction - adds participant to day. /// [EffectMethod] public async Task HandleAddDayParticipant(AddDayParticipantAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Adding participant {PersonId} to day {DayId}", action.Dto.PersonId, action.Dto.DayId); var day = await _dayService.AddParticipantAsync(action.Dto); dispatcher.Dispatch(new AddDayParticipantSuccessAction(day)); _logger.LogInformation("Added participant {PersonId} to day {DayId}", action.Dto.PersonId, action.Dto.DayId); } catch (Exception ex) { _logger.LogError(ex, "Failed to add participant {PersonId} to day {DayId}", action.Dto.PersonId, action.Dto.DayId); dispatcher.Dispatch(new AddDayParticipantFailureAction(ex.Message)); } } /// /// Handles RemoveDayParticipantAction - removes participant from day. /// [EffectMethod] public async Task HandleRemoveDayParticipant(RemoveDayParticipantAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Removing participant {PersonId} from day {DayId}", action.Dto.PersonId, action.Dto.DayId); var day = await _dayService.RemoveParticipantAsync(action.Dto); dispatcher.Dispatch(new RemoveDayParticipantSuccessAction(day)); _logger.LogInformation("Removed participant {PersonId} from day {DayId}", action.Dto.PersonId, action.Dto.DayId); } catch (Exception ex) { _logger.LogError(ex, "Failed to remove participant {PersonId} from day {DayId}", action.Dto.PersonId, action.Dto.DayId); dispatcher.Dispatch(new RemoveDayParticipantFailureAction(ex.Message)); } } /// /// Handles AdvanceDayStatusAction - advances day status. /// [EffectMethod] public async Task HandleAdvanceDayStatus(AdvanceDayStatusAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Advancing status for day {DayId}", action.DayId); var day = await _dayService.AdvanceStatusAsync(action.DayId); dispatcher.Dispatch(new AdvanceDayStatusSuccessAction(day)); _logger.LogInformation("Advanced status for day {DayId} to {Status}", action.DayId, day.Status); } catch (Exception ex) { _logger.LogError(ex, "Failed to advance status for day {DayId}", action.DayId); dispatcher.Dispatch(new AdvanceDayStatusFailureAction(ex.Message)); } } /// /// Handles LoadAvailablePersonsAction - loads all persons for selection. /// [EffectMethod] public async Task HandleLoadAvailablePersons(LoadAvailablePersonsAction action, IDispatcher dispatcher) { try { _logger.LogDebug("Loading available persons"); var persons = await _personService.GetAllAsync(); dispatcher.Dispatch(new LoadAvailablePersonsSuccessAction(persons)); _logger.LogInformation("Loaded {Count} available persons", persons.Count); } catch (Exception ex) { _logger.LogError(ex, "Failed to load available persons"); dispatcher.Dispatch(new LoadAvailablePersonsFailureAction(ex.Message)); } } }