diff --git a/db/get.go b/db/get.go index 1b2dd8b..392302e 100644 --- a/db/get.go +++ b/db/get.go @@ -2,6 +2,7 @@ package db import ( "blog/contracttesting/models" + "database/sql" "log" ) @@ -53,11 +54,17 @@ func GetAllBlogPosts() ([]*models.BlogPost, error) { blogsSlice := []*models.BlogPost{} for rows.Next() { blog := &models.BlogPost{} - err := rows.Scan(&blog.ID, &blog.UserID, &blog.Title, &blog.Content, &blog.CreatedAt, &blog.Subtitle, &blog.Image , &blog.SpotifyLink , &blog.UploadedImageLink) + var uploadedImageLink sql.NullString // Use sql.NullString to handle NULL values + err := rows.Scan(&blog.ID, &blog.UserID, &blog.Title, &blog.Content, &blog.CreatedAt, &blog.Subtitle, &blog.Image , &blog.SpotifyLink , &uploadedImageLink) if err != nil { log.Printf("Error scanning blog row: %v\n", err) return nil, err } + if uploadedImageLink.Valid { // Check if the value is not NULL + blog.UploadedImageLink = uploadedImageLink.String // Assign the value to the struct field + } else { + blog.UploadedImageLink = "" // If NULL, assign an empty string or handle it as needed + } blogsSlice = append(blogsSlice, blog) } return blogsSlice, nil diff --git a/models/models.go b/models/models.go index bb3c1cd..4bdb9e2 100644 --- a/models/models.go +++ b/models/models.go @@ -8,15 +8,15 @@ type User struct { } type BlogPost struct { - ID int `json:"id"` - UserID int `json:"user_id"` - Title string `json:"title"` - Content string `json:"content"` - CreatedAt string `json:"created_at"` - Subtitle string `json:"subtitle"` - Image string `json:"image"` - SpotifyLink string `json:"spotifyLink"` - UploadedImageLink string `json:"uploadedImageLink"` + ID int `json:"id"` + UserID int `json:"user_id"` + Title string `json:"title"` + Content string `json:"content"` + CreatedAt string `json:"created_at"` + Subtitle string `json:"subtitle"` + Image string `json:"image"` + SpotifyLink string `json:"spotifyLink,omitempty"` + UploadedImageLink string `json:"uploadedImageLink"` } type Comment struct {