Пример #1
0
        /**
         * Update single entity but only the changed fields
         */
        public async Task <JToken> PatchItemAsync(string entityTypeName, Dto item)
        {
            var entitySetName = this.metadataCli.EntityTypes[entityTypeName].EntitySetName;
            var keyNames      = this.metadataCli.EntityTypes[entityTypeName].Key;
            var url           = this.apiUrl + "/crud/" + entitySetName + "?" + QueryUtils.RenderQueryString(new QueryObject()
            {
                Keys = (new List <Dto> {
                    DataAdapterUtils.GetKeyFromData(keyNames, (Dto)item["partialEntity"])
                }).ToArray()
            });

            // Info credit: http://benfoster.io/blog/adding-patch-support-to-httpclient
            HttpRequestMessage request;
            var jsonPatchItem = JsonConvert.SerializeObject(item["patchItem"]);

            using (var content = new StringContent(jsonPatchItem, Encoding.UTF8, "application/json"))
            {
                request = new HttpRequestMessage(new HttpMethod("PATCH"), url)
                {
                    Content = content
                };
            }
            var response = await this.client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();

                var resultSingleSerialDataToken = JToken.Parse(json);
                return(resultSingleSerialDataToken);
            }
            return(null);
        }
Пример #2
0
        /**
         * Delete single entity
         */
        public async Task <JToken> DeleteItemAsync(string entityTypeName, Dto partialEntity)
        {
            var entitySetName = this.metadataCli.EntityTypes[entityTypeName].EntitySetName;
            var keyNames      = this.metadataCli.EntityTypes[entityTypeName].Key;
            var url           = this.apiUrl + "/crud/" + entitySetName + "?" + QueryUtils.RenderQueryString(new QueryObject()
            {
                Keys = new List <Dto>()
                {
                    DataAdapterUtils.GetKeyFromData(keyNames, partialEntity)
                }.ToArray()
            });

            var response = await this.client.DeleteAsync(url);

            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();

                var resultSingleSerialDataToken = JToken.Parse(json);
                return(resultSingleSerialDataToken);
            }
            return(null);
        }
Пример #3
0
        /**
         * Update single entity
         */
        public async Task <JToken> PutItemAsync(string entityTypeName, Dto entity)
        {
            var entitySetName = this.metadataCli.EntityTypes[entityTypeName].EntitySetName;
            var keyNames      = this.metadataCli.EntityTypes[entityTypeName].Key;
            var url           = this.apiUrl + "/crud/" + entitySetName + "?" + QueryUtils.RenderQueryString(new QueryObject()
            {
                Keys = (new List <Dto>()
                {
                    DataAdapterUtils.GetKeyFromData(keyNames, entity)
                }).ToArray()
            });

            var jsonPatchItem = JsonConvert.SerializeObject(entity);
            var response      = await this.client.PutAsync(url, new StringContent(jsonPatchItem, Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();

                var resultSingleSerialDataToken = JToken.Parse(json);
                return(resultSingleSerialDataToken);
            }
            return(null);
        }