Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove UID argument from NotionDB methods #17

Merged
merged 2 commits into from
Jan 25, 2024
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
8 changes: 5 additions & 3 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
nDB := &NotionDB{
DatabaseID: os.Getenv("NOTION_DB_PAGEID"),
Token: os.Getenv("NOTION_INTEGRATION_TOKEN"),
UID: uID,
}

// Query the database with the provided uID and text
results, err := nDB.QueryDatabaseContains(message.Text, uID)
results, err := nDB.QueryDatabaseContains(message.Text)
log.Println("Got results:", results)

// If there's an error or no results, reply with an error message
Expand Down Expand Up @@ -155,10 +156,11 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
nDB := &NotionDB{
DatabaseID: os.Getenv("NOTION_DB_PAGEID"),
Token: os.Getenv("NOTION_INTEGRATION_TOKEN"),
UID: uID,
}

// Check email first before adding to database.
dbUser, err := nDB.QueryDatabaseByEmail(uID, person.Email)
dbUser, err := nDB.QueryDatabaseByEmail(person.Email)
if err == nil && len(dbUser) > 0 {
log.Println("Already exist in DB", dbUser)
if err := replyText(e.ReplyToken, "已經存在於資料庫中,請勿重複輸入"+"\n"+jsonData); err != nil {
Expand All @@ -168,7 +170,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
}

// Add namecard to notion database.
err = nDB.AddPageToDatabase(uID, person.Name, person.Title, person.Address, person.Email, person.PhoneNumber)
err = nDB.AddPageToDatabase(person.Name, person.Title, person.Address, person.Email, person.PhoneNumber)
if err != nil {
log.Println("Error adding page to database:", err)
}
Expand Down
63 changes: 31 additions & 32 deletions notion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ type Person struct {
type NotionDB struct {
DatabaseID string
Token string
UID string
}

// QueryDatabase 根據提供的屬性和值查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabase(UId, property, value string) ([]Person, error) {
func (n *NotionDB) QueryDatabase(property, value string) ([]Person, error) {
client := notionapi.NewClient(notionapi.Token(n.Token))

// Add UId to the filter conditions
// 建立查詢過濾條件
filter := &notionapi.DatabaseQueryRequest{
Filter: notionapi.AndCompoundFilter{
Expand All @@ -39,7 +39,7 @@ func (n *NotionDB) QueryDatabase(UId, property, value string) ([]Person, error)
notionapi.PropertyFilter{
Property: "UID",
RichText: &notionapi.TextFilterCondition{
Equals: UId,
Equals: n.UID,
},
},
},
Expand All @@ -61,10 +61,9 @@ func (n *NotionDB) QueryDatabase(UId, property, value string) ([]Person, error)
}

// QueryContainsDatabase 根據提供的屬性和值查詢 Notion 資料庫。
func (n *NotionDB) QueryContainsDatabase(UId, property, value string) ([]Person, error) {
func (n *NotionDB) QueryContainsDatabase(property, value string) ([]Person, error) {
client := notionapi.NewClient(notionapi.Token(n.Token))

// Add UId to the filter conditions
// 建立查詢過濾條件
filter := &notionapi.DatabaseQueryRequest{
Filter: notionapi.AndCompoundFilter{
Expand All @@ -77,7 +76,7 @@ func (n *NotionDB) QueryContainsDatabase(UId, property, value string) ([]Person,
notionapi.PropertyFilter{
Property: "UID",
RichText: &notionapi.TextFilterCondition{
Equals: UId,
Equals: n.UID,
},
},
},
Expand All @@ -98,13 +97,13 @@ func (n *NotionDB) QueryContainsDatabase(UId, property, value string) ([]Person,
return entries, nil
}

// QueryDatabaseByEmail 根據提供的電子郵件地址和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByEmail(email, UId string) ([]Person, error) {
return n.QueryDatabase(UId, "Email", email)
// QueryDatabaseByEmail 根據提供的電子郵件地址查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByEmail(email string) ([]Person, error) {
return n.QueryDatabase("Email", email)
}

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

// 建立 Properties 物件來設置頁面屬性
Expand All @@ -113,7 +112,7 @@ func (n *NotionDB) AddPageToDatabase(Uid string, name string, title string, addr
RichText: []notionapi.RichText{
{
PlainText: name,
Text: &notionapi.Text{Content: Uid},
Text: &notionapi.Text{Content: n.UID},
},
},
},
Expand Down Expand Up @@ -174,7 +173,7 @@ func (n *NotionDB) AddPageToDatabase(Uid string, name string, title string, addr
return err
}

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

Expand All @@ -200,52 +199,52 @@ func (n *NotionDB) getPropertyValue(page *notionapi.Page, property string) strin
return ""
}

// QueryDatabaseByName 根據提供的名稱和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByName(name, UId string) ([]Person, error) {
return n.QueryDatabase(UId, "Name", name)
// QueryDatabaseByName 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByName(name string) ([]Person, error) {
return n.QueryDatabase("Name", name)
}

// QueryDatabaseContainsByName 根據提供的名稱和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByName(name, UId string) ([]Person, error) {
return n.QueryContainsDatabase(UId, "Name", name)
// QueryDatabaseContainsByName 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByName(name string) ([]Person, error) {
return n.QueryContainsDatabase("Name", name)
}

// QueryDatabaseContainsByEmail 根據提供的名稱和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByEmail(name, UId string) ([]Person, error) {
return n.QueryContainsDatabase(UId, "Email", name)
// QueryDatabaseContainsByEmail 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByEmail(name string) ([]Person, error) {
return n.QueryContainsDatabase("Email", name)
}

// QueryDatabaseContainsByName 根據提供的名稱和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByTitle(name, UId string) ([]Person, error) {
return n.QueryContainsDatabase(UId, "Title", name)
// QueryDatabaseContainsByName 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByTitle(name string) ([]Person, error) {
return n.QueryContainsDatabase("Title", name)
}

// QueryDatabaseContains 根據提供的名稱和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContains(query, UId string) ([]Person, error) {
// QueryDatabaseContains 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContains(query string) ([]Person, error) {
// 初始化一個空的結果集
var combinedResult []Person

// 進行名稱查詢
log.Println("QueryDatabaseContainsByName", query, UId)
nameResult, err := n.QueryDatabaseContainsByName(query, UId)
log.Println("QueryDatabaseContainsByName", query, n.UID)
nameResult, err := n.QueryDatabaseContainsByName(query)
log.Println("QueryDatabaseContainsByName", nameResult, err)
if err != nil {
return nil, err
}
combinedResult = append(combinedResult, nameResult...)

// 進行電子郵件查詢
log.Println("QueryDatabaseContainsByEmail", query, UId)
emailResult, err := n.QueryDatabaseContainsByEmail(query, UId)
log.Println("QueryDatabaseContainsByEmail", query, n.UID)
emailResult, err := n.QueryDatabaseContainsByEmail(query)
log.Println("QueryDatabaseContainsByEmail", emailResult, err)
if err != nil {
return nil, err
}
combinedResult = append(combinedResult, emailResult...)

// 進行標題查詢
log.Println("QueryDatabaseContainsByTitle", query, UId)
titleResult, err := n.QueryDatabaseContainsByTitle(query, UId)
log.Println("QueryDatabaseContainsByTitle", query, n.UID)
titleResult, err := n.QueryDatabaseContainsByTitle(query)
log.Println("QueryDatabaseContainsByTitle", titleResult, err)
if err != nil {
return nil, err
Expand Down
15 changes: 9 additions & 6 deletions notion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ func TestQueryNotionDB(t *testing.T) {
db := &NotionDB{
DatabaseID: pageid,
Token: token,
UID: "uid",
}

entries, err := db.QueryDatabaseByName("name", "uid")
entries, err := db.QueryDatabaseByName("name")
if err != nil {
t.Fatal(err)
}
fmt.Printf("%+v\n", entries)

entries, err = db.QueryDatabaseByEmail("[email protected]", "uid")
entries, err = db.QueryDatabaseByEmail("[email protected]")
if err != nil {
t.Fatal(err)
}
Expand All @@ -46,9 +47,10 @@ func TestAddNotionDB(t *testing.T) {
db := &NotionDB{
DatabaseID: pageid,
Token: token,
UID: "uid",
}

err := db.AddPageToDatabase("uid", "name", "title", "address", "[email protected]", "phone")
err := db.AddPageToDatabase("name", "title", "address", "[email protected]", "phone")
if err != nil {
t.Fatal(err)
}
Expand All @@ -66,23 +68,24 @@ func TestQueryContainNotionDB(t *testing.T) {
db := &NotionDB{
DatabaseID: pageid,
Token: token,
UID: "uid",
}

entries, err := db.QueryDatabaseContainsByName("name", "uid")
entries, err := db.QueryDatabaseContainsByName("name")
if err != nil {
t.Fatal(err)
}
fmt.Printf("%+v\n", entries)

entries, err = db.QueryDatabaseContainsByEmail("email", "uid")
entries, err = db.QueryDatabaseContainsByEmail("email")
if err != nil {
t.Fatal(err)
}

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

//test contains all columns (name, title, email)
entries, err = db.QueryDatabaseContains("keyword", "uid")
entries, err = db.QueryDatabaseContains("keyword")
if err != nil {
t.Fatal(err)
}
Expand Down