Skip to content

Instantly share code, notes, and snippets.

@mehmetron
Last active August 18, 2021 05:09
Show Gist options
  • Save mehmetron/d2528df63f9accc23fc7c5f134bbf7cd to your computer and use it in GitHub Desktop.
Save mehmetron/d2528df63f9accc23fc7c5f134bbf7cd to your computer and use it in GitHub Desktop.
type Env struct {
articles ArticleModel
}
type ArticleModel struct {
DB *sqlx.DB
}
func (env *Env) GetAllArticles(w http.ResponseWriter, r *http.Request) {
articles, err := env.articles.GetAllArticles()
if err != nil {
fmt.Println("19 ", err)
}
err = utils.WriteJSON(w, http.StatusOK, articles, nil)
if err != nil {
fmt.Println(err)
}
}
func (m ArticleModel) GetAllArticles() ([]Articles, error) {
var articles []Articles
query := `SELECT id, name, content FROM article ORDER BY name ASC`
err := m.DB.Select(&articles, query)
if err != nil {
return nil, err
}
fmt.Println("80 ", articles)
return articles, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment