-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
100 lines (82 loc) · 2.17 KB
/
events.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
package goautowp
import (
"context"
"github.com/autowp/goautowp/schema"
"github.com/autowp/goautowp/util"
"github.com/doug-martin/goqu/v9"
)
const EventsDefaultLanguage = "en"
type Event struct {
UserID int64
Message string
Users []int64
Pictures []int64
Items []int64
}
type Events struct {
db *goqu.Database
}
func NewEvents(db *goqu.Database) *Events {
return &Events{
db: db,
}
}
func (s *Events) Add(ctx context.Context, event Event) error {
res, err := s.db.Insert(schema.LogEventsTable).
Rows(goqu.Record{
schema.LogEventsTableDescriptionColName: event.Message,
schema.LogEventsTableUserIDColName: event.UserID,
schema.LogEventsTableAddDatetimeColName: goqu.Func("NOW"),
}).
Executor().ExecContext(ctx)
if err != nil {
return err
}
rowID, err := res.LastInsertId()
if err != nil {
return err
}
if len(event.Users) > 0 {
event.Users = util.RemoveDuplicate(event.Users)
rows := make([]interface{}, len(event.Users))
for idx, id := range event.Users {
rows[idx] = goqu.Record{
schema.LogEventsUserTableLogEventIDColName: rowID,
schema.LogEventsUserTableUserIDColName: id,
}
}
_, err = s.db.Insert(schema.LogEventsUserTable).Rows(rows...).Executor().ExecContext(ctx)
if err != nil {
return err
}
}
if len(event.Pictures) > 0 {
event.Pictures = util.RemoveDuplicate(event.Pictures)
rows := make([]interface{}, len(event.Pictures))
for idx, id := range event.Pictures {
rows[idx] = goqu.Record{
schema.LogEventsPicturesTableLogEventIDColName: rowID,
schema.LogEventsPicturesTablePictureIDColName: id,
}
}
_, err = s.db.Insert(schema.LogEventsPicturesTable).Rows(rows...).Executor().ExecContext(ctx)
if err != nil {
return err
}
}
if len(event.Items) > 0 {
event.Items = util.RemoveDuplicate(event.Items)
rows := make([]interface{}, len(event.Items))
for idx, id := range event.Items {
rows[idx] = goqu.Record{
schema.LogEventsItemTableLogEventIDColName: rowID,
schema.LogEventsItemTableItemIDColName: id,
}
}
_, err = s.db.Insert(schema.LogEventsItemTable).Rows(rows...).Executor().ExecContext(ctx)
if err != nil {
return err
}
}
return nil
}