Clean up code for saving bookmark

This commit is contained in:
Radhi Fadlillah 2018-01-30 16:16:25 +07:00
parent e9559e724c
commit a454641d81
4 changed files with 54 additions and 61 deletions

View file

@ -2,6 +2,7 @@ package cmd
import (
"github.com/RadhiFadlillah/go-readability"
"github.com/RadhiFadlillah/shiori/model"
"github.com/spf13/cobra"
"os"
"time"
@ -39,34 +40,48 @@ func init() {
}
func addBookmark(url, title, excerpt string, tags []string, offline bool) (err error) {
// Prepare variable
defaultArticle := readability.Article{
URL: url,
Meta: readability.Metadata{
Title: "Untitled",
},
}
article := defaultArticle
// Fetch data from internet
article := readability.Article{}
if !offline {
article, err = readability.Parse(url, 10*time.Second)
if err != nil {
cError.Println("Failed to fetch article from internet")
article = defaultArticle
cError.Println("Failed to fetch article from internet:", err)
article.URL = url
article.Meta.Title = "Untitled"
}
}
// Prepare bookmark
bookmark := model.Bookmark{
URL: article.URL,
Title: article.Meta.Title,
ImageURL: article.Meta.Image,
Excerpt: article.Meta.Excerpt,
Author: article.Meta.Author,
Language: article.Meta.Language,
MinReadTime: article.Meta.MinReadTime,
MaxReadTime: article.Meta.MaxReadTime,
Content: article.Content,
}
bookTags := make([]model.Tag, len(tags))
for i, tag := range tags {
bookTags[i].Name = tag
}
bookmark.Tags = bookTags
// Set custom value
if title != "" {
article.Meta.Title = title
bookmark.Title = title
}
if excerpt != "" {
article.Meta.Excerpt = excerpt
bookmark.Excerpt = excerpt
}
bookmark, err := DB.SaveBookmark(article, tags...)
// Save to database
bookmark.ID, err = DB.SaveBookmark(bookmark)
if err != nil {
return err
}

View file

@ -2,12 +2,11 @@ package database
import (
"database/sql"
"github.com/RadhiFadlillah/go-readability"
"github.com/RadhiFadlillah/shiori/model"
)
type Database interface {
SaveBookmark(article readability.Article, tags ...string) (model.Bookmark, error)
SaveBookmark(bookmark model.Bookmark) (int64, error)
GetBookmarks(indices ...string) ([]model.Bookmark, error)
DeleteBookmarks(indices ...string) ([]int, []int, error)
SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error)

View file

@ -3,13 +3,11 @@ package database
import (
"database/sql"
"fmt"
"github.com/RadhiFadlillah/go-readability"
"github.com/RadhiFadlillah/shiori/model"
"github.com/jmoiron/sqlx"
"sort"
"strconv"
"strings"
"time"
)
type SQLiteDatabase struct {
@ -76,20 +74,20 @@ func OpenSQLiteDatabase() (*SQLiteDatabase, error) {
return &SQLiteDatabase{*db}, err
}
func (db *SQLiteDatabase) SaveBookmark(article readability.Article, tags ...string) (bookmark model.Bookmark, err error) {
func (db *SQLiteDatabase) SaveBookmark(bookmark model.Bookmark) (bookmarkID int64, err error) {
// Check URL and title
if article.URL == "" {
return model.Bookmark{}, fmt.Errorf("URL must not empty")
if bookmark.URL == "" {
return -1, fmt.Errorf("URL must not empty")
}
if article.Meta.Title == "" {
return model.Bookmark{}, fmt.Errorf("Title must not empty")
if bookmark.Title == "" {
return -1, fmt.Errorf("Title must not empty")
}
// Prepare transaction
tx, err := db.Beginx()
if err != nil {
return model.Bookmark{}, err
return -1, err
}
// Make sure to rollback if panic ever happened
@ -98,7 +96,7 @@ func (db *SQLiteDatabase) SaveBookmark(article readability.Article, tags ...stri
panicErr, _ := r.(error)
tx.Rollback()
bookmark = model.Bookmark{}
bookmarkID = -1
err = panicErr
}
}()
@ -108,23 +106,23 @@ func (db *SQLiteDatabase) SaveBookmark(article readability.Article, tags ...stri
url, title, image_url, excerpt, author,
language, min_read_time, max_read_time)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)`,
article.URL,
article.Meta.Title,
article.Meta.Image,
article.Meta.Excerpt,
article.Meta.Author,
article.Meta.Language,
article.Meta.MinReadTime,
article.Meta.MaxReadTime)
bookmark.URL,
bookmark.Title,
bookmark.ImageURL,
bookmark.Excerpt,
bookmark.Author,
bookmark.Language,
bookmark.MinReadTime,
bookmark.MaxReadTime)
// Get last inserted ID
bookmarkID, err := res.LastInsertId()
bookmarkID, err = res.LastInsertId()
checkError(err)
// Save bookmark content
tx.MustExec(`INSERT INTO bookmark_content
(docid, title, content) VALUES (?, ?, ?)`,
bookmarkID, article.Meta.Title, article.Content)
bookmarkID, bookmark.Title, bookmark.Content)
// Save tags
stmtGetTag, err := tx.Preparex(`SELECT id FROM tag WHERE name = ?`)
@ -136,48 +134,28 @@ func (db *SQLiteDatabase) SaveBookmark(article readability.Article, tags ...stri
stmtInsertBookmarkTag, err := tx.Preparex(`INSERT OR IGNORE INTO bookmark_tag (tag_id, bookmark_id) VALUES (?, ?)`)
checkError(err)
bookmarkTags := []model.Tag{}
for _, tag := range tags {
tag = strings.ToLower(tag)
tag = strings.TrimSpace(tag)
for _, tag := range bookmark.Tags {
tagName := strings.ToLower(tag.Name)
tagName = strings.TrimSpace(tagName)
tagID := int64(-1)
err = stmtGetTag.Get(&tagID, tag)
err = stmtGetTag.Get(&tagID, tagName)
checkError(err)
if tagID == -1 {
res := stmtInsertTag.MustExec(tag)
res := stmtInsertTag.MustExec(tagName)
tagID, err = res.LastInsertId()
checkError(err)
}
stmtInsertBookmarkTag.Exec(tagID, bookmarkID)
bookmarkTags = append(bookmarkTags, model.Tag{
ID: tagID,
Name: tag,
})
}
// Commit transaction
err = tx.Commit()
checkError(err)
// Return result
bookmark = model.Bookmark{
ID: bookmarkID,
URL: article.URL,
Title: article.Meta.Title,
ImageURL: article.Meta.Image,
Excerpt: article.Meta.Excerpt,
Author: article.Meta.Author,
Language: article.Meta.Language,
MinReadTime: article.Meta.MinReadTime,
MaxReadTime: article.Meta.MaxReadTime,
Modified: time.Now().Format("2006-01-02 15:04:05"),
Tags: bookmarkTags,
}
return bookmark, err
return bookmarkID, err
}
func (db *SQLiteDatabase) GetBookmarks(indices ...string) ([]model.Bookmark, error) {

View file

@ -16,5 +16,6 @@ type Bookmark struct {
MinReadTime int `db:"min_read_time" json:"minReadTime"`
MaxReadTime int `db:"max_read_time" json:"maxReadTime"`
Modified string `db:"modified" json:"modified"`
Content string `db:"content" json:"-"`
Tags []Tag `json:"tags"`
}