using System.Net;
using System.Net.Http;
using System.Web.Http;
using iiie.Logs.DBO;
namespace iiie.WebApiUtils.BusinessManager
{
///
/// Handle business manager results
///
/// Data type
public class BMRHandler
{
///
/// Converts ResultStatus codes to HttpStatusCodes codes
///
/// The status code
/// The http code
public static HttpStatusCode ResultStatusToHttp(ResultStatus status)
{
if (status == ResultStatus.LoginError)
return HttpStatusCode.Unauthorized;
if (status == ResultStatus.PermissionError)
return HttpStatusCode.Forbidden;
if (status == ResultStatus.InputError)
return HttpStatusCode.BadRequest;
if (status == ResultStatus.NotFound)
return HttpStatusCode.NotFound;
return HttpStatusCode.InternalServerError;
}
///
/// Returns a string corresponding to the error
///
/// The result of the operation
/// The error string
public static string OpResultToString(OpResult result)
{
if (result)
return result.PublicDetails;
if (result.Status == ResultStatus.LoginError)
return "Bad username/password";
if (result.Status == ResultStatus.PermissionError)
return "You don\'t have the permission to do this action";
if (result.Status == ResultStatus.InputError)
return "Invalid data was provided";
if (result.Status == ResultStatus.NotFound)
return "Ressource not found";
return "Internal error";
}
///
/// Handle business manager results
///
/// The result to handle
/// The request to handle
/// The data to return to the user
public static T Handle(OpResult result, HttpRequestMessage request)
{
if (result.Status == ResultStatus.Success)
return result.Data;
var msg = request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
throw new HttpResponseException(msg);
}
}
}