-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
301 lines (248 loc) · 6.65 KB
/
handlers.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type TemplateData struct {
Url string
UserSession string
Errors []string
Blogs []Blog
Blog
}
type User struct {
Email string `json:"email"`
Password string `json:"password"`
}
type Resp struct {
Success bool `json:"success"`
}
func setUserSession(w http.ResponseWriter, email string) {
session := http.Cookie{
Name: "session",
Value: email,
Path: "/",
Expires: time.Now().Add(12 * time.Minute),
Secure: true,
HttpOnly: true,
}
http.SetCookie(w, &session)
}
func getUserSession(r *http.Request) string {
session, err := r.Cookie("session")
if err != nil {
return ""
}
email := session.Value
return email
}
func (app *Application) HomeHandler(w http.ResponseWriter, r *http.Request) {
blogs, err := GetBlogs()
if err != nil {
log.Println(err)
}
app.RenderTemplate(w, r, "index_page.gohtml", &TemplateData{
Blogs: blogs,
})
}
func (app *Application) HomeBlogViewHandler(w http.ResponseWriter, r *http.Request) {
blogId := r.URL.Query().Get("id")
id, err := strconv.Atoi(blogId)
if err != nil {
log.Println(err)
}
blog, err := GetBlogById(id)
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
app.RenderTemplate(w, r, "blog_view_page.gohtml", &TemplateData{
Blog: *blog,
})
}
func (app *Application) LoginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
app.RenderTemplate(w, r, "login_page.gohtml", nil)
} else if r.Method == http.MethodPost {
var user User
var resp Resp
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
log.Println(err)
return
}
// Busca usuario por el email en la BDD.
dataUserDB, err := GetUserByEmail(user.Email)
if err != nil {
log.Printf("Error al extraer el usuario de la base de datos: %s", err)
}
if user.Email == dataUserDB.Email && user.Password == dataUserDB.Password {
resp.Success = true
setUserSession(w, user.Email)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Println(err)
return
}
} else {
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Println(err)
return
}
}
}
}
func (app *Application) RegisterHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
app.RenderTemplate(w, r, "register_page.gohtml", nil)
} else if r.Method == http.MethodPost {
var user User
var resp Resp
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
log.Println(err)
return
}
resp.Success = true
_, err := db.Exec("INSERT INTO data_users (email, password) VALUES ($1, $2)", user.Email, user.Password)
if err != nil {
log.Println("No fue posible insertar los datos")
resp.Success = false
json.NewEncoder(w).Encode(resp)
return
}
resp.Success = true
setUserSession(w, user.Email)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Println(err)
return
}
// setUserSession(w, user.Email) Esto no es posible hacerlo debido a que en el encoder ya se envio el encabezado
// y no se puede enviar otro encabezado despues.
}
}
func (app *Application) BlogHandler(w http.ResponseWriter, r *http.Request) {
blogs, err := GetBlogs()
if err != nil {
log.Println(err)
return
}
errorMessage := r.URL.Query().Get("error") // si no encuentra nada retorna vacio, se podria tratar sie s vacio para mostrarle que el usuario fue elmininado correctamente o algo asi
app.RenderTemplate(w, r, "blog_page.gohtml", &TemplateData{
Blogs: blogs,
Errors: []string{errorMessage},
})
}
func slugify(value string) string {
value = strings.ToLower(value)
reg := regexp.MustCompile("[^a-z0-9]+")
value = reg.ReplaceAllString(value, "-")
value = strings.Trim(value, "-")
return value
}
func (app *Application) NewBlogHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
app.RenderTemplate(w, r, "blog_new_page.gohtml", nil)
} else if r.Method == http.MethodPost {
title := r.FormValue("title")
content := r.FormValue("content")
errors := make([]string, 0)
if len(strings.Trim(title, " ")) == 0 {
errors = append(errors, "Titulo requerido!")
}
if len(strings.Trim(content, " ")) == 0 {
errors = append(errors, "Contenido requerido!")
}
email := getUserSession(r)
dataUserDB, err := GetUserByEmail(email)
if err != nil {
errors = append(errors, "No estas logueado")
}
blog := Blog{
Title: title,
Content: template.HTML(content),
Slug: slugify(title),
Author: dataUserDB.Id,
}
if err = CreateBlog(blog); err != nil {
errors = append(errors, err.Error())
}
log.Println(blog)
if len(errors) > 0 {
app.RenderTemplate(w, r, "blog_new_page.gohtml", &TemplateData{
Errors: errors,
})
return
}
http.Redirect(w, r, "/blog", http.StatusSeeOther)
}
}
func (app *Application) EditBlogHandler(w http.ResponseWriter, r *http.Request) {
blogId := r.URL.Query().Get("id")
id, err := strconv.Atoi(blogId)
if err != nil {
return
}
if r.Method == http.MethodGet {
blog, err := GetBlogById(id)
if err != nil {
log.Println("error al extraer blog")
return
}
app.RenderTemplate(w, r, "blog_edit.gohtml", &TemplateData{
Blog: *blog,
})
} else if r.Method == http.MethodPost {
title := r.FormValue("title")
content := r.FormValue("content")
// id := r.FormValue("id") tambien funciona para extraer de el id del atributo value de un input hidden. {{.Blog.Id}} que este en el form.
// Actualizar la base de datos
blog := Blog{
Id: id,
Title: title,
Content: template.HTML(content),
Slug: slugify(title),
}
if err := UpdateBlog(blog); err != nil {
log.Println("error ala actualizar blog")
return
}
http.Redirect(w, r, "/blog", http.StatusSeeOther)
return
}
}
func (app *Application) DeleteBlogHandler(w http.ResponseWriter, r *http.Request) {
blogId := r.URL.Query().Get("id")
id, err := strconv.Atoi(blogId)
if err != nil {
return
}
rows, err := DeleteBlog(id)
if err != nil {
log.Printf("error al borrar el blog. %s", err)
return
}
if rows < 1 {
http.Redirect(w, r, fmt.Sprintf("/blog?error=%s", "id no valido"), http.StatusSeeOther)
return
}
http.Redirect(w, r, "/blog", http.StatusSeeOther)
}
func (app *Application) AboutHandler(w http.ResponseWriter, r *http.Request) {
app.RenderTemplate(w, r, "about_page.gohtml", nil)
}
func (app *Application) LogoutHandler(w http.ResponseWriter, r *http.Request) {
session := http.Cookie{
Name: "session",
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(w, &session)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}