using Fitness_core_BE.Models.IOs; using Fitness_core_BE.Interfaces.Services; using Fitness_core_BE.Exceptions; namespace Fitness_core_BE.Services.FunctionHandler { public class ClientStatFuncHandler { public readonly Dictionary>> _functionHandler; public ClientStatFuncHandler(IClientStatService clientStatService) { _functionHandler = new() { { "GetTotalCustomerCount", clientStatService.GetTotalCustomerCount }, { "GetMonthlyCustomerCount", clientStatService.GetMonthlyCustomerCount }, { "GetTotalWorkoutPlanCount", clientStatService.GetTotalWorkoutPlanCount }, { "GetTotalMealPlanCount", clientStatService.GetTotalMealPlanCount }, { "GetLastMonthCustomerCount", clientStatService.GetLastMonthCustomerCount }, { "GetLastFiveAttendances", clientStatService.GetLastFiveAttendances }, { "GetLastFivePayments", clientStatService.GetLastFivePayments }, { "GetLastMonthRevenue", clientStatService.GetLastMonthRevenue }, { "GetMonthlyRevenueVariation", (payload, ctx) => clientStatService.GetMonthlyRevenueVariation(payload, ctx) }, { "GetTotalRevenue", clientStatService.GetTotalRevenue }, { "GetClientStatSummary", (payload, ctx) => clientStatService.GetClientStatSummary(payload, ctx) } }; } 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 (UnauthorizedException ex) { return new ApiResponse { StatusCode = 401, Message = ex.Message, Data = null }; } catch (NotFoundException ex) { return new ApiResponse { StatusCode = 404, Message = ex.Message, Data = null }; } catch (BadRequestException ex) { return new ApiResponse { StatusCode = 400, Message = ex.Message, Data = null }; } catch (ForbiddenException ex) { return new ApiResponse { StatusCode = 403, Message = ex.Message, Data = null }; } catch (Exception ex) { return new ApiResponse { StatusCode = 500, Message = $"An error occurred: {ex.Message}", Data = null }; } } } }