/
area_read_query.go
78 lines (63 loc) · 1.66 KB
/
area_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
package sqlite
import (
"database/sql"
"errors"
"github.com/Tanibox/tania-core/src/growth/query"
"github.com/gofrs/uuid"
)
type AreaReadQuerySqlite struct {
DB *sql.DB
}
func NewAreaReadQuerySqlite(db *sql.DB) query.AreaReadQuery {
return AreaReadQuerySqlite{DB: db}
}
type areaReadResult struct {
UID string
Name string
Size float32
SizeUnit string
Type string
Location string
FarmUID string
}
func (s AreaReadQuerySqlite) FindByID(uid uuid.UUID) <-chan query.Result {
result := make(chan query.Result)
go func() {
areaQueryResult := query.CropAreaQueryResult{}
rowsData := areaReadResult{}
err := s.DB.QueryRow(`SELECT UID, NAME, SIZE, SIZE_UNIT, TYPE, LOCATION, FARM_UID
FROM AREA_READ WHERE UID = ?`, uid).Scan(
&rowsData.UID,
&rowsData.Name,
&rowsData.Size,
&rowsData.SizeUnit,
&rowsData.Type,
&rowsData.Location,
&rowsData.FarmUID,
)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
result <- query.Result{Error: err}
}
if errors.Is(err, sql.ErrNoRows) {
result <- query.Result{Result: areaQueryResult}
}
areaUID, err := uuid.FromString(rowsData.UID)
if err != nil {
result <- query.Result{Error: err}
}
farmUID, err := uuid.FromString(rowsData.FarmUID)
if err != nil {
result <- query.Result{Error: err}
}
areaQueryResult.UID = areaUID
areaQueryResult.Name = rowsData.Name
areaQueryResult.Size.Value = rowsData.Size
areaQueryResult.Size.Symbol = rowsData.SizeUnit
areaQueryResult.Type = rowsData.Type
areaQueryResult.Location = rowsData.Location
areaQueryResult.FarmUID = farmUID
result <- query.Result{Result: areaQueryResult}
close(result)
}()
return result
}