コード例 #1
0
 /// <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);
         }
     }
 }
コード例 #2
0
/// <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");
                    }
                }
            }
        }
コード例 #3
0
 public static void MapAPIEndpoints(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints)
 {
     endpoints.MapGet("/api/recipe/all/", async(context) =>
     {
         if (!context.Request.Query.ContainsKey("page") ||
             !int.TryParse(context.Request.Query["page"].ToString(), out int page))
         {
             page = 0;
         }
         if (!context.Request.Query.ContainsKey("count") ||
             !int.TryParse(context.Request.Query["count"].ToString(), out int count))
         {
             count = 100;
         }
         RecipeAPI api = new RecipeAPI();
         await context.Response.WriteAsJsonAsync(api.GetAll(new AuthenticationToken(), page, count));
     });
     endpoints.MapGet("/api/recipe/", async(context) =>
     {
         if (!context.Request.Query.ContainsKey("id") ||
             !long.TryParse(context.Request.Query["id"].ToString(), out long id))
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 404, Message = "Could not find a Recipe with that ID." });
         }
         else
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.GetOne(new AuthenticationToken(), id));
         }
     });
     endpoints.MapGet("/api/recipe/mine/", async(context) => {
         long id = 0;
         if ((!context.Request.Query.ContainsKey("name") && !context.Request.Query.ContainsKey("id")) ||
             (context.Request.Query.ContainsKey("name") && string.IsNullOrWhiteSpace(context.Request.Query["name"].ToString()) ||
              (context.Request.Query.ContainsKey("id") && !long.TryParse(context.Request.Query["id"].ToString(), out id))))
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Either both no name and no id specified for that Cook, or one of the two wasn't readable." });
         }
         else
         {
             int page = 0, count = 100;
             if (context.Request.Query.ContainsKey("page") && !int.TryParse(context.Request.Query["page"].ToString(), out page) ||
                 (context.Request.Query.ContainsKey("count") && !int.TryParse(context.Request.Query["count"].ToString(), out count)) ||
                 (context.Request.Query.ContainsKey("page") && !context.Request.Query.ContainsKey("count")))
             {
                 await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Either couldn't read page or count requested, or page was requested and count (per page) wasn't." });
             }
             else
             {
                 AuthenticationToken tokenUser = new AuthenticationToken {
                     ApplicationWideId = id, ApplicationWideName = (context.Request.Query.ContainsKey("name")) ? context.Request.Query["name"].ToString() : ""
                 };
                 RecipeAPI api = new RecipeAPI();
                 await context.Response.WriteAsJsonAsync(api.GetMine(tokenUser, page, count));
             }
         }
     });
     endpoints.MapDelete("/api/recipe", async(context) =>
     {
         if (!context.Request.Query.ContainsKey("id") ||
             !long.TryParse(context.Request.Query["id"].ToString(), out long id))
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 404, Message = "Could not find a Recipe with that ID." });
         }
         else
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.Delete(new AuthenticationToken(), id));
         }
     });
     endpoints.MapPost("/api/recipe", async(context) =>
     {
         Stream body   = context.Request.Body;
         Recipe recipe = null;
         if (body != null)
         {
             string bodyJsonString = await(new StreamReader(body)).ReadToEndAsync();
             recipe = JsonSerializer.Deserialize <Recipe>(bodyJsonString);
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Body of request must contain a Recipe in Json format." });
         }
         if (recipe != null)
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.Create(new AuthenticationToken(), recipe));
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Could not read a Recipe from the body of your request." });
         }
     });
     endpoints.MapPut("/api/recipe", async(context) => {
         Stream body   = context.Request.Body;
         Recipe recipe = null;
         if (body != null)
         {
             string bodyJsonString = await(new StreamReader(body)).ReadToEndAsync();
             recipe = JsonSerializer.Deserialize <Recipe>(bodyJsonString);
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Body of request must contain a Recipe in Json format." });
         }
         if (recipe != null)
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.Update(new AuthenticationToken(), recipe));
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Could not read a Recipe from the body of your request." });
         }
     });
 }