-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
202 lines (139 loc) · 4.52 KB
/
Copy pathhandler.go
File metadata and controls
202 lines (139 loc) · 4.52 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
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
package handlers
import (
"MyWeb/models"
"MyWeb/pkg/configs"
"MyWeb/pkg/dbdriver"
"MyWeb/pkg/forms"
"MyWeb/pkg/renders"
"MyWeb/pkg/repository"
"MyWeb/pkg/repository/dbrepo"
"log"
"net/http"
)
type Repository struct {
App *configs.AppConfig
DataBase repository.DataBaseRepo
}
var Repo *Repository // when we call Repo we can control the Repository esey
func NewRepository(appConfig *configs.AppConfig, db *dbdriver.DataBase) *Repository {
return &Repository{
App: appConfig,
DataBase: dbrepo.NewPostgresRepo(db.SQL, appConfig),
}
}
func NewHandlers(r *Repository) {
Repo = r
}
func (m *Repository) HomeHandler(w http.ResponseWriter, r *http.Request) {
var articlesList models.ArticleList
articlesList, err := m.DataBase.Get3AnArticle()
if err != nil {
log.Println(err)
return
}
m.App.Session.Put(r.Context(), "userid", "awab")
data := make(map[string]interface{})
data["articleList"] = articlesList
renders.RenderTemplate(w, r, "home.page.tmpl", &models.DataPage{
Data: data,
})
}
func (m *Repository) AboutHandler(w http.ResponseWriter, r *http.Request) {
strMapData := make(map[string]string)
renders.RenderTemplate(w, r, "about.page.tmpl", &models.DataPage{StrMap: strMapData})
}
func (m *Repository) MakePostHandler(w http.ResponseWriter, r *http.Request) {
if !m.App.Session.Exists(r.Context(), "user_id") {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
}
var emptyArticle models.Article
data := make(map[string]interface{})
data["article"] = emptyArticle
renders.RenderTemplate(w, r, "make-post.page.tmpl",
&models.DataPage{
Data: data,
Form: forms.NewForm(nil),
})
}
// Handler for posting articles useing post
func (m *Repository) PostMakePostHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println("( ! ) Error with Parse Form : ", err)
return
}
uID := (m.App.Session.Get(r.Context(), "user_id")).(int)
article := models.Post{
Title: r.Form.Get("blog_title"),
Content: r.Form.Get("blog_article"),
UserId: int(uID),
}
form := forms.NewForm(r.PostForm)
form.HasRequired("blog_title", "blog_article")
form.MinLength("blog_title", 5, r)
form.MinLength("blog_article", 5, r)
if !form.Valid() { // if the Valid Not True
data := make(map[string]interface{})
data["article"] = article
renders.RenderTemplate(w, r, "make-post.page.tmpl", &models.DataPage{Form: form, Data: data})
return
}
// write to the db
err = m.DataBase.InsertPost(article)
if err != nil {
log.Fatal(err)
}
m.App.Session.Put(r.Context(), "article", article)
http.Redirect(w, r, "/article-received", http.StatusSeeOther)
}
func (m *Repository) ArticleReceived(w http.ResponseWriter, r *http.Request) {
article, ok := m.App.Session.Get(r.Context(), "article").(models.Article)
if !ok {
m.App.Session.Put(r.Context(), "error", "Can't get data from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
data := make(map[string]interface{})
data["article"] = article
renders.RenderTemplate(w, r, "article-received.page.tmpl", &models.DataPage{
Data: data,
})
}
func (m *Repository) LoginHandler(w http.ResponseWriter, r *http.Request) {
strMapData := make(map[string]string)
renders.RenderTemplate(w, r, "login.page.tmpl", &models.DataPage{StrMap: strMapData})
}
func (m *Repository) PostLoginHandler(w http.ResponseWriter, r *http.Request) {
_ = m.App.Session.RenewToken(r.Context())
err := r.ParseForm()
if err != nil {
log.Fatal()
}
email := r.Form.Get("email")
password := r.Form.Get("password")
form := forms.NewForm(r.PostForm)
form.HasRequired("email", "password")
form.IsEmail("email")
if !form.Valid() {
renders.RenderTemplate(w, r, "login.page.tmpl", &models.DataPage{Form: form})
return
}
id, _, err := m.DataBase.AuthenticateUser(email, password)
if err != nil {
m.App.Session.Put(r.Context(), "error", "invalid email or password")
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
m.App.Session.Put(r.Context(), "user_id", id)
m.App.Session.Put(r.Context(), "flash", "Valid Login")
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (m *Repository) PageHandler(w http.ResponseWriter, r *http.Request) {
strMapData := make(map[string]string)
renders.RenderTemplate(w, r, "page.page.tmpl", &models.DataPage{StrMap: strMapData})
}
func (m *Repository) LogoutHandler(w http.ResponseWriter, r *http.Request) {
_ = m.App.Session.Destroy(r.Context())
_ = m.App.Session.RenewToken(r.Context())
http.Redirect(w, r, "/login", http.StatusSeeOther)
}