make item image field optional
This commit is contained in:
18
main.go
18
main.go
@@ -20,7 +20,7 @@ type Item struct {
|
|||||||
URL string `json:"url" db:"url"`
|
URL string `json:"url" db:"url"`
|
||||||
Title string `json:"title" db:"title"`
|
Title string `json:"title" db:"title"`
|
||||||
Description string `json:"description" db:"description"`
|
Description string `json:"description" db:"description"`
|
||||||
ImageURL string `json:"imageUrl" db:"image_url"`
|
Image *string `json:"image" db:"image"`
|
||||||
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
|
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ type Item struct {
|
|||||||
type ArticleMetadata struct {
|
type ArticleMetadata struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
ImageURL string
|
Image *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||||
@@ -56,12 +56,12 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
|||||||
|
|
||||||
description := doc.Find(`head>meta[name="og:description"]`).AttrOr("content", "")
|
description := doc.Find(`head>meta[name="og:description"]`).AttrOr("content", "")
|
||||||
|
|
||||||
imageUrl := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
image := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
||||||
if imageUrl == "" {
|
if image == "" {
|
||||||
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||||
src := s.AttrOr("src", "")
|
src := s.AttrOr("src", "")
|
||||||
if src != "" {
|
if src != "" {
|
||||||
imageUrl = src
|
image = src
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -70,7 +70,7 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
|||||||
return &ArticleMetadata{
|
return &ArticleMetadata{
|
||||||
Title: title,
|
Title: title,
|
||||||
Description: description,
|
Description: description,
|
||||||
ImageURL: imageUrl,
|
Image: &image,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ CREATE TABLE IF NOT EXISTS items (
|
|||||||
url varchar not null unique,
|
url varchar not null unique,
|
||||||
title varchar not null,
|
title varchar not null,
|
||||||
description varchar,
|
description varchar,
|
||||||
image_url varchar not null,
|
image varchar default null,
|
||||||
created_at datetime default current_timestamp,
|
created_at datetime default current_timestamp,
|
||||||
updated_at datetime default current_timestamp
|
updated_at datetime default current_timestamp
|
||||||
);
|
);
|
||||||
@@ -133,11 +133,11 @@ END;
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := db.NamedExec("INSERT INTO items (url, title, description, image_url) VALUES (:url, :title, :description, :image_url)", map[string]any{
|
if _, err := db.NamedExec("INSERT INTO items (url, title, description, image) VALUES (:url, :title, :description, :image)", map[string]any{
|
||||||
"url": pageUrl,
|
"url": pageUrl,
|
||||||
"title": meta.Title,
|
"title": meta.Title,
|
||||||
"description": meta.Description,
|
"description": meta.Description,
|
||||||
"image_url": meta.ImageURL,
|
"image": meta.Image,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
|
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user