Skip to content

Commit 1403c2c

Browse files
add support for query database api ✨
- refactor code - add missing Mention property in RichTextMention - add JsonSubtypes converter - add test case
1 parent 46cb1d2 commit 1403c2c

File tree

10 files changed

+659
-31
lines changed

10 files changed

+659
-31
lines changed

Src/Notion.Client/DatabaseClient.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Notion.Client
77
public interface IDatabaseClient
88
{
99
Task<Database> RetrieveAsync(string databaseId);
10+
Task<PaginatedList<Page>> QueryAsync(string databaseId, DatabasesQueryParameters databasesQueryParameters);
1011
Task<PaginatedList<Database>> ListAsync(DatabasesListParameters databasesListParameters = null);
1112
}
1213

@@ -36,10 +37,11 @@ public async Task<PaginatedList<Database>> ListAsync(DatabasesListParameters dat
3637
{
3738
try
3839
{
40+
var databasesListQueryParmaters = (IDatabasesListQueryParmaters)databasesListParameters;
3941
var queryParams = new Dictionary<string, string>()
4042
{
41-
{ "start_cursor", databasesListParameters?.PaginationParameters?.StartCursor },
42-
{ "page_size", databasesListParameters?.PaginationParameters?.PageSize }
43+
{ "start_cursor", databasesListQueryParmaters?.StartCursor },
44+
{ "page_size", databasesListQueryParmaters?.PageSize }
4345
};
4446

4547
return await _client.GetAsync<PaginatedList<Database>>("databases", queryParams);
@@ -50,5 +52,18 @@ public async Task<PaginatedList<Database>> ListAsync(DatabasesListParameters dat
5052
return null;
5153
}
5254
}
55+
56+
public async Task<PaginatedList<Page>> QueryAsync(string databaseId, DatabasesQueryParameters databasesQueryParameters)
57+
{
58+
try
59+
{
60+
var body = (IDatabaseQueryBodyParameters)databasesQueryParameters;
61+
return await _client.PostAsync<PaginatedList<Page>>($"databases/{databaseId}/query", body);
62+
}
63+
catch (Exception e)
64+
{
65+
return null;
66+
}
67+
}
5368
}
5469
}

Src/Notion.Client/Models/Database.cs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.Serialization;
4+
using JsonSubTypes;
45
using Newtonsoft.Json;
56
using Newtonsoft.Json.Converters;
67
using Newtonsoft.Json.Linq;
78

89
namespace Notion.Client
910
{
10-
public class DatabasesListParameters
11+
public interface IDatabasesListQueryParmaters : IPaginationParameters
1112
{
12-
public PaginationParameters PaginationParameters { get; set; }
13+
14+
}
15+
16+
public class DatabasesListParameters : IDatabasesListQueryParmaters
17+
{
18+
public string StartCursor { get; set; }
19+
public string PageSize { get; set; }
20+
}
21+
22+
23+
public interface IDatabaseQueryBodyParameters : IPaginationParameters
24+
{
25+
Filter Filter { get; set; }
26+
List<Sort> Sorts { get; set; }
27+
}
28+
29+
public class DatabasesQueryParameters : IDatabaseQueryBodyParameters
30+
{
31+
public Filter Filter { get; set; }
32+
public List<Sort> Sorts { get; set; }
33+
public string StartCursor { get; set; }
34+
public string PageSize { get; set; }
1335
}
1436

1537
public class Database
@@ -29,6 +51,10 @@ public class Database
2951
public Dictionary<string, Property> Properties { get; set; }
3052
}
3153

54+
[JsonConverter(typeof(JsonSubtypes), "type")]
55+
[JsonSubtypes.KnownSubType(typeof(RichTextText), RichTextType.Text)]
56+
[JsonSubtypes.KnownSubType(typeof(RichTextEquation), RichTextType.Equation)]
57+
[JsonSubtypes.KnownSubType(typeof(RichTextMention), RichTextType.Mention)]
3258
public class RichTextBase
3359
{
3460
[JsonProperty("plain_text")]
@@ -38,6 +64,7 @@ public class RichTextBase
3864

3965
public Annotations Annotations { get; set; }
4066

67+
[JsonConverter(typeof(StringEnumConverter))]
4168
public virtual RichTextType Type { get; set; }
4269
}
4370

