/// <summary> /// Filter's this route's dataset by a request parameter that is compared to the primary key of the model /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <param name="parameters">The parameters to filter by</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> FilterByPrimaryKey <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder, params ParameterRetriever[] parameters) where TModel : class where TUser : class { return(builder.AddFilter(new PrimaryKeyFilter <TModel>(RestModelOptionsBuilderExtensions.GetPrimaryKey(builder), parameters))); }
/// <summary> /// Filter's this route's dataset by route values compared to the primary key of the model /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <param name="parameterNames">The names of the query parameters to filter by</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> FilterByPrimaryKeyRoute <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder, params string[] parameterNames) where TModel : class where TUser : class => RestModelOptionsBuilderExtensions.FilterByPrimaryKey <TModel, TUser, RouteValueRetriever>( builder, parameterNames);
/// <summary> /// Sets this route up to handle a DELETE request by query parameters using an ORM. The names of the query /// parameters will be camelCased names of the primary key of the entity /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <param name="routePattern">The route pattern to set up the request for</param> /// <param name="optionsHandler">A handler for the route options</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> DeleteByPrimaryKeyQuery <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder, string routePattern, Action <RestModelOptionsBuilder <TModel, TUser> >?optionsHandler = null) where TModel : class where TUser : class => builder.DeleteByPrimaryKeyQuery( routePattern, RestModelOptionsBuilderExtensions.GetKeyNames(builder), optionsHandler);
/// <summary> /// Ensures that the primary key of the <typeparamref name="TModel" /> will be omitted from the response body /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> OmitPrimaryKey <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder) where TModel : class where TUser : class { foreach (KeyProperty Property in RestModelOptionsBuilderExtensions.GetPrimaryKey(builder)) { builder.Omit(Property.PropertyInfo); } return(builder); }
/// <summary> /// Gets camelCased versions of the names of the primary key properties /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The builder containing the type of database context to get the keys for</param> /// <returns>camelCased versions of the primary key property names, in order</returns> private static string[] GetKeyNames <TModel, TUser>(RestModelOptionsBuilder <TModel, TUser> builder) where TModel : class where TUser : class { List <KeyProperty> KeyProperties = RestModelOptionsBuilderExtensions.GetPrimaryKey(builder); IEnumerable <string> Names = KeyProperties.Select(p => RestModelOptionsBuilderExtensions.CamelCase(p.Name)); return(Names.ToArray()); }
/// <summary> /// Sets this route up to handle a DELETE request by route values using an ORM. The names of the route values /// will be camelCased versions of the C# property name. If these route parameters do not already exist they will be /// added in order, e.g. /key1/key2. /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <param name="routePattern">The route pattern to set up the request for</param> /// <param name="optionsHandler">A handler for the route options</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> DeleteByPrimaryKey <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder, string routePattern, Action <RestModelOptionsBuilder <TModel, TUser> >?optionsHandler = null) where TModel : class where TUser : class { string UpdatedPattern = routePattern; string[] ExistingNames = TemplateParser.Parse(routePattern).Parameters.Select(p => p.Name).ToArray(); string[] KeyNames = RestModelOptionsBuilderExtensions.GetKeyNames(builder); foreach (string KeyName in KeyNames.Except(ExistingNames)) { if (!UpdatedPattern.EndsWith("/")) { UpdatedPattern += "/"; } UpdatedPattern += $"{{{KeyName}}}/"; } return(builder.DeleteByPrimaryKey(UpdatedPattern, KeyNames, optionsHandler)); }
/// <summary> /// Sets this route up to handle a GET request by query parameters on the same route pattern using an ORM. /// The names of the query parameters will be camelCased names of the primary key of the entity /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <param name="optionsHandler">A handler for the route options</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> GetByPrimaryKeyQuery <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder, Action <RestModelOptionsBuilder <TModel, TUser> >?optionsHandler = null) where TModel : class where TUser : class => builder.GetByPrimaryKeyQuery("", RestModelOptionsBuilderExtensions.GetKeyNames(builder), optionsHandler);
/// <summary> /// Filter's this route's dataset by route values compared to the primary key of the model. The query parameters' names /// will be camelCased versions of their C# property name /// </summary> /// <typeparam name="TModel">The model type that the API is being built for</typeparam> /// <typeparam name="TUser">The type of authenticated user context</typeparam> /// <param name="builder">The options builder to perform the operation on</param> /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns> public static RestModelOptionsBuilder <TModel, TUser> FilterByPrimaryKeyRoute <TModel, TUser>( this RestModelOptionsBuilder <TModel, TUser> builder) where TModel : class where TUser : class => builder.FilterByPrimaryKeyRoute(RestModelOptionsBuilderExtensions.GetKeyNames(builder));