-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers-regiones.go
More file actions
173 lines (163 loc) · 4.41 KB
/
Copy pathhandlers-regiones.go
File metadata and controls
173 lines (163 loc) · 4.41 KB
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
package main
import (
"database/sql"
"log"
"net/http"
"strconv"
)
func handlerGetDistritosByCanton(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
//consigue el valor de la variable en el url
//si no se encuentra retorna badrequest
canton, err := strconv.Atoi(r.URL.Query().Get("Canton"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
//utiliza el querryWrapper para devolver los datos
distritos := querryWrapper[Distrito](
"SELECT * FROM Distritos WHERE Cantonid = ?",
canton,
)
//utiliza el jsonWrapper para enviar los datos
jsonWrapper(distritos, w)
}
func handlerGetCantonesByProvincia(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
provincia, err := strconv.Atoi(r.URL.Query().Get("Provincia"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
cantones := querryWrapper[Canton](
"SELECT * FROM Cantones WHERE Provinciaid = ?",
provincia,
)
jsonWrapper(cantones, w)
}
func handlerGetProvincias(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
provincias := querryWrapper[Provincia]("SELECT * FROM Provincias")
jsonWrapper(provincias, w)
}
func handlerGetDistritoById(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
distritoId, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var d Distrito
row := db.QueryRow("SELECT * FROM Distritos WHERE DistritoId = ?", distritoId)
err = row.Scan(&d.Id, &d.NombreDistrito, &d.IdCanton)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no existe un distrito con este id"))
return
} else {
log.Fatal(err)
return
}
}
jsonWrapper(d, w)
}
func handlerGetCantonById(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
cantonId, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var c Canton
row := db.QueryRow("SELECT * FROM Cantones WHERE CantonId = ?", cantonId)
err = row.Scan(&c.Id, &c.NombreCanton, &c.IdProvincia)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no existe un canton con este id"))
return
} else {
log.Fatal(err)
return
}
}
jsonWrapper(c, w)
}
func handlerGetProvinciaById(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
provinciaId, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var p Provincia
row := db.QueryRow("SELECT * FROM Provincias WHERE ProvinciaId = ?", provinciaId)
err = row.Scan(&p.Id, &p.NombreProvicia)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no existe una provincia con este id"))
return
} else {
log.Fatal(err)
return
}
}
jsonWrapper(p, w)
}
func handlerGetCalleById(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
calleId, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
calle := struct {
NombreCalle string `json:"nombreCalle"`
CalleId int `json:"calleId"`
DistriId int `json:"distritoId"`
}{}
row := db.QueryRow("SELECT * FROM Calles WHERE CalleId = ?", calleId)
err = row.Scan(&calle.CalleId, &calle.NombreCalle, &calle.DistriId)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no existe una provincia con este id"))
return
} else {
log.Fatal(err)
return
}
}
jsonWrapper(calle, w)
}
func asociarHandlersRegiones() {
http.HandleFunc("/getDistritosByCanton", handlerGetDistritosByCanton)
http.HandleFunc("/getCantonesByProvincia", handlerGetCantonesByProvincia)
http.HandleFunc("/getProvincias", handlerGetProvincias)
http.HandleFunc("/getDistritoById", handlerGetDistritoById)
http.HandleFunc("/getCantonById", handlerGetCantonById)
http.HandleFunc("/getProvinciaById", handlerGetProvinciaById)
http.HandleFunc("/getCalleById", handlerGetCalleById)
}