From 693fedd58be44ba7e645dace02c82bf5f2a970c9 Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Tue, 18 Nov 2025 09:28:53 +0200 Subject: [PATCH] =?UTF-8?q?lis=C3=A4=C3=A4=20header=20ja=20hoida=20=20null?= =?UTF-8?q?=20kategoriat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/database/queries.go | 22 +++++++++++++--------- web/templates/index.html | 35 +++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/internal/database/queries.go b/internal/database/queries.go index 75e7267..a71c720 100644 --- a/internal/database/queries.go +++ b/internal/database/queries.go @@ -7,11 +7,11 @@ import ( ) type Article struct { - Id int - Title string - Content string - Picture string - Category string + Id int + Title string + Content string + Picture string + CategoryId string } type Category struct { @@ -20,7 +20,7 @@ type Category struct { } func GetArticles(db *sql.DB) ([]Article, error) { - rows, err := db.Query("SELECT article_id, article_title, article_content, article_picture, category_name FROM articles INNER JOIN categories ON articles.category_id = categories.category_id;") + rows, err := db.Query("SELECT * FROM articles") if err != nil { return nil, err } @@ -28,9 +28,13 @@ func GetArticles(db *sql.DB) ([]Article, error) { for rows.Next() { article := Article{} - if err := rows.Scan(&article.Id, &article.Title, &article.Content, &article.Picture, &article.Category); err != nil { + var category_id sql.NullString + if err := rows.Scan(&article.Id, &article.Title, &article.Content, &article.Picture, &category_id); err != nil { return nil, err } + if category_id.Valid { + article.CategoryId = category_id.String + } articles = append(articles, article) } @@ -56,7 +60,7 @@ func GetCategories(db *sql.DB) ([]Category, error) { } func GetCategoryArticles(db *sql.DB, category string) ([]Article, error) { - stmtOut, err := db.Prepare("SELECT article_id, article_title, article_content, article_picture, category_name FROM articles INNER JOIN categories ON articles.category_id = categories.category_id WHERE category_name = ?;") + stmtOut, err := db.Prepare("SELECT article_id, article_title, article_content, article_picture FROM articles INNER JOIN categories ON articles.category_id = categories.category_id WHERE category_name = ?;") if err != nil { return nil, err } @@ -70,7 +74,7 @@ func GetCategoryArticles(db *sql.DB, category string) ([]Article, error) { for rows.Next() { article := Article{} - if err := rows.Scan(&article.Id, &article.Title, &article.Content, &article.Picture, &article.Category); err != nil { + if err := rows.Scan(&article.Id, &article.Title, &article.Content, &article.Picture); err != nil { return nil, err } articles = append(articles, article) diff --git a/web/templates/index.html b/web/templates/index.html index 10abebc..716dbc7 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -1,13 +1,32 @@ {{ define "index.html" }} - - - - - {{range .Articles}} + + + + + Uutissivusto + + + +
+ + +
+ {{range .Articles}} {{ .}}
{{end}} - {{range .Categories}} - {{ .Name }}
- {{end}} + {{ end }} \ No newline at end of file -- 2.47.3