Skip to content

Commit

Permalink
Merge pull request #20 from kkdai/add_company_support
Browse files Browse the repository at this point in the history
refactor: refactor `AddPageToDatabase` function to use `Person` struc…
  • Loading branch information
kkdai committed Jan 25, 2024
2 parents f21eeba + 2997dc1 commit 66ea46b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
}

// Add namecard to notion database.
err = nDB.AddPageToDatabase(person.Name, person.Title, person.Address, person.Email, person.PhoneNumber)
err = nDB.AddPageToDatabase(person)
if err != nil {
log.Println("Error adding page to database:", err)
}
Expand Down
36 changes: 23 additions & 13 deletions notion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Person struct {
Address string `json:"address"`
Email string `json:"email"`
PhoneNumber string `json:"phone_number"`
Company string `json:"company"`
}

// DatabaseEntry 定義了 Notion 資料庫條目的結構體。
Expand Down Expand Up @@ -93,56 +94,64 @@ func (n *NotionDB) QueryDatabaseByEmail(email string) ([]Person, error) {
}

// AddPageToDatabase adds a new page with the provided field values to the specified Notion database.
func (n *NotionDB) AddPageToDatabase(name string, title string, address string, email string, phoneNumber string) error {
func (n *NotionDB) AddPageToDatabase(person Person) error {
client := notionapi.NewClient(notionapi.Token(n.Token))

// 建立 Properties 物件來設置頁面屬性
properties := notionapi.Properties{
"UID": notionapi.TitleProperty{
Title: []notionapi.RichText{
{
PlainText: name,
PlainText: n.UID,
Text: &notionapi.Text{Content: n.UID},
},
},
},
"Name": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: name,
Text: &notionapi.Text{Content: name},
PlainText: person.Name,
Text: &notionapi.Text{Content: person.Name},
},
},
},
"Title": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: name,
Text: &notionapi.Text{Content: title},
PlainText: person.Title,
Text: &notionapi.Text{Content: person.Title},
},
},
},
"Address": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: name,
Text: &notionapi.Text{Content: address},
PlainText: person.Address,
Text: &notionapi.Text{Content: person.Address},
},
},
},
"Email": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: name,
Text: &notionapi.Text{Content: email},
PlainText: person.Email,
Text: &notionapi.Text{Content: person.Email},
},
},
},
"Phone Number": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: name,
Text: &notionapi.Text{Content: phoneNumber},
PlainText: person.PhoneNumber,
Text: &notionapi.Text{Content: person.PhoneNumber},
},
},
},
"Compyny": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Company,
Text: &notionapi.Text{Content: person.Company},
},
},
},
Expand All @@ -163,7 +172,7 @@ func (n *NotionDB) AddPageToDatabase(name string, title string, address string,
return err
}

log.Println("Page added successfully:", n.UID, name, title, address, email, phoneNumber)
log.Println("Page added successfully:", n.UID, person)
return nil
}

Expand All @@ -176,6 +185,7 @@ func (n *NotionDB) createEntryFromPage(page *notionapi.Page) Person {
entry.Address = n.getPropertyValue(page, "Address")
entry.Email = n.getPropertyValue(page, "Email")
entry.PhoneNumber = n.getPropertyValue(page, "Phone Number")
entry.Company = n.getPropertyValue(page, "Company")

return entry
}
Expand Down
3 changes: 2 additions & 1 deletion notion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestAddNotionDB(t *testing.T) {
UID: "uid",
}

err := db.AddPageToDatabase("name", "title", "address", "emai@email.com", "phone")
err := db.AddPageToDatabase(Person{Name: "test", Title: "test", Address: "test", Email: "test", PhoneNumber: "test", Company: "test"})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -91,4 +91,5 @@ func TestQueryContainNotionDB(t *testing.T) {
}

fmt.Printf("%+v\n", entries)

}

0 comments on commit 66ea46b

Please sign in to comment.