Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BMRHandler.cs 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Net;
  2. using System.Net.Http;
  3. using System.Web.Http;
  4. using iiie.Logs.DBO;
  5. namespace iiie.WebApiUtils.BusinessManager
  6. {
  7. /// <summary>
  8. /// Handle business manager results
  9. /// </summary>
  10. /// <typeparam name="T">Data type</typeparam>
  11. public class BMRHandler<T>
  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(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. /// <param name="request">The request to handle</param>
  54. /// <returns>The data to return to the user</returns>
  55. public static T Handle(OpResult<T> result, HttpRequestMessage request)
  56. {
  57. if (result.Status == ResultStatus.Success)
  58. return result.Data;
  59. var msg = request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
  60. throw new HttpResponseException(msg);
  61. }
  62. }
  63. }