public async Task <dynamic> GetTodoItemsAsync() { List <TodoItem> mylist = new List <TodoItem>(); // TODO create a View with the right content to avoid Json Parse ... HttpClient dbclient = await TodoDbService.CouchDbClientAsync(); var res = await dbclient.GetAsync(dbname + "/_all_docs?include_docs=true"); if (res.IsSuccessStatusCode) { //force synchron string body = res.Content.ReadAsStringAsync().Result; dynamic o = JObject.Parse(body); // test with https://jsonpath.curiousconcept.com/ IEnumerable <JToken> values = o.SelectTokens("$.rows..doc"); foreach (JToken token in values) { string test = token.ToString(); Console.WriteLine(test); TodoItem item = JsonConvert.DeserializeObject <TodoItem>(test); mylist.Add(item); } return(mylist); } string msg = "Error: " + res.StatusCode + " " + res.ReasonPhrase; _logger.LogDebug(msg); return(mylist); }
public async Task <TodoItem> UpdateItemAsync(TodoItem item) { HttpClient dbclient = await TodoDbService.CouchDbClientAsync(); PostTodoItem postitem = new PostTodoItem(); postitem.done = item.done; postitem.text = item.text; postitem.type = item.type; var postData = JsonConvert.SerializeObject(postitem); var content = new StringContent(postData, Encoding.UTF8, "application/json"); var response = await dbclient.PutAsync(dbname + "/" + WebUtility.UrlEncode(item._id) + "?rev=" + WebUtility.UrlEncode(item._rev), content); response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) { var responseJson = await response.Content.ReadAsStringAsync(); JObject o = JObject.Parse(responseJson); item._id = o.GetValue("id").ToString(); item._rev = o.GetValue("rev").ToString(); return(item); } return(null); }
public async Task <TodoItem> DeleteItemAsync(string _id, string _rev) { HttpClient dbclient = await TodoDbService.CouchDbClientAsync(); var response = await dbclient.DeleteAsync(dbname + "/" + WebUtility.UrlEncode(_id) + "?rev=" + WebUtility.UrlEncode(_rev)); if (response.IsSuccessStatusCode) { var responseJson = await response.Content.ReadAsStringAsync(); TodoItem ret = new TodoItem(); JObject o = JObject.Parse(responseJson); ret._id = o.GetValue("id").ToString(); ret._rev = o.GetValue("rev").ToString(); return(ret); } return(null); }