func ProfileFn(c *gin.Context) { | |
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 | |
} | |
db := dbc.(*gorm.DB) | |
username := c.Param("name") | |
user := models.User{} | |
db.Where(&models.User{Username: username}).First(&user) | |
if user.Username != "" { | |
posts := [...]struct { | |
Author *models.User | |
Body string | |
}{ | |
{&user, "Test post #1"}, | |
{&user, "Test post #2"}, | |
} | |
c.HTML(http.StatusOK, "profile", gin.H{ | |
"title": "Hi! GoBlog.", | |
"user": &user, | |
"posts": posts, | |
}) | |
return | |
} else { | |
c.HTML(http.StatusOK, "index", gin.H{ | |
"title": "Hi! GoBlog.", | |
"error": "User not found", | |
}) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment