8000 Add overload method to create a page. · hognevevle/notion-sdk-net@e9ad85f · GitHub
[go: up one dir, main page]

Skip to content

Commit e9ad85f

Browse files
Add overload method to create a page.
* Fix breaking test case
1 parent 79e16c9 commit e9ad85f

File tree

9 files changed

+103
-2
lines changed

9 files changed

+103
-2
lines changed

Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/ParentPageInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Notion.Client
44
{
5-
public class ParentPageInput
5+
public class ParentPageInput : IPageParentInput
66
{
77
[JsonProperty("page_id")]
88
public string PageId { get; set; }

Src/Notion.Client/Api/Pages/IPagesClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ namespace Notion.Client
66
public interface IPagesClient
77
{
88
Task<Page> CreateAsync(NewPage page);
9+
10+
/// <summary>
11+
/// Creates a new page in the specified database or as a child of an existing page.
12+
///
13+
/// If the parent is a database, the <see href="https://developers.notion.com/reference-link/page#property-value-object">property values</see> of the new page in the properties parameter must conform to the parent <see href="https://developers.notion.com/reference-link/database">database</see>'s property schema.
14+
///
15+
/// If the parent is a page, the only valid property is <strong>title</strong>.
16+
/// </summary>
17+
/// <param name="pagesCreateParameters">Create page parameters</param>
18+
/// <returns>Created page.</returns>
19+
Task<Page> CreateAsync(PagesCreateParameters pagesCreateParameters);
20+
921
Task<Page> RetrieveAsync(string pageId);
1022

1123
Task<Page> UpdatePropertiesAsync(

Src/Notion.Client/Api/Pages/PagesClient.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ public async Task<Page> CreateAsync(NewPage page)
3434
return await _client.PostAsync<Page>(PagesApiUrls.Create(), page);
3535
}
3636

37+
/// <summary>
38+
/// Creates a new page in the specified database or as a child of an existing page.
39+
///
40+
/// If the parent is a database, the <see href="https://developers.notion.com/reference-link/page#property-value-object">property values</see> of the new page in the properties parameter must conform to the parent <see href="https://developers.notion.com/reference-link/database">database</see>'s property schema.
41+
///
42+
/// If the parent is a page, the only valid property is <strong>title</strong>.
43+
/// </summary>
44+
/// <param name="pagesCreateParameters">Create page parameters</param>
45+
/// <returns>Created page.</returns>
46+
public async Task<Page> CreateAsync(PagesCreateParameters pagesCreateParameters)
47+
{
48+
if (pagesCreateParameters is null)
49+
{
50+
throw new ArgumentNullException(nameof(pagesCreateParameters));
51+
}
52+
53+
var bodyParameters = (IPagesCreateBodyParameters)pagesCreateParameters;
54+
55+
if (bodyParameters.Parent == null)
56+
{
57+
throw new ArgumentNullException(nameof(bodyParameters.Parent), "Parent is required!");
58+
}
59+
60+
if (bodyParameters.Properties == null)
61+
{
62+
throw new ArgumentNullException(nameof(bodyParameters.Properties), "Properties are required!");
63+
}
64+
65+
return await _client.PostAsync<Page>(PagesApiUrls.Create(), bodyParameters);
66+
}
67+
3768
public async Task<Page> RetrieveAsync(string pageId)
3869
{
3970
var url = PagesApiUrls.Retrieve(pageId);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class DatabaseParentInput : IPageParentInput
6+
{
7+
[JsonProperty("database_id")]
8+
public string DatabaseId { get; set; }
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Notion.Client
2+
{
3+
public interface IPageParentInput
4+
{
5+
}
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace Notion.Client
5+
{
6+
public interface IPagesCreateBodyParameters
7+
{
8+
[JsonProperty("parent")]
9+
IPageParent Parent { get; set; }
10+
11+
[JsonProperty("properties")]
12+
IDictionary<string, PropertyValue> Properties { get; set; }
13+
14+
[JsonProperty("children")]
15+
IList<Block> Children { get; set; }
16+
17+
[JsonProperty("icon")]
18+
IPageIcon Icon { get; set; }
19+
20+
[JsonProperty("cover")]
21+
FileObject Cover { get; set; }
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Notion.Client
2+
{
3+
public interface IPagesCreateQueryParameters
4+
{
5+
}
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace Notion.Client
4+
{
5+
public class PagesCreateParameters : IPagesCreateBodyParameters, IPagesCreateQueryParameters
6+
{
7+
public IPageParent Parent { get; set; }
8+
public IDictionary<string, PropertyValue> Properties { get; set; }
9+
public IList<Block> Children { get; set; }
10+
public IPageIcon Icon { get; set; }
11+
public FileObject Cover { get; set; }
12+
}
13+
}

Test/Notion.UnitTests/PagesClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public async Task ArchivePageAsync()
201201
[Fact]
202202
public async Task CreateAsync_Throws_ArgumentNullException_When_Parameter_Is_Null()
203203
{
204-
Func<Task> act = async () => await _client.CreateAsync(null);
204+
Func<Task> act = async () => await _client.CreateAsync(page: null);
205205

206206
(await act.Should().ThrowAsync<ArgumentNullException>()).And.ParamName.Should().Be("page");
207207
}

0 commit comments

Comments
 (0)
0