diff --git a/README.md b/README.md index 12ba7a16..ab97f23f 100644 --- a/README.md +++ b/README.md @@ -93,11 +93,12 @@ var complexFiler = new CompoundFilter( ``` ## Supported Endpoints -- [ ] Databases +- [x] Databases - [x] Retrieve a database - [x] Query a database - [x] List databases - - [x] Create a Database + - [x] Create a database + - [x] Update database - [x] Pages - [x] Retrieve a page - [x] Create a page diff --git a/Src/Notion.Client/Api/ApiEndpoints.cs b/Src/Notion.Client/Api/ApiEndpoints.cs index 4340b5af..ed531c0b 100644 --- a/Src/Notion.Client/Api/ApiEndpoints.cs +++ b/Src/Notion.Client/Api/ApiEndpoints.cs @@ -8,6 +8,7 @@ public static class DatabasesApiUrls public static string List() => "/v1/databases"; public static string Query(string databaseId) => $"/v1/databases/{databaseId}/query"; public static string Create => "/v1/databases"; + public static string Update(string databaseId) => $"/v1/databases/{databaseId}"; } public static class UsersApiUrls diff --git a/Src/Notion.Client/Api/Databases/DatabasesClient.cs b/Src/Notion.Client/Api/Databases/DatabasesClient.cs index c263880a..5a7570c1 100644 --- a/Src/Notion.Client/Api/Databases/DatabasesClient.cs +++ b/Src/Notion.Client/Api/Databases/DatabasesClient.cs @@ -44,5 +44,12 @@ public async Task CreateAsync(DatabasesCreateParameters databasesCreat return await _client.PostAsync(DatabasesApiUrls.Create, body); } + + public async Task UpdateAsync(string databaseId, DatabasesUpdateParameters databasesUpdateParameters) + { + var body = (IDatabasesUpdateBodyParameters)databasesUpdateParameters; + + return await _client.PatchAsync(DatabasesApiUrls.Update(databaseId), body); + } } } diff --git a/Src/Notion.Client/Api/Databases/IDatabasesClient.cs b/Src/Notion.Client/Api/Databases/IDatabasesClient.cs index 5b2ed741..e6f591cb 100644 --- a/Src/Notion.Client/Api/Databases/IDatabasesClient.cs +++ b/Src/Notion.Client/Api/Databases/IDatabasesClient.cs @@ -14,5 +14,13 @@ public interface IDatabasesClient /// /// Database Task CreateAsync(DatabasesCreateParameters databasesCreateParameters); + + /// + /// Updates an existing database as specified by the parameters. + /// + /// + /// + /// Database + Task UpdateAsync(string databaseId, DatabasesUpdateParameters databasesUpdateParameters); } } diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/DatabasesUpdateParameters.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/DatabasesUpdateParameters.cs new file mode 100644 index 00000000..aa8e7a56 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/DatabasesUpdateParameters.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public interface IDatabasesUpdateBodyParameters + { + Dictionary Properties { get; set; } + List Title { get; set; } + } + + public class DatabasesUpdateParameters : IDatabasesUpdateBodyParameters + { + public Dictionary Properties { get; set; } + public List Title { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CheckboxUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CheckboxUpdatePropertySchema.cs new file mode 100644 index 00000000..47ef1cba --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CheckboxUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class CheckboxUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary Checkbox { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CreatedByUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CreatedByUpdatePropertySchema.cs new file mode 100644 index 00000000..e8b52cb8 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CreatedByUpdatePropertySchema.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class CreatedByUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("created_by")] + public Dictionary CreatedBy { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CreatedTimeUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CreatedTimeUpdatePropertySchema.cs new file mode 100644 index 00000000..291cb6be --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/CreatedTimeUpdatePropertySchema.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class CreatedTimeUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("created_time")] + public Dictionary CreatedTime { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/DateUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/DateUpdatePropertySchema.cs new file mode 100644 index 00000000..6d40c768 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/DateUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class DateUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary Date { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/EmailUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/EmailUpdatePropertySchema.cs new file mode 100644 index 00000000..f359314e --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/EmailUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class EmailUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary Email { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/FilesUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/FilesUpdatePropertySchema.cs new file mode 100644 index 00000000..951d5667 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/FilesUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class FilesUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary File { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/IUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/IUpdatePropertySchema.cs new file mode 100644 index 00000000..c652d079 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/IUpdatePropertySchema.cs @@ -0,0 +1,6 @@ +namespace Notion.Client +{ + public interface IUpdatePropertySchema + { + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/LastEditedByUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/LastEditedByUpdatePropertySchema.cs new file mode 100644 index 00000000..7e18714c --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/LastEditedByUpdatePropertySchema.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class LastEditedByUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("last_edited_by")] + public Dictionary LastEditedBy { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/LastEditedTimeUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/LastEditedTimeUpdatePropertySchema.cs new file mode 100644 index 00000000..801e04b3 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/LastEditedTimeUpdatePropertySchema.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class LastEditedTimeUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("last_edited_time")] + public Dictionary LastEditedTime { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/MultiSelectUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/MultiSelectUpdatePropertySchema.cs new file mode 100644 index 00000000..05d7ceab --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/MultiSelectUpdatePropertySchema.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class MultiSelectUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("multi_select")] + public OptionWrapper MultiSelect { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/NumberUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/NumberUpdatePropertySchema.cs new file mode 100644 index 00000000..baae3223 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/NumberUpdatePropertySchema.cs @@ -0,0 +1,7 @@ +namespace Notion.Client +{ + public class NumberUpdatePropertySchema : IUpdatePropertySchema + { + public Number Number { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/PeopleUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/PeopleUpdatePropertySchema.cs new file mode 100644 index 00000000..45634994 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/PeopleUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class PeopleUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary People { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/PhoneNumberUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/PhoneNumberUpdatePropertySchema.cs new file mode 100644 index 00000000..2153b7b0 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/PhoneNumberUpdatePropertySchema.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class PhoneNumberUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("phone_number")] + public Dictionary PhoneNumber { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/RichTextUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/RichTextUpdatePropertySchema.cs new file mode 100644 index 00000000..27796cd9 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/RichTextUpdatePropertySchema.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Notion.Client +{ + public class RichTextUpdatePropertySchema : IUpdatePropertySchema + { + [JsonProperty("rich_text")] + public Dictionary RichText { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/SelectUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/SelectUpdatePropertySchema.cs new file mode 100644 index 00000000..705d35db --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/SelectUpdatePropertySchema.cs @@ -0,0 +1,7 @@ +namespace Notion.Client +{ + public class SelectUpdatePropertySchema : IUpdatePropertySchema + { + public OptionWrapper Select { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/TitleUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/TitleUpdatePropertySchema.cs new file mode 100644 index 00000000..d643e61c --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/TitleUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class TitleUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary Title { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/URLUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/URLUpdatePropertySchema.cs new file mode 100644 index 00000000..6b7a1907 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/URLUpdatePropertySchema.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Notion.Client +{ + public class URLUpdatePropertySchema : IUpdatePropertySchema + { + public Dictionary Url { get; set; } + } +} diff --git a/Test/Notion.UnitTests/DatabasesClientTests.cs b/Test/Notion.UnitTests/DatabasesClientTests.cs index dd58dc13..1e8ab15e 100644 --- a/Test/Notion.UnitTests/DatabasesClientTests.cs +++ b/Test/Notion.UnitTests/DatabasesClientTests.cs @@ -255,5 +255,106 @@ public async Task CreateDatabaseAsync() } ); } + + [Fact] + public async Task UpdateDatabaseAsync() + { + var databaseId = "1e9eee34-9c5c-4fe6-a4e1-8244eb141ed8"; + var path = ApiEndpoints.DatabasesApiUrls.Update(databaseId); + var jsonData = await File.ReadAllTextAsync("data/databases/UpdateDatabaseResponse.json"); + + Server.Given(CreatePatchRequestBuilder(path)) + .RespondWith( + Response.Create() + .WithStatusCode(200) + .WithBody(jsonData) + ); + + var updateDatabaseParameters = new DatabasesUpdateParameters(); + + updateDatabaseParameters.Title = new List + { + new RichTextTextInput + { + Text = new Text + { + Content = "Grocery List New", + Link = null + } + } + }; + + updateDatabaseParameters.Properties = new Dictionary + { + { "Name", new TitleUpdatePropertySchema { Title = new Dictionary() } }, + { "Price", new NumberUpdatePropertySchema { Number = new Number { Format = NumberFormat.Yen } } }, + { "Food group", new SelectUpdatePropertySchema + { + Select = new OptionWrapper + { + Options = new List + { + new SelectOption + { + Color = Color.Green, + Name = "🥦Vegetables" + }, + new SelectOption + { + Color = Color.Red, + Name = "🍎Fruit" + }, + new SelectOption + { + Color = Color.Yellow, + Name = "💪Protein" + } + } + } + } + }, + { "Last ordered", new DateUpdatePropertySchema{ Date = new Dictionary() } } + }; + + var database = await _client.UpdateAsync(databaseId, updateDatabaseParameters); + + database.Parent.Type.Should().Be(ParentType.PageId); + database.Parent.Should().BeOfType(); + ((PageParent)database.Parent).PageId.Should().Be("533578e3-edf1-4c0a-91a9-da6b09bac3ee"); + + database.Properties.Should().HaveCount(4); + + database.Title.Should().ContainSingle(); + database.Title.Should().SatisfyRespectively( + title => + { + title.Should().BeAssignableTo(); + ((RichTextText)title).Text.Content.Should().Be("Grocery List New"); + } + ); + + var selectOptions = (SelectProperty)database.Properties["Food group"]; + selectOptions.Name.Should().Be("Food group"); + selectOptions.Select.Options.Should().SatisfyRespectively( + option => + { + option.Name.Should().Be("🥦Vegetables"); + option.Color.Should().Be(Color.Green); + }, + option => + { + option.Name.Should().Be("🍎Fruit"); + option.Color.Should().Be(Color.Red); + }, + option => + { + option.Name.Should().Be("💪Protein"); + option.Color.Should().Be(Color.Yellow); + } + ); + + var price = (NumberProperty)database.Properties["Price"]; + price.Number.Format.Should().Be(NumberFormat.Yen); + } } } diff --git a/Test/Notion.UnitTests/Notion.UnitTests.csproj b/Test/Notion.UnitTests/Notion.UnitTests.csproj index 73552316..006a8b84 100644 --- a/Test/Notion.UnitTests/Notion.UnitTests.csproj +++ b/Test/Notion.UnitTests/Notion.UnitTests.csproj @@ -33,6 +33,9 @@ Always + + Always + Always diff --git a/Test/Notion.UnitTests/data/databases/UpdateDatabaseResponse.json b/Test/Notion.UnitTests/data/databases/UpdateDatabaseResponse.json new file mode 100644 index 00000000..b6c3e363 --- /dev/null +++ b/Test/Notion.UnitTests/data/databases/UpdateDatabaseResponse.json @@ -0,0 +1,75 @@ +{ + "object": "database", + "id": "1e9eee34-9c5c-4fe6-a4e1-8244eb141ed8", + "created_time": "2021-08-18T17:39:00.000Z", + "last_edited_time": "2021-08-18T20:00:00.000Z", + "title": [ + { + "type": "text", + "text": { + "content": "Grocery List New", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Grocery List New", + "href": null + } + ], + "properties": { + "Price": { + "id": "@xZm", + "name": "Price", + "type": "number", + "number": { + "format": "yen" + } + }, + "Last ordered": { + "id": "SN;?", + "name": "Last ordered", + "type": "date", + "date": {} + }, + "Food group": { + "id": "zIZQ", + "name": "Food group", + "type": "select", + "select": { + "options": [ + { + "id": "58c5640a-d34d-4d11-9ce4-2dcd279fa9b7", + "name": "🥦Vegetables", + "color": "green" + }, + { + "id": "3ac2b777-180e-4f45-aa3f-5d49a5bd4344", + "name": "🍎Fruit", + "color": "red" + }, + { + "id": "f0b31ad9-a33b-491c-aeb5-15dffb40dae7", + "name": "💪Protein", + "color": "yellow" + } + ] + } + }, + "Name": { + "id": "title", + "name": "Name", + "type": "title", + "title": {} + } + }, + "parent": { + "type": "page_id", + "page_id": "533578e3-edf1-4c0a-91a9-da6b09bac3ee" + } +} \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index c8a1124d..3923ddbe 100644 --- a/docs/README.md +++ b/docs/README.md @@ -63,11 +63,12 @@ var complexFiler = new CompoundFilter( ## Supported Endpoints -- [ ] Databases +- [x] Databases - [x] Retrieve a database - [x] Query a database - [x] List databases - - [x] Create a Database + - [x] Create a database + - [x] Update database - [x] Pages - [x] Retrieve a page - [x] Create a page