21 lines
732 B
C#
21 lines
732 B
C#
using khmereid_backend.Dtos;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace khmereid_backend.Extensions;
|
|
public static class ControllerExtensions
|
|
{
|
|
public static IActionResult ToActionResult<T>(this ControllerBase controller, ApiResponse<T> response)
|
|
{
|
|
if (response == null)
|
|
return controller.StatusCode(500, new { success = false, error = "Internal error: response is null" });
|
|
|
|
return response.Success switch
|
|
{
|
|
true => controller.Ok(response),
|
|
false when response.Code?.Contains("not found", StringComparison.OrdinalIgnoreCase) == true =>
|
|
controller.NotFound(response),
|
|
false => controller.BadRequest(response)
|
|
};
|
|
}
|
|
}
|