@@ -62,7 +89,7 @@ public class Link
6289
public class RichTextMention : RichTextBase
6390
{
6491
public override RichTextType Type => RichTextType.Mention;
65-
public int MyProperty { get; set; }
92+
public Mention Mention { get; set; }
6693
}
6794

6895
public class Mention
@@ -71,30 +98,14 @@ public class Mention
7198
public User User { get; set; }
7299
public ObjectId Page { get; set; }
73100
public ObjectId Database { get; set; }
74-
public PropertyValue Date { get; set; }
101+
public DatePropertyValue Date { get; set; }
75102
}
76103

77104
public class ObjectId
78105
{
79106
public string Id { get; set; }
80107
}
81108

82-
public class PropertyValue
83-
{
84-
public string Id { get; set; }
85-
public string Type { get; set; }
86-
87-
// DateProperty Value
88-
public Date Date { get; set; }
89-
}
90-
91-
public class Date
92-
{
93-
public string Start { get; set; }
94-
public string End { get; set; }
95-
}
96-
97-
98109
public class RichTextEquation : RichTextBase
99110
{
100111
public override RichTextType Type => RichTextType.Equation;
@@ -134,7 +145,7 @@ public class Annotations
134145

135146
[JsonProperty("code")]
136147
public bool IsCode { get; set; }
137-
148+
138149
// color: Color | BackgroundColor
139150
public string Color { get; set; }
140151
}
@@ -437,10 +448,10 @@ public class PropertyConverter : JsonConverter<Property>
437448
{
438449
public override Property ReadJson(JsonReader reader, Type objectType, Property existingValue, bool hasExistingValue, JsonSerializer serializer)
439450
{
440-
var jsonObject = JObject.Load(reader);
451+
var jsonObject = JObject.Load(reader);
441452
var type = jsonObject["type"].Value<string>();
442453

443-
switch(type)
454+
switch (type)
444455
{
445456
case "title":
446457
return jsonObject.ToObject<TitleProperty>();

Src/Notion.Client/Models/Filter.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
5+
6+
namespace Notion.Client
7+
{
8+
public class Filter
9+
{
10+
11+
}
12+
13+
public class SinglePropertyFilter : Filter
14+
{
15+
public string Property { get; set; }
16+
}
17+
18+
public class TextFilter : SinglePropertyFilter
19+
{
20+
[JsonProperty("equals")]
21+
public string Equal { get; set; }
22+
23+
[JsonProperty("does_not_equal")]
24+
public string DoesNotEqual { get; set; }
25+
26+
public string Contains { get; set; }
27+
28+
[JsonProperty("does_not_contain")]
29+
public string DoesNotContain { get; set; }
30+
31+
32+
[JsonProperty("starts_with")]
33+
public string StartsWith { get; set; }
34+
35+
[JsonProperty("ends_with")]
36+
public string EndsWith { get; set; }
37+
38+
[JsonProperty("is_empty")]
39+
public bool IsEmpty => true;
40+
41+
[JsonProperty("is_not_empty")]
42+
public bool IsNotEmpty => true;
43+
}
44+
45+
public class NumberFilter : SinglePropertyFilter
46+
{
47+
[JsonProperty("equals")]
48+
public double Equal { get; set; }
49+
50+
[JsonProperty("does_not_equal")]
51+
public double DoesNotEqual { get; set; }
52+
53+
[JsonProperty("greater_than")]
54+
public double GreaterThan { get; set; }
55+
56+
[JsonProperty("less_than")]
57+
public double LessThan { get; set; }
58+
59+
[JsonProperty("greater_than_or_equal_to")]
60+
public double GreaterThanOrEqualTo { get; set; }
61+
62+
[JsonProperty("less_than_or_equal_to")]
63+
public double LessThanOrEqualTo { get; set; }
64+
65+
[JsonProperty("is_empty")]
66+
public bool IsEmpty => true;
67+
68+
[JsonProperty("is_not_empty")]
69+
public bool IsNotEmpty => true;
70+
}
71+
72+
public class CheckboxFilter : SinglePropertyFilter
73+
{
74+
[JsonProperty("equals")]
75+
public bool Equal { get; set; }
76+
77+
[JsonProperty("does_not_equal")]
78+
public bool DoesNotEqual { get; set; }
79+
}
80+
81+
public class SelectFilter : SinglePropertyFilter
82+
{
83+
[JsonProperty("equals")]
84+
public string Equal { get; set; }
85+
86+
[JsonProperty("does_not_equal")]
87+
public string DoesNotEqual { get; set; }
88+
89+
[JsonProperty("is_empty")]
90+
public bool IsEmpty => true;
91+
92+
[JsonProperty("is_not_empty")]
93+
public bool IsNotEmpty => true;
94+
}
95+
96+
public class MultiSelectFilter : SinglePropertyFilter
97+
{
98+
public string Contains { get; set; }
99+
100+
[JsonProperty("does_not_contain")]
101+
public string DoesNotContain { get; set; }
102+
103+
[JsonProperty("is_empty")]
104+
public bool IsEmpty => true;
105+
106+
[JsonProperty("is_not_empty")]
107+
public bool IsNotEmpty => true;
108+
}
109+
110+
public class DateFilter : SinglePropertyFilter
111+
{
112+
[JsonProperty("equals")]
113+
[JsonConverter(typeof(IsoDateTimeConverter))]
114+
public DateTime Equal { get; set; }
115+
116+
[JsonProperty("before")]
117+
[JsonConverter(typeof(IsoDateTimeConverter))]
118+
public DateTime Before { get; set; }
119+
120+
[JsonProperty("after")]
121+
[JsonConverter(typeof(IsoDateTimeConverter))]
122+
public DateTime After { get; set; }
123+
124+
[JsonProperty("on_or_before")]
125+
[JsonConverter(typeof(IsoDateTimeConverter))]
126+
public DateTime OnOrBefore { get; set; }
127+
128+
[JsonProperty("on_or_after")]
129+
[JsonConverter(typeof(IsoDateTimeConverter))]
130+
public DateTime OnOrAfter { get; set; }
131+
132+
[JsonProperty("past_week")]
133+
public Dictionary<string, object> PastWeek { get; set; }
134+
135+
[JsonProperty("past_month")]
136+
public Dictionary<string, object> PastMonth { get; set; }
137+
138+
[JsonProperty("past_year")]
139+
public Dictionary<string, object> PastYear { get; set; }
140+
141+
[JsonProperty("next_week")]
142+
public Dictionary<string, object> NextWeek { get; set; }
143+
144+
[JsonProperty("next_month")]
145+
public Dictionary<string, object> NextMonth { get; set; }
146+
147+
[JsonProperty("next_year")]
148+
public Dictionary<string, object> NextYear { get; set; }
149+
150+
[JsonProperty("is_empty")]
151+
public bool IsEmpty => true;
152+
153+
[JsonProperty("is_not_empty")]
154+
public bool IsNotEmpty => true;
155+
}
156+
157+
public class PeopleFilter : SinglePropertyFilter
158+
{
159+
public string Contains { get; set; }
160+
161+
[JsonProperty("does_not_contain")]
162+
public string DoesNotContain { get; set; }
163+
164+
[JsonProperty("is_empty")]
165+
public bool IsEmpty => true;
166+
167+
[JsonProperty("is_not_empty")]
168+
public bool IsNotEmpty => true;
169+
}
170+
171+
public class FilesFilter : SinglePropertyFilter
172+
{
173+
[JsonProperty("is_empty")]
174+
public bool IsEmpty => true;
175+
176+
[JsonProperty("is_not_empty")]
177+
public bool IsNotEmpty => true;
178+
}
179+
180+
public class RelationFilter : SinglePropertyFilter
181+
{
182+
public string Contains { get; set; }
183+
184+
[JsonProperty("does_not_contain")]
185+
public string DoesNotContain { get; set; }
186+
187+
[JsonProperty("is_empty")]
188+
public bool IsEmpty => true;
189+
190+
[JsonProperty("is_not_empty")]
191+
public bool IsNotEmpty => true;
192+
}
193+
194+
public class FormulaFilter : SinglePropertyFilter
195+
{
196+
public TextFilter Text { get; set; }
197+
public CheckboxFilter checkbox { get; set; }
198+
public NumberFilter number { get; set; }
199+
public DateFilter date { get; set; }
200+
}
201+
202+
203+
public class CompoundFilter : Filter
204+
{
205+
public List<Filter> Or { get; set; }
206+
public List<Filter> And { get; set; }
207+
}
208+
}

0 commit comments

Comments
 (0)