Skip to content
Permalink
Browse files

09-Pagination add create post

  • Loading branch information...
bonfy committed Oct 8, 2018
1 parent b1502cf commit f1062528991817ed299dd4536bbfa2defa600c4f
Showing with 67 additions and 7 deletions.
  1. +2 −0 controller/g.go
  2. +21 −2 controller/home.go
  3. +16 −0 controller/utils.go
  4. +17 −1 templates/content/index.html
  5. +11 −4 vm/index.go
@@ -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
@@ -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) {
@@ -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])
}
@@ -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}}
@@ -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.
You can’t perform that action at this time.