選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LuUtilsExtensions.cs 3.9KB

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