first commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
using Fitness_core_BE.Models.IOs;
|
||||
using Fitness_core_BE.Services;
|
||||
using Fitness_core_BE.Exceptions;
|
||||
using Fitness_core_BE.Interfaces.Services;
|
||||
|
||||
namespace Fitness_core_BE.Services.FunctionHandler
|
||||
{
|
||||
public class ClientFuncHandler
|
||||
{
|
||||
public readonly Dictionary<string, Func<object, HttpContext, Task<object>>> _functionHandler;
|
||||
|
||||
public ClientFuncHandler(IClientService clientService)
|
||||
{
|
||||
_functionHandler = new()
|
||||
{
|
||||
{ "GetClientList", clientService.GetClientList },
|
||||
{ "GetClientById", clientService.GetClientById },
|
||||
{ "GetCustomerDetailsById", clientService.GetCustomerDetailsById },
|
||||
{ "CreateCustomerWithPlans", clientService.CreateCustomerWithPlans },
|
||||
{ "UpdateCustomerByClient", clientService.UpdateCustomerByClient },
|
||||
{"GetCustomersByClientId", clientService.GetCustomersByClientId},
|
||||
{"GetCustomerPlansByClientId", clientService.GetCustomerPlansByClientId},
|
||||
{"RegisterClient", clientService.RegisterClient},
|
||||
{ "GetPackageList", clientService.GetPackageList },
|
||||
{"GetAllCustomers", clientService.GetAllCustomers },
|
||||
{"DeleteCustomerByClient", clientService.DeleteCustomerByClient },
|
||||
{"ClientLogOut", clientService.ClientLogOut },
|
||||
{"GetAllCustomerPlans", clientService.GetAllCustomerPlans },
|
||||
{"GetMealPlansByClientId", clientService.GetMealPlansByClientId },
|
||||
{"GetWorkoutPlansByClientId", clientService.GetWorkoutPlansByClientId },
|
||||
{"GetMealPlanById", clientService.GetMealPlanById },
|
||||
{"GetWorkoutPlanById", clientService.GetWorkoutPlanById },
|
||||
{"GetCustomerPlanById", clientService.GetCustomerPlanById },
|
||||
{"GetOwnCards", clientService.GetCardDetails },
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<object>> Execute(ApiRequest request, HttpContext httpContext)
|
||||
{
|
||||
if (!_functionHandler.TryGetValue(request.FunctionName, out var handler))
|
||||
{
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 404,
|
||||
Message = $"Function '{request.FunctionName}' not found.",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
try
|
||||
{
|
||||
var results = await handler(request.Payload, httpContext);
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 200,
|
||||
Message = "success",
|
||||
Data = results
|
||||
};
|
||||
}
|
||||
catch (UnauthorizedException ex)
|
||||
{
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 401,
|
||||
Message = ex.Message,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 404,
|
||||
Message = ex.Message,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
catch (BadRequestException ex)
|
||||
{
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 400,
|
||||
Message = ex.Message,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
catch (ForbiddenException ex)
|
||||
{
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 403,
|
||||
Message = ex.Message,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 500,
|
||||
Message = $"An error occurred: {ex.Message}",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user