-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
199 lines (169 loc) · 5.48 KB
/
site.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
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type formPostData struct {
ID string `form:"contactID"sql:"id" json:"id"` //I don't think conactID is getting any values
FirstName string `form:"firstName"sql:"first_name" json:"first_name"`
LastName string `form:"lastName"sql:"last_name" json:"last_name"`
Phone int `form:"phone"sql:"phone" json:"phone"`
OfficePhone int `form:"officePhone"sql:"office_phone" json:"office_phone"`
City string `form:"city"sql:"city" json:"city"`
State string `form:"state"sql:"state" json:"state"`
Zip string `form:"zip"sql:"zip" json:"zip"`
}
func NewFormPostData() formPostData {
return formPostData{}
}
type ContactInfo struct {
ID string `sql:"id" json:"id"`
FirstName string `sql:"first_name" json:"first_name"`
LastName string `sql:"last_name" json:"last_name"`
Phone int `sql:"phone" json:"phone"`
OfficePhone int `sql:"office_phone" json:"office_phone"`
City string `sql:"city" json:"city"`
State string `sql:"state" json:"state"`
Zip string `sql:"zip" json:"zip"`
Enabled bool `sql:"enabled" json:"enabled"`
}
func NewContact() ContactInfo {
contact := ContactInfo{}
return contact
}
func (ac *appContext) ShowIndex(c *gin.Context) {
ac.Log.Msg(1, "in context show index")
query :=
` select
id, first_name , last_name, phone, office_phone, city, state, zip, enabled
from contacts
where
enabled
order by 3,2`
rows, err := ac.DB.Query(query)
if check := ac.DBErrorCheck(err, query, c); check == false {
ac.Log.Msg(1, "db error")
return
}
defer rows.Close()
var contacts []ContactInfo
ac.Log.Msg(1, "before assignment")
for rows.Next() {
contact := NewContact()
err := rows.Scan(&contact.ID, &contact.FirstName, &contact.LastName, &contact.Phone, &contact.OfficePhone,
&contact.City, &contact.State, &contact.Zip, &contact.Enabled)
if err != nil {
ac.Log.Msg(3, fmt.Sprintf("Error scanning row: %s", err.Error()))
}
ac.Log.Msg(1, fmt.Sprintf("%#v", contact))
contacts = append(contacts, contact)
}
c.HTML(http.StatusOK, "main/index", gin.H{
"contacts": contacts,
})
}
func (ac *appContext) uploadContact(c *gin.Context) {
var form formPostData
if err := c.Bind(&form); err != nil {
fmt.Println("bind error" + err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"data": "ok"})
fmt.Printf("%+v", form)
query := "insert into contacts (first_name, last_name, phone, office_phone, " +
"city, state, zip) " +
"values ($1, $2, $3, $4, $5, $6, $7)"
_, err := ac.DB.Exec(query, &form.FirstName, &form.LastName, &form.Phone,
&form.OfficePhone, &form.City, &form.State, &form.Zip)
ac.DBErrorCheck(err, query, c)
}
func (ac *appContext) editContact(c *gin.Context) {
form := NewFormPostData()
if err := c.Bind(&form); err != nil {
ac.Log.Msg(3, fmt.Sprintf("bind error: %s", err.Error()))
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
query := `
select
id, first_name, last_name, phone, office_phone, city,
state, zip
from contacts
where
id = $1
`
row := ac.DB.QueryRow(query, &form.ID)
err := row.Scan(&form.ID, &form.FirstName, &form.LastName, &form.Phone, &form.OfficePhone, &form.City, &form.State, &form.Zip)
if check := ac.DBErrorCheck(err, query, c); check == false {
ac.AbortMsg(http.StatusInternalServerError, err, c)
}
c.JSON(http.StatusOK, gin.H{
"ID": form.ID,
"FirstName": form.FirstName,
"LastName": form.LastName,
"Phone": form.Phone,
"OfficePhone": form.OfficePhone,
"City": form.City,
"State": form.State,
"Zip": form.Zip,
})
}
func (ac *appContext) saveContact(c *gin.Context) {
form := NewFormPostData()
if err := c.Bind(&form); err != nil {
ac.Log.Msg(3, fmt.Sprintf("bind error: %s", err.Error()))
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if form.ID == "" {
query := `insert into contacts (first_name, last_name, phone, office_phone, city, state, zip)
values ('$1','$2','$3','$4','$5','$6','$7') returning id; `
row := ac.DB.QueryRow(query, &form.FirstName, &form.LastName, &form.Phone, &form.OfficePhone, &form.City, &form.State, &form.Zip)
err := row.Scan(&form.ID)
if check := ac.DBErrorCheck(err, query, c); check == false {
ac.AbortMsg(http.StatusInternalServerError, err, c)
}
} else {
query := `
update user_contacts set
first_name = $1,
last_name = $2,
phone = $3,
office_phone = $4,
city= $5,
state= $6,
zip= $7
where
id = $8
`
res, err := ac.DB.Exec(query, &form.FirstName, &form.LastName, &form.Phone, &form.OfficePhone, &form.City, &form.State, &form.Zip)
if check := ac.DBErrorCheck(err, query, c); check == false {
ac.AbortMsg(http.StatusInternalServerError, err, c)
return
}
ra, _ := res.RowsAffected()
ac.Log.Msg(0, fmt.Sprintf("rows affected [ %d ]", ra))
}
}
func (ac *appContext) deleteContact(c *gin.Context) {
form := NewFormPostData()
if err := c.Bind(&form); err != nil {
ac.Log.Msg(3, fmt.Sprintf("bind error: %s", err.Error()))
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
query := `
delete from contacts
where
id = $1
`
res, err := ac.DB.Exec(query, &form.ID)
if check := ac.DBErrorCheck(err, query, c); check == false {
ac.AbortMsg(http.StatusInternalServerError, err, c)
return
}
ra, err := res.RowsAffected()
ac.Log.Msg(0, fmt.Sprintf("rows affected [ %d ]", ra))
}