Files
Dhananjaya99 bffc9b1686 first commit
2026-09-28 11:44:26 +05:30

101 lines
3.3 KiB
C#

using Fitness_core_BE.Models.IOs;
using Fitness_core_BE.Services.Auth;
using Fitness_core_BE.DTOs.Auth;
using Fitness_core_BE.Exceptions;
using Fitness_core_BE.Interfaces.Services;
namespace Fitness_core_BE.Services.FunctionHandler
{
public class AuthFuncHandler
{
public readonly Dictionary<string, Func<object, HttpContext, Task<AuthResponse>>> _functionHandler;
public AuthFuncHandler(IAuthService authService)
{
_functionHandler = new()
{
{"SendOtp", authService.SendOtp },
{"VerifyOTP", authService.VerifyOTP },
{"IsAvailable", authService.IsAvailable },
{"RegisterUser", authService.RegisterUser },
{"UpdateUser", authService.UpdateUser },
{"GetUserDetails", authService.GetUserDetails },
{"LoginUser", authService.LoginUser },
{"RefreshToken", authService.RefreshToken },
{"VerifyOtpForLogin", authService.VerifyOtpForLogin },
{"forgotPassword", authService.ForgotPassword },
{"resetPassword", authService.ResetPassword },
{"resetPasswordWithToken", authService.ResetPasswordWithToken },
{"LogOut", authService.LogOut }
};
}
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 authResponse = await handler(request.Payload, httpContext);
return new ApiResponse<object>
{
StatusCode = authResponse.StatusCode,
Message = authResponse.Message,
Data = authResponse.Data
};
}
catch (UnauthorizedException ex)
{
return new ApiResponse<object>
{
StatusCode = 401,
Message = ex.Message,
};
}
catch (NotFoundException ex)
{
return new ApiResponse<object>
{
StatusCode = 404,
Message = ex.Message,
};
}
catch (BadRequestException ex)
{
return new ApiResponse<object>
{
StatusCode = 400,
Message = ex.Message,
};
}
catch (ForbiddenException ex)
{
return new ApiResponse<object>
{
StatusCode = 403,
Message = ex.Message,
};
}
catch (Exception ex)
{
return new ApiResponse<object>
{
StatusCode = 500,
Message = $"An error occurred: {ex.Message}",
};
}
}
}
}