public static IRouteBuilder <T> Post <T>(this IRouteBuilder <T> routeBuilder)
 {
     return(routeBuilder.Post(c =>
     {
         Post(c, routeBuilder, routeBuilder.Set as ICollection <T>);
     }));
 }
        public static IRouteBuilder <T> Crud <T>(this IRouteBuilder <T> routeBuilder, Methods methods = Methods.Get | Methods.Post | Methods.Put | Methods.Delete, string id = "id")
        {
            if (methods.HasFlag(Methods.Get))
            {
                routeBuilder.Get();
            }
            if (methods.HasFlag(Methods.Post))
            {
                routeBuilder.Post();
            }

            if (methods.HasFlag(Methods.Get | Methods.Put | Methods.Delete))
            {
                var newRouteBuilder = routeBuilder.SubRoute($"{{id}}");
                if (methods.HasFlag(Methods.Get))
                {
                    newRouteBuilder.Get();
                }
                if (methods.HasFlag(Methods.Put))
                {
                    newRouteBuilder.Put();
                }
                if (methods.HasFlag(Methods.Delete))
                {
                    newRouteBuilder.Delete();
                }
                return(newRouteBuilder.Route(routeBuilder.Template, routeBuilder.Set));
            }
            return(routeBuilder);
        }
 public static IRouteBuilder Post <T>(this IRouteBuilder routeBuilder, ICollection <T> set)
 {
     return(routeBuilder.Post(c =>
     {
         Post(c, routeBuilder, set);
     }));
 }
 public static IRouteBuilder Delete <T>(this IRouteBuilder routeBuilder, ICollection <T> set)
 {
     return(routeBuilder.Delete(c =>
     {
         string key = c.GetRouteData().Values.First().Key;
         Delete(c, set, key);
     }));
 }
 public static IRouteBuilder <T> Put <T>(this IRouteBuilder <T> routeBuilder)
 {
     return(routeBuilder.Put(c =>
     {
         string key = c.GetRouteData().Values.First().Key;
         Put(c, routeBuilder.Set as ICollection <T>, key);
     }));
 }
        public static IRouteBuilder <T> Get <T>(this IRouteBuilder <T> routeBuilder)
        {
            return(routeBuilder.Get(async c =>
            {
                int routeDataCount = c.GetRouteData().Values.Count;
                if (routeDataCount == 0)
                {
                    await c.WriteJsonAsync(routeBuilder.Set);
                    return;
                }

                await Get(c, routeBuilder.Set, c.GetRouteData().Values.First().Key);
            }));
        }
        public static IRouteBuilder Get <T>(this IRouteBuilder routeBuilder, IEnumerable <T> set)
        {
            return(routeBuilder.Get(async c =>
            {
                int routeDataCount = c.GetRouteData().Values.Count;
                if (routeDataCount == 0)
                {
                    await c.WriteJsonAsync(set);
                    return;
                }

                await Get(c, set, c.GetRouteData().Values.First().Key);
            }));
        }
        private static void Post <T>(HttpContext c, IRouteBuilder routeBuilder, ICollection <T> set)
        {
            var o = c.ReadJsonBody <T>();

            if (o != null)
            {
                set.Add(o);
                c.Response.Headers["Location"] = $"/{routeBuilder.Template}/{typeof(T).GetProperty("Id")?.GetValue(o) ?? ""}";
                c.Response.StatusCode          = 201;
            }
            else
            {
                c.Response.StatusCode = 400;
            }
        }
        private static void AddMetadatas <T>(this IRouteBuilder <T> routeBuilder, string httpMethod, bool pathParameter = false)
        {
            if (!routeBuilder.Settings.EnableMetadatas)
            {
                return;
            }
            var routeActionMetadata = new RouteActionMetadata
            {
                HttpMethod   = httpMethod,
                RelativePath = routeBuilder.Template,
                ReturnType   = httpMethod == HttpMethods.Get ? typeof(T) : null,
                InputType    = httpMethod != HttpMethods.Get ? typeof(T) : null
            };

            if (httpMethod == HttpMethods.Get)
            {
                routeActionMetadata.ReturnType = typeof(T);
                if (pathParameter)
                {
                    routeActionMetadata.InputType     = typeof(string);
                    routeActionMetadata.InputLocation = InputLocation.Path;
                }
            }
            else if (httpMethod == HttpMethods.Post)
            {
                routeActionMetadata.InputType = typeof(T);
            }
            else if (httpMethod == HttpMethods.Put)
            {
                routeActionMetadata.InputType = typeof(T);
            }
            else if (httpMethod == HttpMethods.Delete)
            {
                routeActionMetadata.InputType     = typeof(string);
                routeActionMetadata.InputLocation = InputLocation.Path;
            }
            routeBuilder.Metadatas.RouteActionMetadatas.Add(routeActionMetadata);
        }
 public static IRouteBuilder <T> Put <T>(this IRouteBuilder <T> routeBuilder, string id)
 {
     return(routeBuilder.Put(c => { Put(c, routeBuilder.Set as ICollection <T>, id); }));
 }
 public static IRouteBuilder Put <T>(this IRouteBuilder routeBuilder, ICollection <T> set, string id)
 {
     return(routeBuilder.Put(c => { Put(c, set, id); }));
 }
 public static IRouteBuilder Get <T>(this IRouteBuilder routeBuilder, IEnumerable <T> set, string id)
 {
     return(routeBuilder.Get(async c => { await Get(c, set, id); }));
 }
 public static IRouteBuilder <T> Delete <T>(this IRouteBuilder <T> routeBuilder, string id)
 {
     return(routeBuilder.Delete(c => { Delete(c, routeBuilder.Set as ICollection <T>, id); }));
 }
 public static IRouteBuilder Delete <T>(this IRouteBuilder routeBuilder, ICollection <T> set, string id)
 {
     return(routeBuilder.Delete(c => { Delete(c, set, id); }));
 }