using Fitness_core_BE.Models.IOs; using Fitness_core_BE.Services.ClientInvoice; using Fitness_core_BE.Interfaces.Services; namespace Fitness_core_BE.Services.FunctionHandler { public class ClientInvoiceFunctionHandler { public readonly Dictionary>> _functionHandler; public ClientInvoiceFunctionHandler(IClientInvoiceService clientInvoiceService) { _functionHandler = new() { { "CreateClientInvoiceFromSubscription", clientInvoiceService.CreateClientInvoiceFromSubscription }, { "GetClientInvoiceById", clientInvoiceService.GetClientInvoiceById }, { "GetClientInvoicesByClientId", clientInvoiceService.GetClientInvoicesByClientId }, { "GetClientInvoicesByUserId", clientInvoiceService.GetClientInvoicesByUserId }, { "UpdateClientInvoiceStatus", clientInvoiceService.UpdateClientInvoiceStatus }, { "CalculateClientInvoiceTotal", clientInvoiceService.CalculateClientInvoiceTotal } }; } public async Task> Execute(ApiRequest request, HttpContext httpContext) { if (!_functionHandler.TryGetValue(request.FunctionName, out var handler)) { return new ApiResponse { StatusCode = 404, Message = $"Function '{request.FunctionName}' not found.", Data = null! }; } try { var results = await handler(request.Payload, httpContext); return new ApiResponse { StatusCode = 200, Message = "success", Data = results }; } catch (UnauthorizedAccessException ex) { return new ApiResponse { StatusCode = 401, Message = ex.Message, Data = null! }; } catch (KeyNotFoundException ex) { return new ApiResponse { StatusCode = 400, Message = ex.Message, Data = null! }; } catch (InvalidOperationException ex) { return new ApiResponse { StatusCode = 400, Message = ex.Message, Data = null! }; } catch (Exception ex) { return new ApiResponse { StatusCode = 500, Message = $"An error occurred: {ex.Message}", Data = null! }; } } } }