using System; using System.Collections.Generic; using Luticate2.Utils.Business; using Luticate2.Utils.DataAccess; using Luticate2.Utils.DataAccess.Npgsql; using Luticate2.Utils.Dbo.Basic; using Luticate2.Utils.Dbo.Filter; using Luticate2.Utils.Dbo.OrderBy; using Luticate2.Utils.Dbo.PaginatedRequest; using Luticate2.Utils.Dbo.Result; using Luticate2.Utils.Hubs; using Luticate2.Utils.Interfaces; using Luticate2.Utils.Middlewares; using Luticate2.Utils.Utils; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Newtonsoft.Json; namespace Luticate2.Utils.Controllers { public static class LuUtilsExtensions { public static IServiceCollection AddLuticateUtils(this IServiceCollection services, Action optionsDelegate) { var settings = new JsonSerializerSettings(); settings.ContractResolver = new SignalRContractResolver(); var serializer = JsonSerializer.Create(settings); services.Add(new ServiceDescriptor(typeof(JsonSerializer), provider => serializer, ServiceLifetime.Transient)); services.AddSignalR(options => { options.Hubs.EnableDetailedErrors = true; }); services.TryAddSingleton(); services.TryAddScoped(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.Configure(optionsDelegate); return services; } public static IServiceCollection AddEntityFrameworkNpgsqlLuticate(this IServiceCollection services) { services.TryAdd(new ServiceCollection() .AddScoped()); return services; } public static IMvcBuilder AddLuticateUtils(this IMvcBuilder builder) { builder.Services.Configure( options => { options.ModelBinderProviders.Insert(0, new LuOrderByBinderProvider()); options.ModelBinderProviders.Insert(0, new LuFilterBinderProvider()); options.ModelBinderProviders.Insert(0, new LuPaginatedRequestBinderProvider()); options.Filters.Add(typeof(LuModelStateFilter)); }); return builder; } public static IApplicationBuilder UseLuticateUtils(this IApplicationBuilder app) { app.UseMiddleware(); app.UseWebSockets(); app.UseSignalR(); return app; } public static int GetHttpCode(this LuResult result) { if (result.Status == LuStatus.Success) { return 200; } if (result.Status == LuStatus.InputError) { return 400; } if (result.Status == LuStatus.LoginError) { return 401; } if (result.Status == LuStatus.PermissionError) { return 403; } if (result.Status == LuStatus.NotFound) { return 404; } if (result.Status == LuStatus.DbError) { return 500; } if (result.Status == LuStatus.FsError) { return 500; } if (result.Status == LuStatus.BackendError) { return 500; } if (result.Status == LuStatus.InternalError) { return 500; } return 418; } public static string GetHttpString(this LuResult result) { if (result.Status == LuStatus.Success) { return "Success"; } if (result.Status == LuStatus.InputError) { return "Bad User Input"; } if (result.Status == LuStatus.LoginError) { return "Login Error"; } if (result.Status == LuStatus.PermissionError) { return "Permission Error"; } if (result.Status == LuStatus.NotFound) { return "Not Found"; } if (result.Status == LuStatus.DbError) { return "Internal Error"; } if (result.Status == LuStatus.FsError) { return "Internal Error"; } if (result.Status == LuStatus.BackendError) { return "Internal Error"; } if (result.Status == LuStatus.InternalError) { return "Internal Error"; } return "I'm a teapot"; } public static IDictionary GetLuItems(this HttpContext context) { const string key = "luticateItems"; if (!context.Items.ContainsKey(key)) { context.Items.Add(key, new Dictionary()); } return (IDictionary) context.Items[key]; } } }