Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Src/Notion.Client/Api/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Src/Notion.Client/Api/Databases/DatabasesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,12 @@ public async Task<Database> CreateAsync(DatabasesCreateParameters databasesCreat

return await _client.PostAsync<Database>(DatabasesApiUrls.Create, body);
}

public async Task<Database> UpdateAsync(string databaseId, DatabasesUpdateParameters databasesUpdateParameters)
{
var body = (IDatabasesUpdateBodyParameters)databasesUpdateParameters;

return await _client.PatchAsync<Database>(DatabasesApiUrls.Update(databaseId), body);
}
}
}
8 changes: 8 additions & 0 deletions Src/Notion.Client/Api/Databases/IDatabasesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ public interface IDatabasesClient
/// <param name="databasesCreateParameters"></param>
/// <returns>Database</returns>
Task<Database> CreateAsync(DatabasesCreateParameters databasesCreateParameters);

/// <summary>
/// Updates an existing database as specified by the parameters.
/// </summary>
/// <param name="databaseId"></param>
/// <param name="databasesUpdateParameters"></param>
/// <returns>Database</returns>
Task<Database> UpdateAsync(string databaseId, DatabasesUpdateParameters databasesUpdateParameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace Notion.Client
{
public interface IDatabasesUpdateBodyParameters
{
Dictionary<string, IUpdatePropertySchema> Properties { get; set; }
List<RichTextBaseInput> Title { get; set; }
}

public class DatabasesUpdateParameters : IDatabasesUpdateBodyParameters
{
public Dictionary<string, IUpdatePropertySchema> Properties { get; set; }
public List<RichTextBaseInput> Title { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class CheckboxUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> Checkbox { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class CreatedByUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("created_by")]
public Dictionary<string, object> CreatedBy { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class CreatedTimeUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("created_time")]
public Dictionary<string, object> CreatedTime { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class DateUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> Date { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class EmailUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> Email { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class FilesUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> File { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Notion.Client
{
public interface IUpdatePropertySchema
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class LastEditedByUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("last_edited_by")]
public Dictionary<string, object> LastEditedBy { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class LastEditedTimeUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("last_edited_time")]
public Dictionary<string, object> LastEditedTime { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class MultiSelectUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("multi_select")]
public OptionWrapper<SelectOption> MultiSelect { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Notion.Client
{
public class NumberUpdatePropertySchema : IUpdatePropertySchema
{
public Number Number { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class PeopleUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> People { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class PhoneNumberUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("phone_number")]
public Dictionary<string, object> PhoneNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class RichTextUpdatePropertySchema : IUpdatePropertySchema
{
[JsonProperty("rich_text")]
public Dictionary<string, object> RichText { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Notion.Client
{
public class SelectUpdatePropertySchema : IUpdatePropertySchema
{
public OptionWrapper<SelectOption> Select { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class TitleUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> Title { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class URLUpdatePropertySchema : IUpdatePropertySchema
{
public Dictionary<string, object> Url { get; set; }
}
}
101 changes: 101 additions & 0 deletions Test/Notion.UnitTests/DatabasesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RichTextBaseInput>
{
new RichTextTextInput
{
Text = new Text
{
Content = "Grocery List New",
Link = null
}
}
};

updateDatabaseParameters.Properties = new Dictionary<string, IUpdatePropertySchema>
{
{ "Name", new TitleUpdatePropertySchema { Title = new Dictionary<string, object>() } },
{ "Price", new NumberUpdatePropertySchema { Number = new Number { Format = NumberFormat.Yen } } },
{ "Food group", new SelectUpdatePropertySchema
{
Select = new OptionWrapper<SelectOption>
{
Options = new List<SelectOption>
{
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<string, object>() } }
};

var database = await _client.UpdateAsync(databaseId, updateDatabaseParameters);

database.Parent.Type.Should().Be(ParentType.PageId);
database.Parent.Should().BeOfType<PageParent>();
((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>();
((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);
}
}
}
3 changes: 3 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<None Update="data\databases\DatabasePropertyObjectContainParentProperty.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\databases\UpdateDatabaseResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\pages\CreatePageResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Loading