public void ProcessRequest(HttpContext context) { var verb = context.Request.Method.ToUpper(); var reqUri = context.Request.Path.Value; var baseUri = context.Request.PathBase.Value; var path = Regex.Replace(reqUri.Substring(baseUri.Length), @"^(.+)/$", "$1"); if (path.StartsWith("/")) { path = path.Substring(1, path.Length - 1); // path must start with / } path = path.ToLower(); string[] seg = path.Split('/'); if (seg[0] != "api") { return; // is not an api call } string EntityName = seg[1]; Type EntityType = DataManager.RegisteredTypes.FirstOrDefault(x => x.Name.ToLower() == EntityName); if (EntityType == null) { return; // requested entity does not exist } StreamReader sr = new StreamReader(context.Request.Body); object hasObj = Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), EntityType); DataProcessRequest req = new DataProcessRequest { Type = (RequestType)Enum.Parse(typeof(RequestType), verb, true), EntityType = EntityType, Object = hasObj }; dynamic val = DataManager.Process(req); string json = Newtonsoft.Json.JsonConvert.SerializeObject(val); context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; context.Response.ContentLength = json.Length; context.Response.WriteAsync(json); }
public static dynamic Process(DataProcessRequest request) { dynamic result = null; var tbl = new DynamicModel(ConnectionString, tableName: request.EntityType.Name, primaryKeyField: "Id"); switch (request.Type) { case RequestType.GET: if (request.Object != null) { throw new NotImplementedException(); //result = tbl. } else { result = tbl.All(); } break; case RequestType.POST: // Id is not allowed when doing an insert as this is the auto generated key as IDENTITY(1,1) IDictionary <string, object> map = ObjectExtensions.ToDictionary(request.Object); map.Remove("Id"); result = tbl.Insert(map); break; case RequestType.UPDATE: result = tbl.Update(request.Object); break; case RequestType.PUT: throw new NotImplementedException(); break; case RequestType.DELETE: throw new NotImplementedException(); break; default: break; } return(result); }