-
Notifications
You must be signed in to change notification settings - Fork 6
/
search.go
54 lines (47 loc) · 1.07 KB
/
search.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
package google
import (
"github.com/bionic-dev/bionic/imports/google"
"github.com/bionic-dev/bionic/types"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strings"
)
const searchQueryPrefix = "Searched for "
type Search struct {
gorm.Model
Text string
Time types.DateTime
ActionID int `gorm:"unique"`
Action google.Action
}
func (Search) TableName() string {
return tablePrefix + "searches"
}
func (Search) Update(db *gorm.DB) error {
var results []google.Action
query := db.
Model(&google.Action{}).
Where("Title like ? AND Header = 'Search'", "%"+searchQueryPrefix+"%")
query.FindInBatches(&results, 100, func(tx *gorm.DB, batch int) error {
var items []Search
for _, action := range results {
items = append(items, Search{
Text: strings.TrimPrefix(action.Title, searchQueryPrefix),
Time: action.Time,
Action: action,
})
}
err := db.
Clauses(clause.OnConflict{
DoNothing: true,
}).
Create(items).
Error
return err
})
return nil
}
func (Search) Migrate(db *gorm.DB) error {
err := db.AutoMigrate(&Search{})
return err
}