Skip to content
Permalink
Browse files

13-Javascript-Magic add server popup route

  • Loading branch information...
bonfy committed Oct 19, 2018
1 parent b5cdcf1 commit f48f4a33425c1a10622ab721adee43053cf96ccc
Showing with 64 additions and 0 deletions.
  1. +16 −0 controller/home.go
  2. +29 −0 templates/content/popup.html
  3. +19 −0 vm/profile.go
@@ -27,6 +27,7 @@ func (h home) registerRoutes() {
r.HandleFunc("/explore", middleAuth(exploreHandler))
r.HandleFunc("/reset_password_request", resetPasswordRequestHandler)
r.HandleFunc("/reset_password/{token}", resetPasswordHandler)
r.HandleFunc("/user/{username}/popup", popupHandler)
r.HandleFunc("/404", notfoundHandler)
r.HandleFunc("/", middleAuth(indexHandler))

@@ -277,6 +278,21 @@ func resetPasswordHandler(w http.ResponseWriter, r *http.Request) {
}
}

func popupHandler(w http.ResponseWriter, r *http.Request) {
tpName := "popup.html"
vars := mux.Vars(r)
pUser := vars["username"]
sUser, _ := getSessionUser(r)
vop := vm.ProfileViewModelOp{}
v, err := vop.GetPopupVM(sUser, pUser)
if err != nil {
msg := fmt.Sprintf("user ( %s ) does not exist", pUser)
w.Write([]byte(msg))
return
}
templates[tpName].Execute(w, &v)
}

func notfoundHandler(w http.ResponseWriter, r *http.Request) {
flash := getFlash(w, r)
message := vm.NotFoundMessage{Flash: flash}
@@ -0,0 +1,29 @@
<table>
<tr valign="top">
<td width="64" style="border: 0px;"><img src="{{.ProfileUser.Avatar}}&s=64"></td>
<td style="border: 0px;">
<small>
<p><a href="/user/{{.ProfileUser.Username}}">{{.ProfileUser.Username}}</a></p>
{{if .ProfileUser.AboutMe}}
<p>{{ .ProfileUser.AboutMe }}</p>
{{end}}

{{if .ProfileUser.LastSeen}}
<p>Last seen on: {{ .ProfileUser.FormattedLastSeen }}</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 class="btn btn-outline-primary" href="/unfollow/{{.ProfileUser.Username}}">Unfollow</a></p>
{{else}}
<p><a class="btn btn-outline-primary" href="/follow/{{.ProfileUser.Username}}">Follow</a></p>
{{end}}
{{end}}
</small>
</td>
</tr>
</table>
@@ -40,6 +40,25 @@ func (ProfileViewModelOp) GetVM(sUser, pUser string, page, limit int) (ProfileVi
return v, nil
}

// GetPopupVM func
func (ProfileViewModelOp) GetPopupVM(sUser, pUser string) (ProfileViewModel, error) {
v := ProfileViewModel{}
v.SetTitle("Profile")
u, err := model.GetUserByUsername(pUser)
if err != nil {
return v, err
}
v.ProfileUser = *u
v.Editable = (sUser == pUser)
if !v.Editable {
v.IsFollow = u.IsFollowedByUser(sUser)
}
v.FollowersCount = u.FollowersCount()
v.FollowingCount = u.FollowingCount()
v.SetCurrentUser(sUser)
return v, nil
}

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

0 comments on commit f48f4a3

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