/
material_event_query.go
72 lines (57 loc) · 1.62 KB
/
material_event_query.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
package mysql
import (
"database/sql"
"encoding/json"
"time"
"github.com/Tanibox/tania-core/src/assets/decoder"
"github.com/Tanibox/tania-core/src/assets/query"
"github.com/Tanibox/tania-core/src/assets/storage"
"github.com/gofrs/uuid"
)
type MaterialEventQueryMysql struct {
DB *sql.DB
}
func NewMaterialEventQueryMysql(db *sql.DB) query.MaterialEvent {
return &MaterialEventQueryMysql{DB: db}
}
func (f *MaterialEventQueryMysql) FindAllByID(uid uuid.UUID) <-chan query.Result {
result := make(chan query.Result)
go func() {
events := []storage.MaterialEvent{}
rows, err := f.DB.Query("SELECT * FROM MATERIAL_EVENT WHERE MATERIAL_UID = ? ORDER BY VERSION ASC", uid.Bytes())
if err != nil {
result <- query.Result{Error: err}
}
rowsData := struct {
ID int
MaterialUID []byte
Version int
CreatedDate time.Time
Event []byte
}{}
for rows.Next() {
err := rows.Scan(&rowsData.ID, &rowsData.MaterialUID, &rowsData.Version, &rowsData.CreatedDate, &rowsData.Event)
if err != nil {
result <- query.Result{Error: err}
}
wrapper := decoder.MaterialEventWrapper{}
err = json.Unmarshal(rowsData.Event, &wrapper)
if err != nil {
result <- query.Result{Error: err}
}
materialUID, err := uuid.FromBytes(rowsData.MaterialUID)
if err != nil {
result <- query.Result{Error: err}
}
events = append(events, storage.MaterialEvent{
MaterialUID: materialUID,
Version: rowsData.Version,
CreatedDate: rowsData.CreatedDate,
Event: wrapper.EventData,
})
}
result <- query.Result{Result: events}
close(result)
}()
return result
}