Slide 38
Slide 38 text
(P$POGFSFODF4QSJOH
package api
func GetUserWithProfile(c echo.Context) error {
n := c.Param("username")
u, e := model.FindUserByUsernameWithProfile(n)
if e != nil {
return NewError(http.StatusNotFound, e, "not found")
}
return c.JSON(http.StatusOK, u))
}
package model
func FindUserByUsernameWithProfile(n string) (*User, error) {
u := new(User)
if err := db.Preload("Profile").Scopes(Enabled).Where("username = ?", n).First(u).Error; err != nil {
return nil, err
}
return u, nil
}
มߋલ