-
Notifications
You must be signed in to change notification settings - Fork 5
/
fair.go
187 lines (154 loc) · 4.84 KB
/
fair.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package fair
import (
"database/sql"
"github.com/jeffersonto/feira-api/internal/entity"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" // import driver for sqlite connection
)
type FairRepository interface {
GetByID(fairID int64) (entity.Fair, error)
DeleteByID(fairID int64) error
Save(fair entity.Fair) (int64, error)
Update(id int64, fair entity.Fair) error
GetByQueryID(filters entity.Filter) ([]entity.Fair, error)
AlreadyAnID(userID int64) (bool, error)
}
type Repository struct {
DB *sqlx.DB
}
func NewRepository(db *sqlx.DB) (*Repository, error) {
return &Repository{
DB: db,
}, nil
}
func (repo *Repository) GetByID(fairID int64) (entity.Fair, error) {
var (
fair = entity.Fair{}
)
err := repo.DB.Get(&fair, "SELECT id, longitude, latitude, setor_censitario, area_ponderacao,"+
" codigo_ibge, distrito, codigo_subprefeitura, subprefeitura, regiao5, regiao8, nome_feira,"+
" registro, logradouro, numero, bairro, referencia "+
" FROM feiras_livres "+
" WHERE id = ? ", fairID)
if err != nil {
return fair, err
}
return fair, nil
}
func (repo *Repository) GetByQueryID(filters entity.Filter) ([]entity.Fair, error) {
parametersForQuery := make([]interface{}, 0)
query := "SELECT id, longitude, latitude, setor_censitario, area_ponderacao," +
" codigo_ibge, distrito, codigo_subprefeitura, subprefeitura, regiao5," +
" regiao8, nome_feira, registro, logradouro, numero, bairro, referencia " +
" FROM feiras_livres " +
" WHERE 1=1 "
if filters.Distrito != "" {
query += " AND UPPER(TRIM(distrito)) = UPPER(TRIM(?))"
parametersForQuery = append(parametersForQuery, filters.Distrito)
}
if filters.Regiao5 != "" {
query += " AND UPPER(TRIM(regiao5)) = UPPER(TRIM(?))"
parametersForQuery = append(parametersForQuery, filters.Regiao5)
}
if filters.NomeFeira != "" {
query += " AND UPPER(TRIM(nome_feira)) = UPPER(TRIM(?))"
parametersForQuery = append(parametersForQuery, filters.NomeFeira)
}
if filters.Bairro != "" {
query += " AND UPPER(TRIM(bairro)) = UPPER(TRIM(?))"
parametersForQuery = append(parametersForQuery, filters.Bairro)
}
fairs := make([]entity.Fair, 0)
var rows *sql.Rows
rows, err := repo.DB.Query(query, parametersForQuery...)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var data entity.Fair
err = rows.Scan(&data.ID, &data.Longitude, &data.Latitude, &data.SetorCensitario, &data.AreaPonderacao,
&data.CodigoIBGE, &data.Distrito, &data.CodigoSubPrefeitura, &data.SubPrefeitura, &data.Regiao5,
&data.Regiao8, &data.NomeFeira, &data.Registro, &data.Logradouro, &data.Numero, &data.Bairro, &data.Referencia)
if err != nil {
return nil, err
}
fairs = append(fairs, data)
}
if rows.Err() != nil {
return nil, rows.Err()
}
return fairs, nil
}
func (repo *Repository) DeleteByID(fairID int64) error {
_, err := repo.DB.Exec(
"DELETE FROM feiras_livres"+
" WHERE id = ? ", fairID)
if err != nil {
return err
}
return nil
}
func (repo *Repository) Save(fair entity.Fair) (int64, error) {
result, err := repo.DB.Exec(
"INSERT INTO feiras_livres (longitude, latitude, setor_censitario, area_ponderacao,"+
" codigo_ibge, distrito, codigo_subprefeitura, subprefeitura, regiao5, regiao8, nome_feira,"+
"registro, logradouro, numero, bairro, referencia) "+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
fair.Longitude, fair.Latitude, fair.SetorCensitario,
fair.AreaPonderacao, fair.CodigoIBGE, fair.Distrito,
fair.CodigoSubPrefeitura, fair.SubPrefeitura,
fair.Regiao5, fair.Regiao8, fair.NomeFeira,
fair.Registro, fair.Logradouro, fair.Numero,
fair.Bairro, fair.Referencia)
if err != nil {
return 0, err
}
id, _ := result.LastInsertId()
return id, nil
}
func (repo *Repository) Update(id int64, fair entity.Fair) error {
_, err := repo.DB.Exec(
"UPDATE feiras_livres SET"+
" longitude = ?,"+
" latitude = ?,"+
" setor_censitario = ?,"+
" area_ponderacao = ?,"+
" codigo_ibge = ?, "+
" distrito = ?,"+
" codigo_subprefeitura = ?, "+
" subprefeitura = ?,"+
" regiao5 = ?,"+
" regiao8 = ?,"+
" nome_feira = ?,"+
" registro = ?,"+
" logradouro = ?,"+
" numero = ?,"+
" bairro = ?,"+
" referencia = ?"+
" WHERE id = ? ",
fair.Longitude, fair.Latitude, fair.SetorCensitario,
fair.AreaPonderacao, fair.CodigoIBGE, fair.Distrito,
fair.CodigoSubPrefeitura, fair.SubPrefeitura,
fair.Regiao5, fair.Regiao8, fair.NomeFeira,
fair.Registro, fair.Logradouro, fair.Numero,
fair.Bairro, fair.Referencia, id)
if err != nil {
return err
}
return nil
}
func (repo *Repository) AlreadyAnID(userID int64) (bool, error) {
var idFetched int64
err := repo.DB.QueryRow("SELECT id "+
" FROM feiras_livres "+
" WHERE id = ?", userID).Scan(&idFetched)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, err
default:
return true, nil
}
}