Implements history

This commit is contained in:
Mikhail Klementyev 2016-07-24 11:23:24 +03:00
parent 94c6cd800f
commit 6c56bb9e2f
2 changed files with 77 additions and 1 deletions

34
main.go
View File

@ -35,6 +35,7 @@ var (
historyList = kingpin.Command("history", "List history") historyList = kingpin.Command("history", "List history")
historyListItems = historyList.Arg("items", "Amount of items").Int64() historyListItems = historyList.Arg("items", "Amount of items").Int64()
historyListAll = historyList.Flag("all", "Show all items").Bool()
) )
func parseLink(db *sql.DB, oldPage, value string, req *http.Request) (htmlPage string, err error) { func parseLink(db *sql.DB, oldPage, value string, req *http.Request) (htmlPage string, err error) {
@ -90,6 +91,8 @@ func parseLinks(db *sql.DB, body []byte, req *http.Request) (htmlPage string, er
} }
func cmd_url(db *sql.DB, url string) { func cmd_url(db *sql.DB, url string) {
storage.AddHistoryURL(db, url)
client := &http.Client{} client := &http.Client{}
// TODO Full url encoding // TODO Full url encoding
@ -142,6 +145,35 @@ func cmd_link(db *sql.DB, linkID int64) {
cmd_url(db, url) cmd_url(db, url)
} }
func cmd_history(db *sql.DB, argAmount, defaultAmount int64, all bool) {
history, err := storage.GetHistory(db)
if err != nil {
panic(err)
}
var amount int64
if all {
amount = int64(len(history))
} else if argAmount == 0 {
if int64(len(history)) < defaultAmount {
amount = int64(len(history))
} else {
amount = defaultAmount
}
} else {
if amount > int64(len(history)) {
amount = int64(len(history))
} else {
amount = argAmount
}
}
for _, h := range history[int64(len(history))-amount:] {
fmt.Println(h.ID, h.URL)
}
}
func main() { func main() {
db, err := storage.OpenDB("/tmp/wi.db") db, err := storage.OpenDB("/tmp/wi.db")
if err != nil { if err != nil {
@ -155,6 +187,6 @@ func main() {
case "link": case "link":
cmd_link(db, *linkNo) cmd_link(db, *linkNo)
case "history": case "history":
fmt.Println("not implemented") cmd_history(db, *historyListItems, 20, *historyListAll)
} }
} }

View File

@ -21,6 +21,12 @@ func OpenDB(path string) (db *sql.DB, err error) {
_, err = db.Exec("CREATE TABLE IF NOT EXISTS `links` " + _, err = db.Exec("CREATE TABLE IF NOT EXISTS `links` " +
"( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `url` TEXT );") "( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `url` TEXT );")
if err != nil {
return
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS `history` " +
"( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `url` TEXT );")
return return
} }
@ -65,3 +71,41 @@ func GetLinkID(db *sql.DB, url string) (linkID int64, err error) {
return return
} }
func AddHistoryURL(db *sql.DB, url string) (err error) {
stmt, err := db.Prepare("INSERT INTO `history` (`url`) VALUES ($1);")
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(url)
return
}
type HistoryItem struct {
ID int64
URL string
}
func GetHistory(db *sql.DB) (history []HistoryItem, err error) {
rows, err := db.Query("SELECT `id`, `url` FROM `history`;")
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var h HistoryItem
err = rows.Scan(&h.ID, &h.URL)
if err != nil {
return
}
history = append(history, h)
}
return
}