/
farm_read_query.go
146 lines (121 loc) · 3.09 KB
/
farm_read_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
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
package sqlite
import (
"database/sql"
"errors"
"time"
"github.com/Tanibox/tania-core/src/assets/query"
"github.com/Tanibox/tania-core/src/assets/storage"
"github.com/gofrs/uuid"
)
type FarmReadQuerySqlite struct {
DB *sql.DB
}
func NewFarmReadQuerySqlite(db *sql.DB) query.FarmRead {
return FarmReadQuerySqlite{DB: db}
}
type farmReadResult struct {
UID string
Name string
Latitude string
Longitude string
Type string
Country string
City string
IsActive int
CreatedDate string
}
func (s FarmReadQuerySqlite) FindByID(uid uuid.UUID) <-chan query.Result {
result := make(chan query.Result)
go func() {
farmRead := storage.FarmRead{}
rowsData := farmReadResult{}
err := s.DB.QueryRow("SELECT * FROM FARM_READ WHERE UID = ?", uid).Scan(
&rowsData.UID,
&rowsData.Name,
&rowsData.Latitude,
&rowsData.Longitude,
&rowsData.Type,
&rowsData.Country,
&rowsData.City,
&rowsData.IsActive,
&rowsData.CreatedDate,
)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
result <- query.Result{Error: err}
}
if errors.Is(err, sql.ErrNoRows) {
result <- query.Result{Result: farmRead}
}
farmUID, err := uuid.FromString(rowsData.UID)
if err != nil {
result <- query.Result{Error: err}
}
createdDate, err := time.Parse(time.RFC3339, rowsData.CreatedDate)
if err != nil {
result <- query.Result{Error: err}
}
farmRead = storage.FarmRead{
UID: farmUID,
Name: rowsData.Name,
Latitude: rowsData.Latitude,
Longitude: rowsData.Longitude,
Type: rowsData.Type,
Country: rowsData.Country,
City: rowsData.City,
IsActive: rowsData.IsActive != 0,
CreatedDate: createdDate,
}
result <- query.Result{Result: farmRead}
close(result)
}()
return result
}
func (s FarmReadQuerySqlite) FindAll() <-chan query.Result {
result := make(chan query.Result)
go func() {
farmReads := []storage.FarmRead{}
rowsData := farmReadResult{}
rows, err := s.DB.Query("SELECT * FROM FARM_READ ORDER BY CREATED_DATE ASC")
if err != nil {
result <- query.Result{Error: err}
}
for rows.Next() {
err = rows.Scan(
&rowsData.UID,
&rowsData.Name,
&rowsData.Latitude,
&rowsData.Longitude,
&rowsData.Type,
&rowsData.Country,
&rowsData.City,
&rowsData.IsActive,
&rowsData.CreatedDate,
)
if err != nil {
result <- query.Result{Error: err}
}
farmUID, err := uuid.FromString(rowsData.UID)
if err != nil {
result <- query.Result{Error: err}
}
createdDate, err := time.Parse(time.RFC3339, rowsData.CreatedDate)
if err != nil {
result <- query.Result{Error: err}
}
farmReads = append(farmReads, storage.FarmRead{
UID: farmUID,
Name: rowsData.Name,
Latitude: rowsData.Latitude,
Longitude: rowsData.Longitude,
Type: rowsData.Type,
Country: rowsData.Country,
City: rowsData.City,
IsActive: rowsData.IsActive != 0,
CreatedDate: createdDate,
})
}
result <- query.Result{Result: farmReads}
close(result)
}()
return result
}