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.

LuUtilsExtensions.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections.Generic;
  2. using Luticate2.Utils.Dbo.Basic;
  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 delegate void LuOptionsDelegate(LuOptionsDbo options);
  15. public static LuOptionsDbo Options { get; private set; }
  16. public static IServiceCollection AddLuticateUtils(this IServiceCollection services, LuOptionsDelegate optionsDelegate)
  17. {
  18. Options = new LuOptionsDbo();
  19. optionsDelegate(Options);
  20. return services;
  21. }
  22. public static IMvcBuilder AddLuticateUtils(this IMvcBuilder builder)
  23. {
  24. builder.Services.Configure<MvcOptions>(
  25. options => options.ModelBinderProviders.Insert(0, new LuOrderByBinderProvider()));
  26. return builder;
  27. }
  28. public static IApplicationBuilder UseLuticateUtils(this IApplicationBuilder app)
  29. {
  30. app.UseMiddleware<LuExceptionMiddleware>();
  31. return app;
  32. }
  33. public static int GetHttpCode<T>(this LuResult<T> result)
  34. {
  35. if (result.Status == LuStatus.Success)
  36. {
  37. return 200;
  38. }
  39. if (result.Status == LuStatus.InputError)
  40. {
  41. return 400;
  42. }
  43. if (result.Status == LuStatus.LoginError)
  44. {
  45. return 401;
  46. }
  47. if (result.Status == LuStatus.PermissionError)
  48. {
  49. return 403;
  50. }
  51. if (result.Status == LuStatus.NotFound)
  52. {
  53. return 404;
  54. }
  55. if (result.Status == LuStatus.DbError)
  56. {
  57. return 500;
  58. }
  59. if (result.Status == LuStatus.InternalError)
  60. {
  61. return 500;
  62. }
  63. return 418;
  64. }
  65. public static string GetHttpString<T>(this LuResult<T> result)
  66. {
  67. if (result.Status == LuStatus.Success)
  68. {
  69. return "Success";
  70. }
  71. if (result.Status == LuStatus.InputError)
  72. {
  73. return "Bad User Input";
  74. }
  75. if (result.Status == LuStatus.LoginError)
  76. {
  77. return "Login Error";
  78. }
  79. if (result.Status == LuStatus.PermissionError)
  80. {
  81. return "Permission Error";
  82. }
  83. if (result.Status == LuStatus.NotFound)
  84. {
  85. return "Not Found";
  86. }
  87. if (result.Status == LuStatus.DbError)
  88. {
  89. return "Internal Error";
  90. }
  91. if (result.Status == LuStatus.InternalError)
  92. {
  93. return "Internal Error";
  94. }
  95. return "I'm a teapot";
  96. }
  97. public static IDictionary<object, object> GetLuItems(this HttpContext context)
  98. {
  99. return (IDictionary<object, object>) context.Items["luticateItems"];
  100. }
  101. }
  102. }