/// <summary>
 /// Registers the actual middlware
 /// </summary>
 /// <param name="options">The options</param>
 /// <param name="routeBuilder">The routebuilder</param>
 protected internal void RegisterMiddleware(SPMiddlewareOptions options,
                                            IRouteBuilder routeBuilder)
 {
     this.options = options;
     foreach (var ent in entities)
     {
         Microsoft.AspNetCore.Http.RequestDelegate requestDelegate = context =>
         {
             var srv = (IGenericSPMiddleware)context.RequestServices.GetService(typeof(IGenericSPMiddleware));
             return(srv.HandleRequest(context, ent));
         };
         if (ent.Methods.ContainsKey("GET"))
         {
             routeBuilder.MapGet(GetUrlForMvcRouting(ent), requestDelegate);
         }
         if (ent.Methods.ContainsKey("PUT"))
         {
             routeBuilder.MapPut(GetUrlForMvcRouting(ent), requestDelegate);
         }
         if (ent.Methods.ContainsKey("POST"))
         {
             routeBuilder.MapPost(GetUrlForMvcRouting(ent), requestDelegate);
         }
         if (ent.Methods.ContainsKey("DELETE"))
         {
             routeBuilder.MapDelete(GetUrlForMvcRouting(ent), requestDelegate);
         }
     }
 }
/// <summary>
        /// Registers the actual middlware
        /// </summary>
        /// <param name="options">The options</param>
        /// <param name="routeBuilder">The routebuilder</param>
        protected internal void RegisterMiddleware(SPMiddlewareOptions options,
                                                   IRouteBuilder routeBuilder)
        {
            this.options = options;
            foreach (var ent in entities)
            {
                Microsoft.AspNetCore.Http.RequestDelegate requestDelegate = context =>
                {
                    var srv = (IGenericSPMiddleware)context.RequestServices.GetService(typeof(IGenericSPMiddleware));
                    if (srv == null)
                    {
                        throw new InvalidOperationException("IGenericSPMiddleware not given");
                    }
                    return(srv.HandleRequest(context, ent));
                };
                foreach (var method in ent.Methods)
                {
                    switch (method.Key)
                    {
                    case OperationType.Get:
                        routeBuilder.MapGet(GetUrlForMvcRouting(ent), requestDelegate);
                        break;

                    case OperationType.Put:
                        routeBuilder.MapPut(GetUrlForMvcRouting(ent), requestDelegate);
                        break;

                    case OperationType.Post:
                        routeBuilder.MapPost(GetUrlForMvcRouting(ent), requestDelegate);
                        break;

                    case OperationType.Delete:
                        routeBuilder.MapDelete(GetUrlForMvcRouting(ent), requestDelegate);
                        break;

                    case OperationType.Patch:
                        // TODO: Testing

#if NETSTD2
                        routeBuilder.MapVerb("PATCH", GetUrlForMvcRouting(ent), requestDelegate);
#else
                        routeBuilder.Map(GetUrlForMvcRouting(ent), context =>
                        {
                            if (context.Request.Method.ToUpper() == "PATCH")
                            {
                                return(requestDelegate(context));
                            }
                            return(null);
                        });
#endif
                        break;

                    default:
                        throw new InvalidOperationException("Only Get, Pust, Post and Delete are allowed");
                    }
                }
            }
        }
 public GenericSPJsonSerializer(Model.NamingMappingHandler namingMappingHandler, SPMiddlewareOptions options,
                                ILogger <GenericSPJsonSerializer> logger,
                                IEnumerable <Error.IResponseExceptionHandler> errorHandlers)
 {
     this.namingMappingHandler = namingMappingHandler;
     this.options       = options;
     this.logger        = logger;
     this.errorHandlers = errorHandlers;
 }
        protected internal void RegisterMiddleware(SPMiddlewareOptions options,
                                                   RouteCollection routeBuilder)
        {
            this.options = options;

            foreach (var ent in entities)
            {
                routeBuilder.Add(ent.ToString(),
                                 new Route(GetUrlForMvcRouting(ent), new RouteHandlerWrap(ent)));
            }
        }
Exemplo n.º 5
0
 public GenericSPMiddleware(
     ParameterProvider parameterProvider,
     SqlHelper sqlHelper,
     ILogger <GenericSPMiddleware> logger,
     IEnumerable <IGenericSPSerializer> serializers,
     SPParametersProvider sPParametersProvider,
     SPMiddlewareOptions sPMiddlewareOptions,
     DbConnection dbConnection)
 {
     this.logger               = logger;
     this.serializers          = serializers;
     this.sPMiddlewareOptions  = sPMiddlewareOptions;
     this.dbConnection         = dbConnection;
     this.parameterProvider    = parameterProvider;
     this.sqlHelper            = sqlHelper;
     this.sPParametersProvider = sPParametersProvider;
 }
 public GenericSPMiddleware(
     ILogger <GenericSPMiddleware> logger,
     SerializerResolver serializerResolver,
     SPMiddlewareOptions sPMiddlewareOptions,
     DbConnection dbConnection,
     IEnumerable <Filter.IRequestInterceptor> requestInterceptors,
     IEnumerable <Filter.RequestLogger> requestLoggers,
     CommandPreparation commandPreparation)
 {
     this.logger              = logger;
     this.serializerResolver  = serializerResolver;
     this.sPMiddlewareOptions = sPMiddlewareOptions;
     this.dbConnection        = dbConnection;
     this.requestInterceptors = requestInterceptors;
     this.commandPreparation  = commandPreparation;
     this.requestLoggers      = requestLoggers;
 }
        public static void AddGenericBackend(this IServiceCollection services,
                                             GenericSP.SPMiddlewareOptions options = null,
                                             SwaggerGeneration.SwaggerFromSPOptions swaggerFromSPOptions = null)
        {
            services.AddRouting();
            services.AddKullDatabaseMetadata();
            services.AddSingleton <Model.NamingMappingHandler>();
            services.AddSingleton <Filter.IParameterInterceptor, Filter.SystemParameters>();
            services.AddTransient <GenericSP.ParameterProvider>();
            services.AddSingleton <GenericSP.MiddlewareRegistration>();

            services.AddTransient <GenericSP.IGenericSPSerializer, GenericSP.GenericSPJsonSerializer>();
            services.AddTransient <GenericSP.IGenericSPSerializer, GenericSP.GenericSPXmlSerializer>();
            services.AddTransient <GenericSP.IGenericSPMiddleware, GenericSP.GenericSPMiddleware>();
            services.AddTransient <Error.IResponseExceptionHandler, Error.SqlServerExceptionHandler>();
            var opts = options ??
                       new GenericSP.SPMiddlewareOptions();

            services.AddSingleton(opts);
            services.AddSingleton(swaggerFromSPOptions ?? new SwaggerGeneration.SwaggerFromSPOptions());
        }