Skip to content

Commit

Permalink
08-Follower improve profile add follow & unfollow
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Sep 30, 2018
1 parent ef4e3aa commit b1502cf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
30 changes: 30 additions & 0 deletions controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func (h home) registerRoutes() {
r.HandleFunc("/login", loginHandler)
r.HandleFunc("/register", registerHandler)
r.HandleFunc("/user/{username}", middleAuth(profileHandler))
r.HandleFunc("/follow/{username}", middleAuth(followHandler))
r.HandleFunc("/unfollow/{username}", middleAuth(unFollowHandler))
r.HandleFunc("/profile_edit", middleAuth(profileEditHandler))
r.HandleFunc("/", middleAuth(indexHandler))

Expand Down Expand Up @@ -132,3 +134,31 @@ func profileEditHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprintf("/user/%s", username), http.StatusSeeOther)
}
}

func followHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pUser := vars["username"]
sUser, _ := getSessionUser(r)

err := vm.Follow(sUser, pUser)
if err != nil {
log.Println("Follow error:", err)
w.Write([]byte("Error in Follow"))
return
}
http.Redirect(w, r, fmt.Sprintf("/user/%s", pUser), http.StatusSeeOther)
}

func unFollowHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pUser := vars["username"]
sUser, _ := getSessionUser(r)

err := vm.UnFollow(sUser, pUser)
if err != nil {
log.Println("UnFollow error:", err)
w.Write([]byte("Error in UnFollow"))
return
}
http.Redirect(w, r, fmt.Sprintf("/user/%s", pUser), http.StatusSeeOther)
}
2 changes: 1 addition & 1 deletion model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Post struct {
// GetPostsByUserID func
func GetPostsByUserID(id int) (*[]Post, error) {
var posts []Post
if err := db.Preload("User").Where("user_id=?", id).Find(&posts).Error; err != nil {
if err := db.Preload("User").Order("timestamp desc").Where("user_id=?", id).Find(&posts).Error; err != nil {
return nil, err
}
return &posts, nil
Expand Down
8 changes: 8 additions & 0 deletions templates/content/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ <h1>User: {{.ProfileUser.Username}}</h1>
<p>Last seen on: {{ .ProfileUser.LastSeen }}</p>
{{end}}

<p>{{ .FollowersCount }} followers, {{ .FollowingCount }} following.</p>

{{if .Editable}}
<p><a href="/profile_edit">Edit your profile</a></p>
{{else}}
{{if .IsFollow}}
<p><a href="/unfollow/{{.ProfileUser.Username}}">Unfollow</a></p>
{{else}}
<p><a href="/follow/{{.ProfileUser.Username}}">Follow</a></p>
{{end}}
{{end}}
</td>
</tr>
Expand Down
40 changes: 34 additions & 6 deletions vm/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import "github.com/bonfy/go-mega-code/model"
// ProfileViewModel struct
type ProfileViewModel struct {
BaseViewModel
Posts []model.Post
Editable bool
ProfileUser model.User
Posts []model.Post
Editable bool
IsFollow bool
FollowersCount int
FollowingCount int
ProfileUser model.User
}

// ProfileViewModelOp struct
Expand All @@ -17,14 +20,39 @@ type ProfileViewModelOp struct{}
func (ProfileViewModelOp) GetVM(sUser, pUser string) (ProfileViewModel, error) {
v := ProfileViewModel{}
v.SetTitle("Profile")
u1, err := model.GetUserByUsername(pUser)
u, err := model.GetUserByUsername(pUser)
if err != nil {
return v, err
}
posts, _ := model.GetPostsByUserID(u1.ID)
v.ProfileUser = *u1
posts, _ := model.GetPostsByUserID(u.ID)
v.ProfileUser = *u
v.Editable = (sUser == pUser)

if !v.Editable {
v.IsFollow = u.IsFollowedByUser(sUser)
}
v.FollowersCount = u.FollowersCount()
v.FollowingCount = u.FollowingCount()

v.Posts = *posts
v.SetCurrentUser(sUser)
return v, nil
}

// Follow func : A follow B
func Follow(a, b string) error {
u, err := model.GetUserByUsername(a)
if err != nil {
return err
}
return u.Follow(b)
}

// UnFollow func : A unfollow B
func UnFollow(a, b string) error {
u, err := model.GetUserByUsername(a)
if err != nil {
return err
}
return u.Unfollow(b)
}

0 comments on commit b1502cf

Please sign in to comment.