Skip to content
Permalink
Browse files

08-Follower improve profile add follow & unfollow

  • Loading branch information...
bonfy committed Sep 30, 2018
1 parent ef4e3aa commit b1502cf92a9e1ee9f77cfa2b661646e6e4fd414c
Showing with 73 additions and 7 deletions.
  1. +30 −0 controller/home.go
  2. +1 −1 model/post.go
  3. +8 −0 templates/content/profile.html
  4. +34 −6 vm/profile.go
@@ -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))

@@ -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)
}
@@ -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
@@ -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>
@@ -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
@@ -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.
You can’t perform that action at this time.