mirror of
https://github.com/go-shiori/shiori.git
synced 2025-02-22 23:14:02 +08:00
Add code documentation
This commit is contained in:
parent
e410e47ae4
commit
db688e9651
4 changed files with 20 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// DB is database that used by this cli
|
||||||
DB database.Database
|
DB database.Database
|
||||||
|
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
|
|
|
@ -5,11 +5,21 @@ import (
|
||||||
"github.com/RadhiFadlillah/shiori/model"
|
"github.com/RadhiFadlillah/shiori/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Database is interface for manipulating data in database.
|
||||||
type Database interface {
|
type Database interface {
|
||||||
|
// SaveBookmark saves new bookmark to database.
|
||||||
SaveBookmark(bookmark model.Bookmark) (int64, error)
|
SaveBookmark(bookmark model.Bookmark) (int64, error)
|
||||||
|
|
||||||
|
// GetBookmarks fetch list of bookmarks based on submitted indices.
|
||||||
GetBookmarks(withContent bool, 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)
|
DeleteBookmarks(indices ...string) ([]int, []int, error)
|
||||||
|
|
||||||
|
// SearchBookmarks search bookmarks by the keyword or tags.
|
||||||
SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error)
|
SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error)
|
||||||
|
|
||||||
|
// UpdateBookmarks updates the saved bookmark in database.
|
||||||
UpdateBookmarks(bookmarks []model.Bookmark) error
|
UpdateBookmarks(bookmarks []model.Bookmark) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,10 +10,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SQLiteDatabase is implementation of Database interface for connecting to SQLite3 database.
|
||||||
type SQLiteDatabase struct {
|
type SQLiteDatabase struct {
|
||||||
sqlx.DB
|
sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenSQLiteDatabase creates and open connection to new SQLite3 database.
|
||||||
func OpenSQLiteDatabase() (*SQLiteDatabase, error) {
|
func OpenSQLiteDatabase() (*SQLiteDatabase, error) {
|
||||||
// Open database and start transaction
|
// Open database and start transaction
|
||||||
var err error
|
var err error
|
||||||
|
@ -73,6 +75,7 @@ func OpenSQLiteDatabase() (*SQLiteDatabase, error) {
|
||||||
return &SQLiteDatabase{*db}, err
|
return &SQLiteDatabase{*db}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveBookmark saves new bookmark to database. Returns new ID and error if any happened.
|
||||||
func (db *SQLiteDatabase) SaveBookmark(bookmark model.Bookmark) (bookmarkID int64, err error) {
|
func (db *SQLiteDatabase) SaveBookmark(bookmark model.Bookmark) (bookmarkID int64, err error) {
|
||||||
// Check URL and title
|
// Check URL and title
|
||||||
if bookmark.URL == "" {
|
if bookmark.URL == "" {
|
||||||
|
@ -156,6 +159,7 @@ func (db *SQLiteDatabase) SaveBookmark(bookmark model.Bookmark) (bookmarkID int6
|
||||||
return bookmarkID, err
|
return bookmarkID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetBookmarks fetch list of bookmarks based on submitted indices.
|
||||||
func (db *SQLiteDatabase) GetBookmarks(withContent bool, 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{}
|
||||||
|
@ -248,6 +252,7 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m
|
||||||
return bookmarks, nil
|
return bookmarks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteBookmarks removes all record with matching indices from database.
|
||||||
func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newIndices []int, err error) {
|
func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newIndices []int, err error) {
|
||||||
// Convert list of index to int
|
// Convert list of index to int
|
||||||
listIndex := []int{}
|
listIndex := []int{}
|
||||||
|
@ -375,6 +380,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd
|
||||||
return oldIndices, newIndices, err
|
return oldIndices, newIndices, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchBookmarks search bookmarks by the keyword or tags.
|
||||||
func (db *SQLiteDatabase) SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error) {
|
func (db *SQLiteDatabase) SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error) {
|
||||||
// Create initial variable
|
// Create initial variable
|
||||||
keyword = strings.TrimSpace(keyword)
|
keyword = strings.TrimSpace(keyword)
|
||||||
|
@ -440,6 +446,7 @@ func (db *SQLiteDatabase) SearchBookmarks(keyword string, tags ...string) ([]mod
|
||||||
return bookmarks, nil
|
return bookmarks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateBookmarks updates the saved bookmark in database.
|
||||||
func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error) {
|
func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error) {
|
||||||
// Prepare transaction
|
// Prepare transaction
|
||||||
tx, err := db.Beginx()
|
tx, err := db.Beginx()
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
// Tag is tag for the bookmark
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
ID int64 `db:"id" json:"id"`
|
ID int64 `db:"id" json:"id"`
|
||||||
Name string `db:"name" json:"name"`
|
Name string `db:"name" json:"name"`
|
||||||
Deleted bool `json:"-"`
|
Deleted bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bookmark is record of a specified URL
|
||||||
type Bookmark struct {
|
type Bookmark struct {
|
||||||
ID int64 `db:"id" json:"id"`
|
ID int64 `db:"id" json:"id"`
|
||||||
URL string `db:"url" json:"url"`
|
URL string `db:"url" json:"url"`
|
||||||
|
|
Loading…
Reference in a new issue