Skip to content

Commit

Permalink
implements update document
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen committed Nov 29, 2020
1 parent 06dda53 commit f259efe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
- [x] Create collection
- [x] Create document
- [x] Upsert document
- [ ] Update document
- [ ] Bulk documents
- [x] Search collection
- [x] Retrieve document
- [x] Update document
- [x] Delete document
- [x] Filter Delete document
- [x] Retrieve collection
- [ ] Export documents
- [ ] Import documents
Expand Down Expand Up @@ -103,10 +101,16 @@ var searchResult = await typesenseClient.Search<Address>("Addresses", query);
var retrievedDocument = await typesenseClient.RetrieveDocument<Address>("Addresses", "1");
```

## Update document on id

``` c#
var updateDocumentResult = await typesenseClient.UpdateDocument("Addresses", "1", address);
```

## Delete document on id

``` c#
var deleteResult = await typesenseClient.DeleteDocument<Address>("Addresses", "2");
var deleteResult = await typesenseClient.DeleteDocument<Address>("Addresses", "1");
```

## Delete documents using filter
Expand Down
4 changes: 4 additions & 0 deletions examples/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ async static Task Main(string[] args)
await typesenseClient.UpsertDocument("Addresses", houseOne);
await typesenseClient.UpsertDocument("Addresses", houseTwo);

houseFour.HouseNumber = 1;
var updateDocumentResult = await typesenseClient.UpdateDocument("Addresses", "4", houseFour);
Console.WriteLine($"Updated document: {JsonSerializer.Serialize(updateDocumentResult)}");

var query = new SearchParameters
{
Text = "Smed",
Expand Down
17 changes: 17 additions & 0 deletions src/Typesense/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public async Task<T> RetrieveDocument<T>(string collection, string id)
return JsonSerializer.Deserialize<T>(response, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}

public async Task<T> UpdateDocument<T>(string collection, string id, T document)
{
var response = await Patch($"collections/{collection}/documents/{id}", document);
return JsonSerializer.Deserialize<T>(response, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}

public async Task<Collection> RetrieveCollection(string schema)
{
var response = await Get($"/collections/{schema}");
Expand Down Expand Up @@ -128,5 +134,16 @@ private async Task<string> Delete(string path)

return await response.Content.ReadAsStringAsync();
}

private async Task<string> Patch(string path, object obj)
{
var jsonString = JsonSerializer.Serialize(obj, obj.GetType(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
var response = await _httpClient.PatchAsync(path, new StringContent(jsonString, Encoding.UTF8, "application/json"));

if (!response.IsSuccessStatusCode)
throw new Exception(await response.Content.ReadAsStringAsync());

return await response.Content.ReadAsStringAsync();
}
}
}
1 change: 1 addition & 0 deletions src/Typesense/ITypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface ITypesenseClient
Task UpsertDocument(string collection, object document);
Task<SearchResult<T>> Search<T>(string schema, SearchParameters searchParameters);
Task<T> RetrieveDocument<T>(string collection, string id);
Task<T> UpdateDocument<T>(string collection, string id, T document);
Task<Collection> RetrieveCollection(string schema);
Task<T> DeleteDocument<T>(string collection, string documentId);
Task<FilterDeleteResponse> DeleteDocument(string collection, string filter, int batchSize);
Expand Down

0 comments on commit f259efe

Please sign in to comment.