public class APIResponse
{
public APIResponse()
{
ErrorMessages = new List<string>();
}
public HttpStatusCode StatusCode { get; set; }
public bool IsSuccess { get; set; } = true;
public List<string> ErrorMessages { get; set; }
public object Result { get; set; }
}
public async Task<ActionResult<APIResponse>> GetProductById(int id)
{
try
{
if (id == 0)
{
_response.StatusCode = HttpStatusCode.BadRequest;
return BadRequest(_response);
}
var product = await _productRepository.GetById(id);
if (product == null)
{
_response.StatusCode = HttpStatusCode.NotFound;
return NotFound(_response);
}
_response.Result = product;
_response.StatusCode = HttpStatusCode.OK;
return Ok(_response);
}
catch (Exception ex)
{
_response.IsSuccess = false;
_response.ErrorMessages
= new List<string>() { ex.ToString() };
}
return _response;
}