You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BMRHandler.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Net;
  2. using System.Net.Http;
  3. using System.Web.Http;
  4. using iiie.Logs.DataAccess;
  5. using iiie.Logs.DBO;
  6. namespace iiie.WebApiUtils.BusinessManager
  7. {
  8. /// <summary>
  9. /// Handle business manager results
  10. /// </summary>
  11. public class BMRHandler : ApiController
  12. {
  13. /// <summary>
  14. /// Converts ResultStatus codes to HttpStatusCodes codes
  15. /// </summary>
  16. /// <param name="status">The status code</param>
  17. /// <returns>The http code</returns>
  18. public static HttpStatusCode ResultStatusToHttp(ResultStatus status)
  19. {
  20. if (status == ResultStatus.LoginError)
  21. return HttpStatusCode.Unauthorized;
  22. if (status == ResultStatus.PermissionError)
  23. return HttpStatusCode.Forbidden;
  24. if (status == ResultStatus.InputError)
  25. return HttpStatusCode.BadRequest;
  26. if (status == ResultStatus.NotFound)
  27. return HttpStatusCode.NotFound;
  28. return HttpStatusCode.InternalServerError;
  29. }
  30. /// <summary>
  31. /// Returns a string corresponding to the error
  32. /// </summary>
  33. /// <param name="result">The result of the operation</param>
  34. /// <returns>The error string</returns>
  35. public static string OpResultToString<T>(OpResult<T> result)
  36. {
  37. if (result.PublicDetails != null)
  38. return result.PublicDetails;
  39. if (result.Status == ResultStatus.LoginError)
  40. return "Bad username/password";
  41. if (result.Status == ResultStatus.PermissionError)
  42. return "You don\'t have the permission to do this action";
  43. if (result.Status == ResultStatus.InputError)
  44. return "Invalid data was provided";
  45. if (result.Status == ResultStatus.NotFound)
  46. return "Ressource not found";
  47. return "Internal error";
  48. }
  49. /// <summary>
  50. /// Handle business manager results
  51. /// </summary>
  52. /// <param name="result">The result to handle</param>
  53. /// <returns>The data to return to the user</returns>
  54. [NonAction]
  55. public T Handle<T>(OpResult<T> result)
  56. {
  57. if (result.Status == ResultStatus.Success)
  58. return result.Data;
  59. result.Log();
  60. var msg = Request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
  61. throw new HttpResponseException(msg);
  62. }
  63. }
  64. }