123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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.Middlewares;
- 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<LuUtilsOptionsDbo> 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.AddScoped<LuEfTransactionScope>();
- services.AddSingleton<LuHubConnectionTracker>();
- services.AddSingleton<LuNotificationsBusiness>();
- services.Configure(optionsDelegate);
- return services;
- }
-
- public static IServiceCollection AddEntityFrameworkNpgsqlLuticate(this IServiceCollection services)
- {
- services.TryAdd(new ServiceCollection()
- .AddScoped<NpgsqlCompositeMethodCallTranslator, NpgsqlCompositeMethodCallTranslatorLuticate>());
- return services;
- }
-
- public static IMvcBuilder AddLuticateUtils(this IMvcBuilder builder)
- {
- builder.Services.Configure<MvcOptions>(
- 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<LuExceptionMiddleware>();
-
- app.UseWebSockets();
- app.UseSignalR();
-
- return app;
- }
-
- public static int GetHttpCode<T>(this LuResult<T> 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.InternalError)
- {
- return 500;
- }
- return 418;
- }
-
- public static string GetHttpString<T>(this LuResult<T> 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.InternalError)
- {
- return "Internal Error";
- }
- return "I'm a teapot";
- }
-
- public static IDictionary<object, object> GetLuItems(this HttpContext context)
- {
- return (IDictionary<object, object>) context.Items["luticateItems"];
- }
- }
- }
|