Refactor indices->indexList conversion

This commit is contained in:
Peter Etelej 2018-03-12 14:01:14 +03:00
parent 6bab302785
commit 10d76acdeb
No known key found for this signature in database
GPG key ID: 5F1F0595EC237B87
2 changed files with 46 additions and 66 deletions

View file

@ -112,12 +112,7 @@ func addAccount(username, password string) error {
return fmt.Errorf("Password must be at least 8 characters") return fmt.Errorf("Password must be at least 8 characters")
} }
err := DB.CreateAccount(username, password) return DB.CreateAccount(username, password)
if err != nil {
return err
}
return nil
} }
func printAccounts(keyword string, wr io.Writer) error { func printAccounts(keyword string, wr io.Writer) error {

View file

@ -2,6 +2,7 @@ package database
import ( import (
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -168,34 +169,10 @@ 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(withContent bool, 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")
for _, strIndex := range indices { listIndex, err := parseIndexList(indices)
if strings.Contains(strIndex, "-") { if err != nil {
parts := strings.Split(strIndex, "-") return nil, err
if len(parts) != 2 {
return nil, errInvalidIndex
}
minIndex, errMin := strconv.Atoi(parts[0])
maxIndex, errMax := strconv.Atoi(parts[1])
if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex {
return nil, errInvalidIndex
}
for i := minIndex; i <= maxIndex; i++ {
listIndex = append(listIndex, i)
}
} else {
index, err := strconv.Atoi(strIndex)
if err != nil || index < 1 {
return nil, errInvalidIndex
}
listIndex = append(listIndex, index)
}
} }
// Prepare where clause // Prepare where clause
@ -220,7 +197,7 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m
FROM bookmark` + whereClause FROM bookmark` + whereClause
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 {
return nil, err return nil, err
} }
@ -263,34 +240,10 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m
// DeleteBookmarks removes all record with matching indices from database. // DeleteBookmarks removes all record with matching indices from database.
func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (err error) { func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (err error) {
// Convert list of index to int
listIndex := []int{}
errInvalidIndex := fmt.Errorf("Index is not valid")
for _, strIndex := range indices { listIndex, err := parseIndexList(indices)
if strings.Contains(strIndex, "-") { if err != nil {
parts := strings.Split(strIndex, "-") return err
if len(parts) != 2 {
return errInvalidIndex
}
minIndex, errMin := strconv.Atoi(parts[0])
maxIndex, errMax := strconv.Atoi(parts[1])
if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex {
return errInvalidIndex
}
for i := minIndex; i <= maxIndex; i++ {
listIndex = append(listIndex, i)
}
} else {
index, err := strconv.Atoi(strIndex)
if err != nil || index < 1 {
return errInvalidIndex
}
listIndex = append(listIndex, index)
}
} }
// Create args and where clause // Create args and where clause
@ -311,7 +264,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (err error) {
// Begin transaction // Begin transaction
tx, err := db.Beginx() tx, err := db.Beginx()
if err != nil { if err != nil {
return errInvalidIndex return ErrInvalidIndex
} }
// Make sure to rollback if panic ever happened // Make sure to rollback if panic ever happened
@ -517,11 +470,8 @@ func (db *SQLiteDatabase) CreateAccount(username, password string) (err error) {
_, err = db.Exec(`INSERT INTO account _, err = db.Exec(`INSERT INTO account
(username, password) VALUES (?, ?)`, (username, password) VALUES (?, ?)`,
username, hashedPassword) username, hashedPassword)
if err != nil {
return err
}
return nil return err
} }
// GetAccounts fetch list of accounts in database // GetAccounts fetch list of accounts in database
@ -581,3 +531,38 @@ func (db *SQLiteDatabase) GetTags() ([]model.Tag, error) {
return tags, nil return tags, nil
} }
// ErrInvalidIndex is returned is an index is not valid
var ErrInvalidIndex = errors.New("Index is not valid")
// parseIndexList converts a list of indices to their integer values
func parseIndexList(indices []string) ([]int, error) {
var listIndex []int
for _, strIndex := range indices {
if !strings.Contains(strIndex, "-") {
index, err := strconv.Atoi(strIndex)
if err != nil || index < 1 {
return nil, ErrInvalidIndex
}
listIndex = append(listIndex, index)
continue
}
parts := strings.Split(strIndex, "-")
if len(parts) != 2 {
return nil, ErrInvalidIndex
}
minIndex, errMin := strconv.Atoi(parts[0])
maxIndex, errMax := strconv.Atoi(parts[1])
if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex {
return nil, ErrInvalidIndex
}
for i := minIndex; i <= maxIndex; i++ {
listIndex = append(listIndex, i)
}
}
return listIndex, nil
}