diff --git a/cmd/root.go b/cmd/root.go index e33d457..6030f83 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,6 +8,7 @@ import ( ) var ( + // DB is database that used by this cli DB database.Database rootCmd = &cobra.Command{ diff --git a/database/database.go b/database/database.go index 0c3edf6..2f52df6 100644 --- a/database/database.go +++ b/database/database.go @@ -5,11 +5,21 @@ import ( "github.com/RadhiFadlillah/shiori/model" ) +// Database is interface for manipulating data in database. type Database interface { + // SaveBookmark saves new bookmark to database. SaveBookmark(bookmark model.Bookmark) (int64, error) + + // GetBookmarks fetch list of bookmarks based on submitted indices. GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error) + + // DeleteBookmarks removes all record with matching indices from database. DeleteBookmarks(indices ...string) ([]int, []int, error) + + // SearchBookmarks search bookmarks by the keyword or tags. SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error) + + // UpdateBookmarks updates the saved bookmark in database. UpdateBookmarks(bookmarks []model.Bookmark) error } diff --git a/database/sqlite.go b/database/sqlite.go index bb6a0db..0014d92 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -10,10 +10,12 @@ import ( "strings" ) +// SQLiteDatabase is implementation of Database interface for connecting to SQLite3 database. type SQLiteDatabase struct { sqlx.DB } +// OpenSQLiteDatabase creates and open connection to new SQLite3 database. func OpenSQLiteDatabase() (*SQLiteDatabase, error) { // Open database and start transaction var err error @@ -73,6 +75,7 @@ func OpenSQLiteDatabase() (*SQLiteDatabase, error) { 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) { // Check URL and title if bookmark.URL == "" { @@ -156,6 +159,7 @@ func (db *SQLiteDatabase) SaveBookmark(bookmark model.Bookmark) (bookmarkID int6 return bookmarkID, err } +// GetBookmarks fetch list of bookmarks based on submitted indices. func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error) { // Convert list of index to int listIndex := []int{} @@ -248,6 +252,7 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m return bookmarks, nil } +// DeleteBookmarks removes all record with matching indices from database. func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newIndices []int, err error) { // Convert list of index to int listIndex := []int{} @@ -375,6 +380,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd return oldIndices, newIndices, err } +// SearchBookmarks search bookmarks by the keyword or tags. func (db *SQLiteDatabase) SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error) { // Create initial variable keyword = strings.TrimSpace(keyword) @@ -440,6 +446,7 @@ func (db *SQLiteDatabase) SearchBookmarks(keyword string, tags ...string) ([]mod return bookmarks, nil } +// UpdateBookmarks updates the saved bookmark in database. func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error) { // Prepare transaction tx, err := db.Beginx() diff --git a/model/model.go b/model/model.go index d25951c..4982558 100644 --- a/model/model.go +++ b/model/model.go @@ -1,11 +1,13 @@ package model +// Tag is tag for the bookmark type Tag struct { ID int64 `db:"id" json:"id"` Name string `db:"name" json:"name"` Deleted bool `json:"-"` } +// Bookmark is record of a specified URL type Bookmark struct { ID int64 `db:"id" json:"id"` URL string `db:"url" json:"url"`