-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_base_row.go
187 lines (170 loc) · 8.41 KB
/
action_base_row.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package seatapi
import (
"fmt"
"github.com/ns-cn/seatapi/util"
"net/http"
"strings"
)
// ListRowsWithSQL 使用SQL方式获取行数据
// convertKeys 确定列是作为其键 (true) 返回,还是作为其名称(默认为 false)返回。
func (api SeaTableApi) ListRowsWithSQL(ctx *BaseContext, sql string, convertKeys bool) SeaTableAction[RowsWithSQL[map[string]interface{}]] {
url := api.assignUrl(ctx.DtableDb, "/api/v1/query/%s/", ctx.DtableUuid)
//url := api.wholeUrl("/dtable-db/api/v1/query/%s/", ctx.DtableUuid)
payload := strings.NewReader(fmt.Sprintf("{\"convert_keys\":%t,\"sql\":\"%s\"}", convertKeys, sql))
req, err := http.NewRequest("POST", url, payload)
if err != nil {
panic(err)
}
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[RowsWithSQL[map[string]interface{}]]{Request: req}
}
// RowsWithSQL 结构体
// Result Result的数据类型
type RowsWithSQL[Result any] struct {
ApiResult
Metadata []struct {
Key string `json:"key"`
Name string `json:"name"`
Type string `json:"type"`
Data *struct {
Options []interface{} `json:"options"`
DefaultValue interface{} `json:"default_value"`
EnableFillDefaultValue bool `json:"enable_fill_default_value,omitempty"`
} `json:"data"`
} `json:"metadata"`
Results []Result `json:"results"`
}
// ParseRowsWithSQL 将 RowsWithSQL 结构体解析为 RowsWithSQL 结构体(强制转换Result结构体)
func ParseRowsWithSQL[F any, T any](from RowsWithSQL[F], to *RowsWithSQL[T]) {
to.ApiResult = from.ApiResult
to.Metadata = from.Metadata
data := make([]T, 0)
util.ParseFromMapToPointer(from.Results, &data)
to.Results = data
}
// GetRowByRowId 获取行数据
// convertKeys 确定列是作为其键 (true) 返回,还是作为其名称(默认为 false)返回。
func (api SeaTableApi) GetRowByRowId(ctx *BaseContext, tableName string, rowId string, convertKeys bool) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/rows/%s/?table_name=%s&convert=%b", ctx.DtableUuid, rowId, tableName, convertKeys)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("accept", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// InsertRowPosition 插入位置
type InsertRowPosition string
// InsertRowPosition 定义插入位置
const (
// InsertBelow 在锚点行的下面插入
InsertBelow InsertRowPosition = "insert_below"
// InsertAbove 在锚点行的上面插入
InsertAbove InsertRowPosition = "insert_above"
)
// InsertRowAnchor 插入锚点
type InsertRowAnchor struct {
AnchorRowId string // 瞄点行
Position InsertRowPosition // 插入行位置
}
// AddRow 添加行
func (api SeaTableApi) AddRow(ctx *BaseContext, tableName string, data interface{}, anchor *InsertRowAnchor) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "row": data}
if anchor != nil && anchor.AnchorRowId != "" {
body["anchor_row_id"] = anchor.AnchorRowId
if anchor.Position != "" {
body["row_insert_position"] = anchor.Position
} else {
body["row_insert_position"] = InsertBelow
}
}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// UpdateRow 更新行
func (api SeaTableApi) UpdateRow(ctx *BaseContext, tableName string, data UpdateRowReq) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "row_id": data.RowId, "row": data.RowData}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("PUT", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// UpdateRowReq 更新行请求
type UpdateRowReq struct {
RowId string `json:"row_id"`
RowData interface{} `json:"row"`
}
// DeleteRow 删除行
func (api SeaTableApi) DeleteRow(ctx *BaseContext, tableName string, rowId string) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "row_id": rowId}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("DELETE", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// AddRows 添加多行数据
func (api SeaTableApi) AddRows(ctx *BaseContext, tableName string, data ...interface{}) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/batch-append-rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "rows": data}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// UpdateRows 更新多行数据
func (api SeaTableApi) UpdateRows(ctx *BaseContext, tableName string, data ...UpdateRowReq) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/batch-update-rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "updates": data}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("PUT", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// DeleteRows 删除多行数据
func (api SeaTableApi) DeleteRows(ctx *BaseContext, tableName string, rowIds ...string) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/batch-delete-rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "row_ids": rowIds}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("DELETE", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// LockRows 锁定多行数据
func (api SeaTableApi) LockRows(ctx *BaseContext, tableName string, rowIds ...string) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/lock-rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "row_ids": rowIds}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("PUT", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}
// UnLockRows 解锁多行数据
func (api SeaTableApi) UnLockRows(ctx *BaseContext, tableName string, rowIds ...string) SeaTableAction[map[string]interface{}] {
url := api.assignUrl(ctx.DtableServer, "/api/v1/dtables/%s/unlock-rows/", ctx.DtableUuid)
body := map[string]interface{}{"table_name": tableName, "row_ids": rowIds}
payload := strings.NewReader(util.ParseToJsonString(body))
req, _ := http.NewRequest("PUT", url, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", api.tokenHeader(ctx.AccessToken))
return SeaTableAction[map[string]interface{}]{Request: req}
}