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 4.8KB

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