Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LuUtilsExtensions.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections.Generic;
  2. using Luticate2.Utils.Dbo;
  3. using Luticate2.Utils.Dbo.OrderBy;
  4. using Luticate2.Utils.Dbo.Result;
  5. using Luticate2.Utils.Middlewares;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.DependencyInjection;
  10. namespace Luticate2.Utils.Controllers
  11. {
  12. public static class LuUtilsExtensions
  13. {
  14. public static IServiceCollection AddLuticatUtils(this IServiceCollection services)
  15. {
  16. return services;
  17. }
  18. public static IMvcBuilder AddLuticatUtils(this IMvcBuilder builder)
  19. {
  20. builder.Services.Configure<MvcOptions>(
  21. options => options.ModelBinderProviders.Insert(0, new LuOrderByBinderProvider()));
  22. return builder;
  23. }
  24. public static IApplicationBuilder UseLuticateUtils(this IApplicationBuilder app)
  25. {
  26. app.UseMiddleware<LuExceptionMiddleware>();
  27. return app;
  28. }
  29. public static int GetHttpCode<T>(this LuResult<T> result)
  30. {
  31. if (result.Status == LuStatus.Success)
  32. {
  33. return 200;
  34. }
  35. if (result.Status == LuStatus.InputError)
  36. {
  37. return 400;
  38. }
  39. if (result.Status == LuStatus.LoginError)
  40. {
  41. return 401;
  42. }
  43. if (result.Status == LuStatus.PermissionError)
  44. {
  45. return 403;
  46. }
  47. if (result.Status == LuStatus.NotFound)
  48. {
  49. return 404;
  50. }
  51. if (result.Status == LuStatus.DbError)
  52. {
  53. return 500;
  54. }
  55. if (result.Status == LuStatus.InternalError)
  56. {
  57. return 500;
  58. }
  59. return 418;
  60. }
  61. public static string GetHttpString<T>(this LuResult<T> result)
  62. {
  63. if (result.Status == LuStatus.Success)
  64. {
  65. return "Success";
  66. }
  67. if (result.Status == LuStatus.InputError)
  68. {
  69. return "Bad User Input";
  70. }
  71. if (result.Status == LuStatus.LoginError)
  72. {
  73. return "Login Error";
  74. }
  75. if (result.Status == LuStatus.PermissionError)
  76. {
  77. return "Permission Error";
  78. }
  79. if (result.Status == LuStatus.NotFound)
  80. {
  81. return "Not Found";
  82. }
  83. if (result.Status == LuStatus.DbError)
  84. {
  85. return "Internal Error";
  86. }
  87. if (result.Status == LuStatus.InternalError)
  88. {
  89. return "Internal Error";
  90. }
  91. return "I'm a teapot";
  92. }
  93. public static IDictionary<object, object> GetLuItems(this HttpContext context)
  94. {
  95. return (IDictionary<object, object>) context.Items["luticateItems"];
  96. }
  97. }
  98. }