Last active
June 30, 2018 23:00
-
-
Save Cibernomadas/fac721c5945ba488f123804c4b9600cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func EditProfileFn(c *gin.Context) { | |
currentUser := models.User{} | |
session := sessions.Default(c) | |
u := session.Get("user") | |
if u != nil { | |
currentUser = u.(models.User) | |
} | |
dbc, exist := c.Get("db") | |
if !exist { | |
c.HTML(http.StatusOK, "index", gin.H{ | |
"title": "Hi! GoBlog.", | |
"error": "We've got an internal error, please try later.", | |
}) | |
return | |
} | |
if c.Request.Method == http.MethodGet { // Edit profile form | |
c.HTML(http.StatusOK, "edit_profile", gin.H{ | |
"title": "Hi! GoBlog.", | |
"user": currentUser, | |
}) | |
} else if c.Request.Method == http.MethodPost { // Process profile form | |
var profileForm models.EditProfile | |
if err := c.ShouldBind(&profileForm); err == nil { | |
db := dbc.(*gorm.DB) | |
currentUser.Username = profileForm.Username | |
currentUser.AboutMe = profileForm.AboutMe | |
db.Save(¤tUser) | |
session.Set("user", currentUser) | |
session.Save() | |
posts := [...]struct { | |
Author *models.User | |
Body string | |
}{ | |
{¤tUser, "Test post #1"}, | |
{¤tUser, "Test post #2"}, | |
} | |
c.HTML(http.StatusOK, "profile", gin.H{ | |
"title": "Hi! GoBlog.", | |
"user": ¤tUser, | |
"current_user": ¤tUser, | |
"posts": posts, | |
}) | |
} else { | |
c.HTML(http.StatusOK, "edit_profile", gin.H{ | |
"title": "Hi! GoBlog.", | |
"error": "Required fields are not provided correctly.", | |
}) | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment