Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuUtilsExtensions.cs 5.3KB

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