Implements follow link from history

This commit is contained in:
Mikhail Klementyev 2016-07-25 18:41:12 +03:00
parent 159907723b
commit fd1e14128e
3 changed files with 27 additions and 5 deletions

View File

@ -126,8 +126,17 @@ func Get(db *sql.DB, url string) {
fmt.Println(text)
}
func Link(db *sql.DB, linkID int64) {
url, err := storage.GetLink(db, linkID)
func Link(db *sql.DB, linkID int64, fromHistory bool) {
var url string
var err error
if fromHistory {
url, err = storage.GetHistoryUrl(db, linkID)
} else {
url, err = storage.GetLink(db, linkID)
}
if err != nil {
panic(err)
}

View File

@ -19,8 +19,9 @@ var (
get = kingpin.Command("get", "Get url")
getUrl = get.Arg("url", "Url").Required().String()
link = kingpin.Command("link", "Get link")
linkNo = link.Arg("no", "Number").Required().Int64()
link = kingpin.Command("link", "Get link")
linkNo = link.Arg("no", "Number").Required().Int64()
linkFromHistory = link.Flag("history", "Item from history").Bool()
historyList = kingpin.Command("history", "List history")
historyListItems = historyList.Arg("items", "Amount of items").Int64()
@ -38,7 +39,7 @@ func main() {
case "get":
commands.Get(db, *getUrl)
case "link":
commands.Link(db, *linkNo)
commands.Link(db, *linkNo, *linkFromHistory)
case "history":
commands.History(db, *historyListItems, 20, *historyListAll)
}

View File

@ -109,3 +109,15 @@ func GetHistory(db *sql.DB) (history []HistoryItem, err error) {
return
}
func GetHistoryUrl(db *sql.DB, historyID int64) (url string, err error) {
stmt, err := db.Prepare("SELECT `url` FROM `history` WHERE id=$1;")
if err != nil {
return
}
defer stmt.Close()
err = stmt.QueryRow(historyID).Scan(&url)
return
}