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

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