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

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