mirror of
https://github.com/go-shiori/shiori.git
synced 2025-09-13 00:16:34 +08:00
Clean up code
This commit is contained in:
parent
a03239b988
commit
0a4850eacb
6 changed files with 9 additions and 24 deletions
|
@ -2,7 +2,6 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
db "github.com/RadhiFadlillah/shiori/database"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -50,7 +49,7 @@ func init() {
|
||||||
|
|
||||||
func openBookmarks(args ...string) {
|
func openBookmarks(args ...string) {
|
||||||
// Read bookmarks from database
|
// Read bookmarks from database
|
||||||
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{}, args...)
|
bookmarks, err := DB.GetBookmarks(false, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -76,9 +75,7 @@ func openBookmarks(args ...string) {
|
||||||
|
|
||||||
func openBookmarksCache(trimSpace bool, args ...string) {
|
func openBookmarksCache(trimSpace bool, args ...string) {
|
||||||
// Read bookmark content from database
|
// Read bookmark content from database
|
||||||
bookmarks, err := DB.GetBookmarks(
|
bookmarks, err := DB.GetBookmarks(true, args...)
|
||||||
db.GetBookmarksOptions{WithContents: true},
|
|
||||||
args...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -3,7 +3,6 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
db "github.com/RadhiFadlillah/shiori/database"
|
|
||||||
"github.com/RadhiFadlillah/shiori/model"
|
"github.com/RadhiFadlillah/shiori/model"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
|
@ -23,7 +22,7 @@ var (
|
||||||
indexOnly, _ := cmd.Flags().GetBool("index-only")
|
indexOnly, _ := cmd.Flags().GetBool("index-only")
|
||||||
|
|
||||||
// Read bookmarks from database
|
// Read bookmarks from database
|
||||||
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{}, args...)
|
bookmarks, err := DB.GetBookmarks(false, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
db "github.com/RadhiFadlillah/shiori/database"
|
|
||||||
"github.com/RadhiFadlillah/shiori/model"
|
"github.com/RadhiFadlillah/shiori/model"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/dgrijalva/jwt-go/request"
|
"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")
|
id := ps.ByName("id")
|
||||||
|
|
||||||
// Read bookmarks
|
// Read bookmarks
|
||||||
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{WithContents: true}, id)
|
bookmarks, err := DB.GetBookmarks(true, id)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
if len(bookmarks) == 0 {
|
if len(bookmarks) == 0 {
|
||||||
|
@ -159,7 +158,7 @@ func apiGetBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Param
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
// Fetch all bookmarks
|
// Fetch all bookmarks
|
||||||
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{OrderLatest: true})
|
bookmarks, err := DB.GetBookmarks(false)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(&bookmarks)
|
err = json.NewEncoder(w).Encode(&bookmarks)
|
||||||
|
|
|
@ -3,7 +3,6 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/RadhiFadlillah/go-readability"
|
"github.com/RadhiFadlillah/go-readability"
|
||||||
db "github.com/RadhiFadlillah/shiori/database"
|
|
||||||
"github.com/RadhiFadlillah/shiori/model"
|
"github.com/RadhiFadlillah/shiori/model"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -84,7 +83,7 @@ func init() {
|
||||||
func updateBookmarks(indices []string, url, title, excerpt string, tags []string, offline bool) ([]model.Bookmark, error) {
|
func updateBookmarks(indices []string, url, title, excerpt string, tags []string, offline bool) ([]model.Bookmark, error) {
|
||||||
mutex := sync.Mutex{}
|
mutex := sync.Mutex{}
|
||||||
// Read bookmarks from database
|
// Read bookmarks from database
|
||||||
bookmarks, err := DB.GetBookmarks(db.GetBookmarksOptions{WithContents: true}, indices...)
|
bookmarks, err := DB.GetBookmarks(true, indices...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []model.Bookmark{}, err
|
return []model.Bookmark{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ type Database interface {
|
||||||
CreateBookmark(bookmark model.Bookmark) (int64, error)
|
CreateBookmark(bookmark model.Bookmark) (int64, error)
|
||||||
|
|
||||||
// GetBookmarks fetch list of bookmarks based on submitted indices.
|
// 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 removes all record with matching indices from database.
|
||||||
DeleteBookmarks(indices ...string) ([]int, []int, error)
|
DeleteBookmarks(indices ...string) ([]int, []int, error)
|
||||||
|
@ -32,11 +32,6 @@ type Database interface {
|
||||||
DeleteAccounts(usernames ...string) error
|
DeleteAccounts(usernames ...string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetBookmarksOptions struct {
|
|
||||||
WithContents bool
|
|
||||||
OrderLatest bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkError(err error) {
|
func checkError(err error) {
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -161,7 +161,7 @@ func (db *SQLiteDatabase) CreateBookmark(bookmark model.Bookmark) (bookmarkID in
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBookmarks fetch list of bookmarks based on submitted indices.
|
// 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
|
// Convert list of index to int
|
||||||
listIndex := []int{}
|
listIndex := []int{}
|
||||||
errInvalidIndex := fmt.Errorf("Index is not valid")
|
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
|
min_read_time, max_read_time, modified
|
||||||
FROM bookmark` + whereClause
|
FROM bookmark` + whereClause
|
||||||
|
|
||||||
if options.OrderLatest {
|
|
||||||
query += ` ORDER BY modified DESC`
|
|
||||||
}
|
|
||||||
|
|
||||||
bookmarks := []model.Bookmark{}
|
bookmarks := []model.Bookmark{}
|
||||||
err := db.Select(&bookmarks, query, args...)
|
err := db.Select(&bookmarks, query, args...)
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
|
@ -246,7 +242,7 @@ func (db *SQLiteDatabase) GetBookmarks(options GetBookmarksOptions, indices ...s
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.WithContents {
|
if withContent {
|
||||||
err = stmtGetContent.Get(&book, book.ID)
|
err = stmtGetContent.Get(&book, book.ID)
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Add table
Reference in a new issue