public virtual ITagadaBuilder GetAsync <TQuery, TResult>(string path, Func <TQuery, Task <TResult> > function) where TQuery : class, new() { void addRoute(RouteBuilder routeBuilder) { routeBuilder.MapGet(path.TrimStart('/'), async(request, response, routeData) => { var query = new TQuery(); var queryProperties = CachedTypes.GetTypeProperties(typeof(TQuery)); foreach (var queryProperty in queryProperties) { // copy route params inside query object if (routeData.Values.ContainsKey(queryProperty.Name)) { var copiedValue = Convert.ChangeType(routeData.Values[queryProperty.Name], queryProperty.PropertyType); queryProperty.SetValue(query, copiedValue); continue; } // copy query params inside query object if (request.Query.ContainsKey(queryProperty.Name)) { var stringValues = request.Query[queryProperty.Name]; var copiedValue = Convert.ChangeType(stringValues[0], queryProperty.PropertyType); queryProperty.SetValue(query, copiedValue); } } ExecuteBeforeRoute(new TagadaRouteResult { HttpVerb = "GET", Path = path, Input = query }); var result = await function(query); await response.WriteJsonAsync(_serializer, result); ExecuteAfterRoute(new TagadaRouteResult { HttpVerb = "GET", Path = path, Input = query, Result = result }); }); } _routes.Add(new TagadaRoute { HttpVerb = "GET", Path = path, InputType = typeof(TQuery), ResultType = typeof(TResult) }); AddRouteAction(addRoute); return(this); }
public virtual ITagadaBuilder Delete <TCommand, TResult>(string path, Func <TCommand, TResult> function) where TCommand : class, new() { void addRoute(RouteBuilder routeBuilder) { routeBuilder.MapDelete(path.TrimStart('/'), async(request, response, routeData) => { var command = new TCommand(); var commandProperties = CachedTypes.GetTypeProperties(typeof(TCommand)); foreach (var commandProperty in commandProperties) { // copy route params inside query object if (routeData.Values.ContainsKey(commandProperty.Name)) { var copiedValue = Convert.ChangeType(routeData.Values[commandProperty.Name], commandProperty.PropertyType); commandProperty.SetValue(command, copiedValue); continue; } // copy query params inside query object if (request.Query.ContainsKey(commandProperty.Name)) { var stringValues = request.Query[commandProperty.Name]; var copiedValue = Convert.ChangeType(stringValues[0], commandProperty.PropertyType); commandProperty.SetValue(command, copiedValue); } } ExecuteBeforeRoute(new TagadaRouteResult { HttpVerb = "DELETE", Path = path, Input = command }); var result = function(command); await response.WriteJsonAsync(_serializer, result); ExecuteAfterRoute(new TagadaRouteResult { HttpVerb = "DELETE", Path = path, Input = command, Result = result }); }); } _routes.Add(new TagadaRoute { HttpVerb = "DELETE", Path = path, InputType = typeof(TCommand), ResultType = typeof(TResult) }); AddRouteAction(addRoute); return(this); }
public virtual ITagadaBuilder PutAsync <TCommand>(string path, Func <TCommand, Task> action) where TCommand : class, new() { void addRoute(RouteBuilder routeBuilder) { routeBuilder.MapPut(path.TrimStart('/'), async(request, response, routeData) => { var command = await request.HttpContext.ReadFromJsonAsync <TCommand>(_serializer); var commandProperties = CachedTypes.GetTypeProperties(typeof(TCommand)); foreach (var commandProperty in commandProperties) { // copy route params inside command object if (routeData.Values.ContainsKey(commandProperty.Name)) { var copiedValue = Convert.ChangeType(routeData.Values[commandProperty.Name], commandProperty.PropertyType); commandProperty.SetValue(command, copiedValue); } } ExecuteBeforeRoute(new TagadaRouteResult { HttpVerb = "PUT", Path = path, Input = command }); await action(command); ExecuteAfterRoute(new TagadaRouteResult { HttpVerb = "PUT", Path = path, Input = command, Result = null }); }); } _routes.Add(new TagadaRoute { HttpVerb = "PUT", Path = path, InputType = typeof(TCommand), ResultType = null }); AddRouteAction(addRoute); return(this); }