Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.",
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
result := db.Save(&currentUser)
if result.Error != nil {
var a, b int
a = 1
b = 0
fmt.Println(a / b)
session.Set("user", currentUser)
posts := [...]struct {
Author *models.User
Body string
{&currentUser, "Test post #1"},
{&currentUser, "Test post #2"},
c.HTML(http.StatusOK, "profile", gin.H{
"title": "Hi! GoBlog.",
"user": &currentUser,
"current_user": &currentUser,
"posts": posts,
} else {
c.HTML(http.StatusOK, "edit_profile", gin.H{
"title": "Hi! GoBlog.",
"error": "Required fields are not provided correctly.",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.