Skip to content

Commit ca0d2a9

Browse files
committed
feat: create database if not exists
1 parent 1fedeeb commit ca0d2a9

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

.leetcode.example.json

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"api_key": "sk-xxx",
66
"model": "gpt-3.5-turbo",
77
"prompt": "您是一个算法专家,请基于下面的算法题目,给出该算法的思路和复杂度, 使用 {{ .TextLang }} 回答\nSETP1. 给出算法的归类,如递归,栈\nSETP2. 若是存在暴力解法,给出思路和复杂度\nSETP3. 给出最优解法和复杂度\nSETP4. 代码实现,使用 {{ .Lang }} 语言,代码带注释和测试样例。\n\n{{ .Problem }}"
8+
},
9+
"notion": {
10+
"token": "secret_nDvoJWo1g2TWUqYxqnArow4w3qo2NX",
11+
"database_id": "3fb81c2bf55e4fa18c66a5b0b61fa403",
12+
"page_id": "59194b5733c444eca8e7c996a13db505"
813
}
914
}
1015

cmd/sync/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Run(lc *leetcode.Leetcode, isNotion bool) {
1919

2020
if isNotion {
2121
nc := notion.NewNotion(lc.Config.Notion.Token).
22-
WithConfig("", lc.Config.Notion.DatabaseID)
22+
WithConfig(lc.Config.Notion.PageID, lc.Config.Notion.DatabaseID)
2323

2424
err := nc.Init()
2525
if err != nil {

internal/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type GptCfg struct {
1414
type NotionCfg struct {
1515
Token string `json:"token" mapstructure:"token"`
1616
DatabaseID string `json:"database_id" mapstructure:"database_id"`
17+
PageID string `json:"page_id" mapstructure:"page_id"`
1718
}
1819

1920
type Config struct {

internal/notion/notion.go

+45-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type PageData struct {
8787

8888
func ParsePage(page *notionapi.Page) (PageUID, *PageData) {
8989
var pageUID PageUID
90-
pageID := GetPageID(page.ID)
90+
pageID := GetStandardID(page.ID)
9191
data := make(map[string]string)
9292
for name, property := range page.Properties {
9393
data[name] = ParseProperty(property)
@@ -165,6 +165,22 @@ func (n *Notion) WithConfig(pageID, databaseID string) *Notion {
165165
}
166166

167167
func (n *Notion) Init() error {
168+
if n.DatabaseID == "" && n.PageID == "" {
169+
return fmt.Errorf("both database_id and page_id are empty")
170+
}
171+
// 1. 未填写database_id则创建数据库
172+
if n.DatabaseID == "" && n.PageID != "" {
173+
db, err := n.CreateDB()
174+
if err != nil {
175+
return fmt.Errorf("create database failed: %v", err)
176+
}
177+
n.DatabaseID = notionapi.DatabaseID(GetStandardID(db.ID))
178+
179+
log.Printf("Create Database: %d, Please add the database_id in the config file\n", n.DatabaseID)
180+
log.Printf("Visited Link: %s", fmt.Sprintf("https://www.notion.so/%s", n.DatabaseID))
181+
}
182+
183+
// 2. 创建完成database_id则添加记录
168184
if n.DatabaseID != "" {
169185
records, err := n.Query()
170186
if err != nil {
@@ -237,10 +253,36 @@ func (n *Notion) InsertOrUpdate(record *Record) error {
237253
return n.Insert(record)
238254
}
239255

256+
var EmptySelect = notionapi.Select{Options: make([]notionapi.Option, 0)}
257+
258+
func (n *Notion) CreateDB() (db *notionapi.Database, err error) {
259+
ctx := context.Background()
260+
261+
dbReq := &notionapi.DatabaseCreateRequest{
262+
Parent: notionapi.Parent{
263+
Type: "page_id",
264+
PageID: n.PageID,
265+
},
266+
Title: []notionapi.RichText{{Text: &notionapi.Text{Content: "Leetcode"}}},
267+
Properties: notionapi.PropertyConfigs{
268+
"Link": &notionapi.URLPropertyConfig{Type: "url"},
269+
"Tags": &notionapi.MultiSelectPropertyConfig{Type: "multi_select", MultiSelect: EmptySelect},
270+
"Solved": &notionapi.SelectPropertyConfig{Type: "select", Select: EmptySelect},
271+
"Difficulty": &notionapi.SelectPropertyConfig{Type: "select", Select: EmptySelect},
272+
"ID": &notionapi.RichTextPropertyConfig{Type: "rich_text"},
273+
"_id": &notionapi.RichTextPropertyConfig{Type: "rich_text"},
274+
"Name": &notionapi.TitlePropertyConfig{Type: "title"},
275+
},
276+
IsInline: true, // show database inline in the parent page
277+
}
278+
279+
return n.client.Database.Create(ctx, dbReq)
280+
}
281+
240282
func GetPageSig(v map[string]string) string {
241283
return fmt.Sprintf("%s_%s_%s_%s_%s_%s", v["Difficulty"], v["ID"], v["Link"], v["Name"], v["Solved"], v["Tags"])
242284
}
243285

244-
func GetPageID(objectID notionapi.ObjectID) string {
245-
return strings.ReplaceAll(objectID.String(), "-", "")
286+
func GetStandardID(stringer fmt.Stringer) string {
287+
return strings.ReplaceAll(stringer.String(), "-", "")
246288
}

internal/notion/notion_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@ func TestNotion(t *testing.T) {
4545
err := notion.Update("465b111ca3e74113a0fc382aa1d3dfa6", MetaToRecord(m))
4646
assert.NoError(t, err)
4747
})
48+
4849
t.Run("InsertOrUpdate", func(t *testing.T) {
4950
_ = notion.Init()
5051

5152
m.Title = "test"
5253
err := notion.InsertOrUpdate(MetaToRecord(m))
5354
assert.NoError(t, err)
5455
})
56+
57+
t.Run("CreateDB", func(t *testing.T) {
58+
db, err := notion.CreateDB()
59+
assert.NoError(t, err)
60+
t.Log(db)
61+
})
5562
}
5663

5764
func MetaToRecord(e *meta.Meta) *Record {

0 commit comments

Comments
 (0)