Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LuUtilsExtensions.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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<ILuNotificationsBusiness, LuNotificationsBusiness>();
  42. services.Configure(optionsDelegate);
  43. return services;
  44. }
  45. public static IServiceCollection AddEntityFrameworkNpgsqlLuticate(this IServiceCollection services)
  46. {
  47. services.TryAdd(new ServiceCollection()
  48. .AddScoped<NpgsqlCompositeMethodCallTranslator, NpgsqlCompositeMethodCallTranslatorLuticate>());
  49. return services;
  50. }
  51. public static IMvcBuilder AddLuticateUtils(this IMvcBuilder builder)
  52. {
  53. builder.Services.Configure<MvcOptions>(
  54. options =>
  55. {
  56. options.ModelBinderProviders.Insert(0, new LuOrderByBinderProvider());
  57. options.ModelBinderProviders.Insert(0, new LuFilterBinderProvider());
  58. options.ModelBinderProviders.Insert(0, new LuPaginatedRequestBinderProvider());
  59. options.Filters.Add(typeof(LuModelStateFilter));
  60. });
  61. return builder;
  62. }
  63. public static IApplicationBuilder UseLuticateUtils(this IApplicationBuilder app)
  64. {
  65. app.UseMiddleware<LuExceptionMiddleware>();
  66. app.UseWebSockets();
  67. app.UseSignalR();
  68. return app;
  69. }
  70. public static int GetHttpCode<T>(this LuResult<T> result)
  71. {
  72. if (result.Status == LuStatus.Success)
  73. {
  74. return 200;
  75. }
  76. if (result.Status == LuStatus.InputError)
  77. {
  78. return 400;
  79. }
  80. if (result.Status == LuStatus.LoginError)
  81. {
  82. return 401;
  83. }
  84. if (result.Status == LuStatus.PermissionError)
  85. {
  86. return 403;
  87. }
  88. if (result.Status == LuStatus.NotFound)
  89. {
  90. return 404;
  91. }
  92. if (result.Status == LuStatus.DbError)
  93. {
  94. return 500;
  95. }
  96. if (result.Status == LuStatus.FsError)
  97. {
  98. return 500;
  99. }
  100. if (result.Status == LuStatus.BackendError)
  101. {
  102. return 500;
  103. }
  104. if (result.Status == LuStatus.InternalError)
  105. {
  106. return 500;
  107. }
  108. return 418;
  109. }
  110. public static string GetHttpString<T>(this LuResult<T> result)
  111. {
  112. if (result.Status == LuStatus.Success)
  113. {
  114. return "Success";
  115. }
  116. if (result.Status == LuStatus.InputError)
  117. {
  118. return "Bad User Input";
  119. }
  120. if (result.Status == LuStatus.LoginError)
  121. {
  122. return "Login Error";
  123. }
  124. if (result.Status == LuStatus.PermissionError)
  125. {
  126. return "Permission Error";
  127. }
  128. if (result.Status == LuStatus.NotFound)
  129. {
  130. return "Not Found";
  131. }
  132. if (result.Status == LuStatus.DbError)
  133. {
  134. return "Internal Error";
  135. }
  136. if (result.Status == LuStatus.FsError)
  137. {
  138. return "Internal Error";
  139. }
  140. if (result.Status == LuStatus.BackendError)
  141. {
  142. return "Internal Error";
  143. }
  144. if (result.Status == LuStatus.InternalError)
  145. {
  146. return "Internal Error";
  147. }
  148. return "I'm a teapot";
  149. }
  150. public static IDictionary<object, object> GetLuItems(this HttpContext context)
  151. {
  152. const string key = "luticateItems";
  153. if (!context.Items.ContainsKey(key))
  154. {
  155. context.Items.Add(key, new Dictionary<object, object>());
  156. }
  157. return (IDictionary<object, object>) context.Items[key];
  158. }
  159. }
  160. }