123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System.Collections.Generic;
- using Luticate2.Utils.DataAccess.Npgsql;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.Filter;
- using Luticate2.Utils.Dbo.OrderBy;
- using Luticate2.Utils.Dbo.Result;
- 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;
-
- namespace Luticate2.Utils.Controllers
- {
- public static class LuUtilsExtensions
- {
- public delegate void LuOptionsDelegate(LuOptionsDbo options);
-
- public static LuOptionsDbo Options { get; private set; }
-
- public static IServiceCollection AddLuticateUtils(this IServiceCollection services, LuOptionsDelegate optionsDelegate)
- {
- Options = new LuOptionsDbo();
- optionsDelegate(Options);
- 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());
- });
- return builder;
- }
-
- public static IApplicationBuilder UseLuticateUtils(this IApplicationBuilder app)
- {
- app.UseMiddleware<LuExceptionMiddleware>();
- 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"];
- }
- }
- }
|