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"`
|
||||
Title string `json:"title" db:"title"`
|
||||
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"`
|
||||
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
|
||||
}
|
||||
@@ -28,7 +28,7 @@ type Item struct {
|
||||
type ArticleMetadata struct {
|
||||
Title string
|
||||
Description string
|
||||
ImageURL string
|
||||
Image *string
|
||||
}
|
||||
|
||||
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", "")
|
||||
|
||||
imageUrl := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
||||
if imageUrl == "" {
|
||||
image := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
||||
if image == "" {
|
||||
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||
src := s.AttrOr("src", "")
|
||||
if src != "" {
|
||||
imageUrl = src
|
||||
image = src
|
||||
return
|
||||
}
|
||||
})
|
||||
@@ -70,7 +70,7 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||
return &ArticleMetadata{
|
||||
Title: title,
|
||||
Description: description,
|
||||
ImageURL: imageUrl,
|
||||
Image: &image,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ CREATE TABLE IF NOT EXISTS items (
|
||||
url varchar not null unique,
|
||||
title varchar not null,
|
||||
description varchar,
|
||||
image_url varchar not null,
|
||||
image varchar default null,
|
||||
created_at datetime default current_timestamp,
|
||||
updated_at datetime default current_timestamp
|
||||
);
|
||||
@@ -133,11 +133,11 @@ END;
|
||||
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,
|
||||
"title": meta.Title,
|
||||
"description": meta.Description,
|
||||
"image_url": meta.ImageURL,
|
||||
"image": meta.Image,
|
||||
}); err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user