Skip to content

Commit

Permalink
09-Pagination add explore
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Oct 8, 2018
1 parent fd3c825 commit 4609f2b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
15 changes: 15 additions & 0 deletions model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
19 changes: 19 additions & 0 deletions templates/content/explore.html
Original file line number Diff line number Diff line change
@@ -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}}
25 changes: 25 additions & 0 deletions vm/explore.go
Original file line number Diff line number Diff line change
@@ -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.