Skip to content

Commit

Permalink
09-Pagination add create post
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Oct 8, 2018
1 parent b1502cf commit f106252
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
2 changes: 2 additions & 0 deletions controller/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ var (
homeController home
templates map[string]*template.Template
sessionName string
flashName string
store *sessions.CookieStore
)

func init() {
templates = PopulateTemplates()
store = sessions.NewCookieStore([]byte("something-very-secret"))
sessionName = "go-mega"
flashName = "go-flash"
}

// Startup func
Expand Down
23 changes: 21 additions & 2 deletions controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,27 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
tpName := "index.html"
vop := vm.IndexViewModelOp{}
username, _ := getSessionUser(r)
v := vop.GetVM(username)
templates[tpName].Execute(w, &v)
if r.Method == http.MethodGet {
flash := getFlash(w, r)
v := vop.GetVM(username, flash)
templates[tpName].Execute(w, &v)
}
if r.Method == http.MethodPost {
r.ParseForm()
body := r.Form.Get("body")
errMessage := checkLen("Post", body, 1, 180)
if errMessage != "" {
setFlash(w, r, errMessage)
} else {
err := vm.CreatePost(username, body)
if err != nil {
log.Println("add Post error:", err)
w.Write([]byte("Error insert Post in database"))
return
}
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
16 changes: 16 additions & 0 deletions controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,19 @@ func checkRegister(username, email, pwd1, pwd2 string) []string {
func addUser(username, password, email string) error {
return vm.AddUser(username, password, email)
}

func setFlash(w http.ResponseWriter, r *http.Request, message string) {
session, _ := store.Get(r, sessionName)
session.AddFlash(message, flashName)
session.Save(r, w)
}

func getFlash(w http.ResponseWriter, r *http.Request) string {
session, _ := store.Get(r, sessionName)
fm := session.Flashes(flashName)
if fm == nil {
return ""
}
session.Save(r, w)
return fmt.Sprintf("%v", fm[0])
}
18 changes: 17 additions & 1 deletion templates/content/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
{{define "content"}}
<h1>Hello, {{.CurrentUser}}!</h1>

<form action="/" method="post">

<p><textarea name="body" rows="3" cols="80" value="" placeholder="say something..."></textarea></p>
<p><input type="submit" name="submit" value="Post"></p>

{{ if .Flash }}
<span style="color: red;">[{{.Flash}}]</span>
{{ end }}
</form>


{{range .Posts}}
<div><p>{{ .User.Username }} says: <b>{{ .Body }}</b></p></div>
<table>
<tr valign="top">
<td><img src="{{.User.Avatar}}&s=36"></td>
<td>{{ .User.Username }} says:<br>{{ .Body }}</td>
</tr>
</table>
{{end}}
{{end}}
15 changes: 11 additions & 4 deletions vm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import "github.com/bonfy/go-mega-code/model"
type IndexViewModel struct {
BaseViewModel
Posts []model.Post
Flash string
}

// IndexViewModelOp struct
type IndexViewModelOp struct{}

// GetVM func
func (IndexViewModelOp) GetVM(username string) IndexViewModel {
u1, _ := model.GetUserByUsername(username)
posts, _ := model.GetPostsByUserID(u1.ID)
v := IndexViewModel{BaseViewModel{Title: "Homepage"}, *posts}
func (IndexViewModelOp) GetVM(username string, flash string) IndexViewModel {
u, _ := model.GetUserByUsername(username)
posts, _ := u.FollowingPosts()
v := IndexViewModel{BaseViewModel{Title: "Homepage"}, *posts, flash}
v.SetCurrentUser(username)
return v
}

// CreatePost func
func CreatePost(username, post string) error {
u, _ := model.GetUserByUsername(username)
return u.CreatePost(post)
}

0 comments on commit f106252

Please sign in to comment.