Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LuUtilsExtensions.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Middlewares;
  13. using Microsoft.AspNetCore.Builder;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal;
  17. using Microsoft.Extensions.DependencyInjection;
  18. using Microsoft.Extensions.DependencyInjection.Extensions;
  19. using Newtonsoft.Json;
  20. namespace Luticate2.Utils.Controllers
  21. {
  22. public static class LuUtilsExtensions
  23. {
  24. public static IServiceCollection AddLuticateUtils(this IServiceCollection services, Action<LuUtilsOptionsDbo> 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.AddScoped<LuEfTransactionScope>();
  37. services.AddSingleton<LuHubConnectionTracker>();
  38. services.AddSingleton<LuNotificationsBusiness>();
  39. services.Configure(optionsDelegate);
  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. }