private string SaveObject(dynamic value, string statementid) { string returnval = null; if (value == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } value.stored = DateTime.Now; if (value.timestamp == null) value.timestamp = value.stored; if (statementid != null) { value._id = statementid; value.id = statementid; } value.type = "statement"; value.authority = "anonymous"; string json = Newtonsoft.Json.JsonConvert.SerializeObject(value); try { var str = new DB().CreateDocument("http://localhost:5984", "tin_can", json); return str; } catch (WebException ex) { Console.Write(ex.Response); throw new HttpResponseException(HttpStatusCode.BadRequest); } }
// GET api/statements public dynamic Get(string statementid = null, string limit = null) { var db = new DB(); try { // hacky if (statementid == null) { statementid = "_design/statements/_view/all"; } if(limit != null) statementid += "?limit=" + limit; var doc = db.GetDocument("http://localhost:5984", "tin_can", statementid); dynamic jo = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(doc); if(((IDictionary<String, Object>)jo).ContainsKey("_id")){ return CleanStatement(jo); } else if ( ((IDictionary<String, Object>)jo).ContainsKey("rows") ){ var list = new List<dynamic>(); foreach (var r in jo.rows) { var stm = r.value; list.Add(CleanStatement(stm)); } return new { statements = list }; } else { return new {result = jo}; } }catch(WebException ex){ switch (ex.Status) { case WebExceptionStatus.ProtocolError: throw new HttpResponseException(HttpStatusCode.NotFound); default: throw new HttpResponseException(HttpStatusCode.BadRequest); } } }