Skip to content
Permalink
Browse files

09-Pagination add explore

  • Loading branch information...
bonfy committed Oct 8, 2018
1 parent fd3c825 commit 4609f2bd8bf293ccd2dc1871a03e7e8c91d44706
Showing with 70 additions and 0 deletions.
  1. +10 −0 controller/home.go
  2. +15 −0 model/post.go
  3. +1 −0 templates/_base.html
  4. +19 −0 templates/content/explore.html
  5. +25 −0 vm/explore.go
@@ -20,6 +20,7 @@ func (h home) registerRoutes() {
r.HandleFunc("/follow/{username}", middleAuth(followHandler))
r.HandleFunc("/unfollow/{username}", middleAuth(unFollowHandler))
r.HandleFunc("/profile_edit", middleAuth(profileEditHandler))
r.HandleFunc("/explore", middleAuth(exploreHandler))
r.HandleFunc("/", middleAuth(indexHandler))

http.Handle("/", r)
@@ -183,3 +184,12 @@ func unFollowHandler(w http.ResponseWriter, r *http.Request) {
}
http.Redirect(w, r, fmt.Sprintf("/user/%s", pUser), http.StatusSeeOther)
}

func exploreHandler(w http.ResponseWriter, r *http.Request) {
tpName := "explore.html"
vop := vm.ExploreViewModelOp{}
username, _ := getSessionUser(r)
page := getPage(r)
v := vop.GetVM(username, page, pageLimit)
templates[tpName].Execute(w, &v)
}
@@ -33,3 +33,18 @@ func GetPostsByUserIDPageAndLimit(id, page, limit int) (*[]Post, int, error) {
db.Model(&Post{}).Where("user_id=?", id).Count(&total)
return &posts, total, nil
}

// GetPostsByPageAndLimit func
func GetPostsByPageAndLimit(page, limit int) (*[]Post, int, error) {
var total int
var posts []Post

offset := (page - 1) * limit
if err := db.Preload("User").Offset(offset).Limit(limit).Order("timestamp desc").Find(&posts).Error; err != nil {
return nil, total, err
}

db.Model(&Post{}).Count(&total)

return &posts, total, nil
}
@@ -10,6 +10,7 @@
<div>
Blog:
<a href="/">Home</a>
<a href="/explore">Explore</a>
{{if .CurrentUser}}
<a href="/user/{{.CurrentUser}}">Profile</a>
<a href="/logout">Logout</a>
@@ -0,0 +1,19 @@
{{define "content"}}
<h1>Hello, {{.CurrentUser}}!</h1>

{{range .Posts}}
<table>
<tr valign="top">
<td><img src="{{.User.Avatar}}&s=36"></td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> says:<br>{{ .Body }}</td>
</tr>
</table>
{{end}}

{{ if gt .PrevPage 0 }}
<a href="/explore?page={{.PrevPage}}">Newer posts</a>
{{ end }}
{{ if gt .NextPage 0 }}
<a href="/explore?page={{.NextPage}}">Older posts</a>
{{ end }}
{{end}}
@@ -0,0 +1,25 @@
package vm

import "github.com/bonfy/go-mega-code/model"

// ExploreViewModel struct
type ExploreViewModel struct {
BaseViewModel
Posts []model.Post
BasePageViewModel
}

// ExploreViewModelOp struct
type ExploreViewModelOp struct{}

// GetVM func
func (ExploreViewModelOp) GetVM(username string, page, limit int) ExploreViewModel {
// posts, _ := model.GetAllPosts()
posts, total, _ := model.GetPostsByPageAndLimit(page, limit)
v := ExploreViewModel{}
v.SetTitle("Explore")
v.Posts = *posts
v.SetBasePageViewModel(total, page, limit)
v.SetCurrentUser(username)
return v
}

0 comments on commit 4609f2b

Please sign in to comment.
You can’t perform that action at this time.