Files
Core_Be/Services/FunctionHandler/MealFunctionHandler.cs
T
Dhananjaya99 bffc9b1686 first commit
2026-09-28 11:44:26 +05:30

89 lines
3.1 KiB
C#

using Fitness_core_BE.Models.IOs;
using Fitness_core_BE.Services.Meal;
using Fitness_core_BE.Interfaces.Services;
namespace Fitness_core_BE.Services.FunctionHandler
{
public class MealFunctionHandler
{
public readonly Dictionary<string, Func<object, HttpContext, Task<object>>> _functionHandler;
public MealFunctionHandler(IMealService mealService)
{
_functionHandler = new()
{
{ "GetMealCategoryList", mealService.GetMealCategoryList },
{ "CreateMealCategory", mealService.CreateMealCategory },
{ "GetMealPlanList", mealService.GetMealPlanList },
{ "GetMealPlanById", mealService.GetMealPlanById },
{ "GetMealPlanByCustomerId", mealService.GetMealPlanByCustomerId },
{ "CreateMealPlan", mealService.CreateMealPlan },
{ "CreateCustomerMealPlan", mealService.CreateCustomerMealPlan },
{ "UpdateMealPlan", mealService.UpdateMealPlan },
{ "GetMealItemsByMealPlanId", mealService.GetMealItemsByMealPlanId },
{ "CreateMealItem", mealService.CreateMealItem },
{ "DeleteMealPlan", mealService.DeleteMealPlan }
};
}
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 (UnauthorizedAccessException ex)
{
return new ApiResponse<object>
{
StatusCode = 401,
Message = ex.Message,
Data = null
};
}
catch (KeyNotFoundException ex)
{
return new ApiResponse<object>
{
StatusCode = 400,
Message = ex.Message,
Data = null
};
}
catch (InvalidOperationException ex)
{
return new ApiResponse<object>
{
StatusCode = 400,
Message = ex.Message,
Data = null
};
}
catch (Exception ex)
{
return new ApiResponse<object>
{
StatusCode = 500,
Message = $"An error occurred: {ex.Message}",
Data = null
};
}
}
}
}