Skip to content
Permalink
Browse files

07-Profile-Page add profile_edit

  • Loading branch information...
bonfy committed Sep 28, 2018
1 parent 4e8cba8 commit a0a50dfc38c61949f065d0e02ef5bb0e89ffcdd0
Showing with 84 additions and 1 deletion.
  1. +26 −0 controller/home.go
  2. +6 −0 model/user.go
  3. +5 −1 templates/content/profile.html
  4. +17 −0 templates/content/profile_edit.html
  5. +2 −0 vm/profile.go
  6. +28 −0 vm/profile_edit.go
@@ -17,6 +17,7 @@ func (h home) registerRoutes() {
r.HandleFunc("/login", loginHandler)
r.HandleFunc("/register", registerHandler)
r.HandleFunc("/user/{username}", middleAuth(profileHandler))
r.HandleFunc("/profile_edit", middleAuth(profileEditHandler))
r.HandleFunc("/", middleAuth(indexHandler))

http.Handle("/", r)
@@ -106,3 +107,28 @@ func profileHandler(w http.ResponseWriter, r *http.Request) {
}
templates[tpName].Execute(w, &v)
}

func profileEditHandler(w http.ResponseWriter, r *http.Request) {
tpName := "profile_edit.html"
username, _ := getSessionUser(r)
vop := vm.ProfileEditViewModelOp{}
v := vop.GetVM(username)
if r.Method == http.MethodGet {
err := templates[tpName].Execute(w, &v)
if err != nil {
log.Println(err)
}
}

if r.Method == http.MethodPost {
r.ParseForm()
aboutme := r.Form.Get("aboutme")
log.Println(aboutme)
if err := vm.UpdateAboutMe(username, aboutme); err != nil {
log.Println("update Aboutme error:", err)
w.Write([]byte("Error update aboutme"))
return
}
http.Redirect(w, r, fmt.Sprintf("/user/%s", username), http.StatusSeeOther)
}
}
@@ -64,3 +64,9 @@ func UpdateLastSeen(username string) error {
contents := map[string]interface{}{"last_seen": time.Now()}
return UpdateUserByUsername(username, contents)
}

// UpdateAboutMe func
func UpdateAboutMe(username, text string) error {
contents := map[string]interface{}{"about_me": text}
return UpdateUserByUsername(username, contents)
}
@@ -6,12 +6,16 @@
<td>
<h1>User: {{.ProfileUser.Username}}</h1>
{{if .ProfileUser.AboutMe}}
<p>{{ .ProfileUser.AboutMe }}</p>
<p><pre>{{ .ProfileUser.AboutMe }}</pre></p>
{{end}}

{{if .ProfileUser.LastSeen}}
<p>Last seen on: {{ .ProfileUser.LastSeen }}</p>
{{end}}

{{if .Editable}}
<p><a href="/profile_edit">Edit your profile</a></p>
{{end}}
</td>
</tr>
</table>
@@ -0,0 +1,17 @@
{{define "content"}}
<h1>Profile Edit</h1>
<p>Username: {{.ProfileUser.Username}}</p>
<form action="/profile_edit" method="post" name="profile_edit">
<p>About Me</p>
<p><textarea name="aboutme" rows="5" cols="80" value="" placeholder="about me">{{.ProfileUser.AboutMe}}</textarea></p>
<p><input type="submit" name="submit" value="Save"></p>
</form>

{{if .Errs}}
<ul>
{{range .Errs}}
<li>{{.}}</li>
{{end}}
</ul>
{{end}}
{{end}}
@@ -6,6 +6,7 @@ import "github.com/bonfy/go-mega-code/model"
type ProfileViewModel struct {
BaseViewModel
Posts []model.Post
Editable bool
ProfileUser model.User
}

@@ -22,6 +23,7 @@ func (ProfileViewModelOp) GetVM(sUser, pUser string) (ProfileViewModel, error) {
}
posts, _ := model.GetPostsByUserID(u1.ID)
v.ProfileUser = *u1
v.Editable = (sUser == pUser)
v.Posts = *posts
v.SetCurrentUser(sUser)
return v, nil
@@ -0,0 +1,28 @@
package vm

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

// ProfileEditViewModel struct
type ProfileEditViewModel struct {
LoginViewModel
ProfileUser model.User
}

// ProfileEditViewModelOp struct
type ProfileEditViewModelOp struct{}

// GetVM func
func (ProfileEditViewModelOp) GetVM(username string) ProfileEditViewModel {

v := ProfileEditViewModel{}
u, _ := model.GetUserByUsername(username)
v.SetTitle("Profile Edit")
v.SetCurrentUser(username)
v.ProfileUser = *u
return v
}

// UpdateAboutMe func
func UpdateAboutMe(username, text string) error {
return model.UpdateAboutMe(username, text)
}

0 comments on commit a0a50df

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