Instantly share code, notes, and snippets.

Embed
What would you like to do?
func ProfileFn(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
}
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,
"current_user": &currentUser,
})
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