Clean up code

This commit is contained in:
Radhi Fadlillah 2018-02-22 20:45:43 +07:00
parent a03239b988
commit 0a4850eacb
6 changed files with 9 additions and 24 deletions

View file

@ -2,7 +2,6 @@ package cmd
import (
"fmt"
db "github.com/RadhiFadlillah/shiori/database"
"github.com/spf13/cobra"
"os/exec"
"strings"
@ -50,7 +49,7 @@ func init() {
func openBookmarks(args ...string) {
// Read bookmarks from database
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{}, args...)
bookmarks, err := DB.GetBookmarks(false, args...)
if err != nil {
cError.Println(err)
return
@ -76,9 +75,7 @@ func openBookmarks(args ...string) {
func openBookmarksCache(trimSpace bool, args ...string) {
// Read bookmark content from database
bookmarks, err := DB.GetBookmarks(
db.GetBookmarksOptions{WithContents: true},
args...)
bookmarks, err := DB.GetBookmarks(true, args...)
if err != nil {
cError.Println(err)
return

View file

@ -3,7 +3,6 @@ package cmd
import (
"encoding/json"
"fmt"
db "github.com/RadhiFadlillah/shiori/database"
"github.com/RadhiFadlillah/shiori/model"
"github.com/spf13/cobra"
"os"
@ -23,7 +22,7 @@ var (
indexOnly, _ := cmd.Flags().GetBool("index-only")
// Read bookmarks from database
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{}, args...)
bookmarks, err := DB.GetBookmarks(false, args...)
if err != nil {
cError.Println(err)
os.Exit(1)

View file

@ -4,7 +4,6 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
db "github.com/RadhiFadlillah/shiori/database"
"github.com/RadhiFadlillah/shiori/model"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
@ -99,7 +98,7 @@ func serveBookmarkCache(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
id := ps.ByName("id")
// Read bookmarks
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{WithContents: true}, id)
bookmarks, err := DB.GetBookmarks(true, id)
checkError(err)
if len(bookmarks) == 0 {
@ -159,7 +158,7 @@ func apiGetBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Param
checkError(err)
// Fetch all bookmarks
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{OrderLatest: true})
bookmarks, err := DB.GetBookmarks(false)
checkError(err)
err = json.NewEncoder(w).Encode(&bookmarks)

View file

@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"github.com/RadhiFadlillah/go-readability"
db "github.com/RadhiFadlillah/shiori/database"
"github.com/RadhiFadlillah/shiori/model"
"github.com/spf13/cobra"
"html/template"
@ -84,7 +83,7 @@ func init() {
func updateBookmarks(indices []string, url, title, excerpt string, tags []string, offline bool) ([]model.Bookmark, error) {
mutex := sync.Mutex{}
// Read bookmarks from database
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{WithContents: true}, indices...)
bookmarks, err := DB.GetBookmarks(true, indices...)
if err != nil {
return []model.Bookmark{}, err
}

View file

@ -11,7 +11,7 @@ type Database interface {
CreateBookmark(bookmark model.Bookmark) (int64, error)
// GetBookmarks fetch list of bookmarks based on submitted indices.
GetBookmarks(options GetBookmarksOptions, indices ...string) ([]model.Bookmark, error)
GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error)
// DeleteBookmarks removes all record with matching indices from database.
DeleteBookmarks(indices ...string) ([]int, []int, error)
@ -32,11 +32,6 @@ type Database interface {
DeleteAccounts(usernames ...string) error
}
type GetBookmarksOptions struct {
WithContents bool
OrderLatest bool
}
func checkError(err error) {
if err != nil && err != sql.ErrNoRows {
panic(err)

View file

@ -161,7 +161,7 @@ func (db *SQLiteDatabase) CreateBookmark(bookmark model.Bookmark) (bookmarkID in
}
// GetBookmarks fetch list of bookmarks based on submitted indices.
func (db *SQLiteDatabase) GetBookmarks(options GetBookmarksOptions, indices ...string) ([]model.Bookmark, error) {
func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error) {
// Convert list of index to int
listIndex := []int{}
errInvalidIndex := fmt.Errorf("Index is not valid")
@ -213,10 +213,6 @@ func (db *SQLiteDatabase) GetBookmarks(options GetBookmarksOptions, indices ...s
min_read_time, max_read_time, modified
FROM bookmark` + whereClause
if options.OrderLatest {
query += ` ORDER BY modified DESC`
}
bookmarks := []model.Bookmark{}
err := db.Select(&bookmarks, query, args...)
if err != nil && err != sql.ErrNoRows {
@ -246,7 +242,7 @@ func (db *SQLiteDatabase) GetBookmarks(options GetBookmarksOptions, indices ...s
return nil, err
}
if options.WithContents {
if withContent {
err = stmtGetContent.Get(&book, book.ID)
if err != nil && err != sql.ErrNoRows {
return nil, err