Skip to content

Commit

Permalink
Added null check for uploadedImageLink
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Singh <[email protected]>
  • Loading branch information
SkySingh04 committed Apr 27, 2024
1 parent 20c8f97 commit 9665115
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
9 changes: 8 additions & 1 deletion db/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"blog/contracttesting/models"
"database/sql"
"log"
)

Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9665115

Please sign in to comment.