1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-15 00:05:04 +02:00

Add "update page properties" endpoint support

This commit is contained in:
David Stotijn
2021-05-15 18:06:11 +02:00
parent b2af8b7d0b
commit e32f7422cd
3 changed files with 95 additions and 21 deletions

45
page.go
View File

@ -42,10 +42,16 @@ type PageTitle struct {
type DatabasePageProperties map[string]DatabasePageProperty
type DatabasePageProperty struct {
DatabaseProperty
RichText []RichText `json:"rich_text"`
Select *SelectOptions `json:"select"`
MultiSelect []SelectOptions `json:"multi_select"`
ID string `json:"id,omitempty"`
Type DatabasePropertyType `json:"type"`
RichText []RichText `json:"rich_text,omitempty"`
Number *NumberMetadata `json:"number,omitempty"`
Select *SelectOptions `json:"select,omitempty"`
MultiSelect []SelectOptions `json:"multi_select,omitempty"`
Formula *FormulaMetadata `json:"formula,omitempty"`
Relation *RelationMetadata `json:"relation,omitempty"`
Rollup *RollupMetadata `json:"rollup,omitempty"`
}
// CreatePageParams are the params used for creating a page.
@ -61,6 +67,12 @@ type CreatePageParams struct {
Children []Block
}
type UpdatePageParams struct {
// Either DatabasePageProperties or Title must be not nil.
DatabasePageProperties *DatabasePageProperties
Title []RichText
}
type ParentType string
const (
@ -166,3 +178,28 @@ func (p *Page) UnmarshalJSON(b []byte) error {
return nil
}
func (p UpdatePageParams) Validate() error {
if p.DatabasePageProperties == nil && p.Title == nil {
return errors.New("either database page properties or title is required")
}
return nil
}
func (p UpdatePageParams) MarshalJSON() ([]byte, error) {
type UpdatePageParamsDTO struct {
Properties interface{} `json:"properties"`
}
var dto UpdatePageParamsDTO
if p.DatabasePageProperties != nil {
dto.Properties = p.DatabasePageProperties
} else if p.Title != nil {
dto.Properties = PageTitle{
Title: p.Title,
}
}
return json.Marshal(dto)
}