diff options
author | Joop Kiefte <ikojba@gmail.com> | 2021-12-13 22:29:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-13 22:29:14 +0000 |
commit | 4d42a08a166ddd71037c546c78b035b96890f2fa (patch) | |
tree | 14cb8016f8628aa2b927206b10c36d0f66448f28 /tags.go | |
parent | afdcbec2147b9bfc54334955dab825cba625423b (diff) |
Spread command over seperate files
Diffstat (limited to 'tags.go')
-rw-r--r-- | tags.go | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -0,0 +1,56 @@ +package main + +import ( + "database/sql" + "fmt" + "strings" +) + +func ConfigureFollowedTags() { + db := GetDatabase() + fmt.Println("At the moment you follow the following tags:") + // Get the tags that the user is following + tags := GetFollowedTags(db) + // Show the tags + for _, tag := range tags { + fmt.Print(tag, " ") + } + fmt.Println() + fmt.Println("Enter the tags you want to follow, separated by spaces\nTo remove tags, prefix them with a minus sign: ") + newtags := Readline() + // Split the tags into an array and insert them into database DB + // using table "followed_tags" + tagArray := strings.Split(newtags, " ") + for _, tag := range tagArray { + tag = strings.Trim(tag, " \n") + if !strings.HasPrefix(tag, "-") { + _, err := db.Exec("INSERT INTO followed_tags(tag) VALUES(?)", tag) + if err != nil { + fmt.Println(err) + } + } else { + _, err := db.Exec("DELETE FROM followed_tags WHERE tag=?", tag[1:]) + if err != nil { + fmt.Println(err) + } + } + } +} + +func GetFollowedTags(db *sql.DB) []string { + // Get the tags from the database + rows, err := db.Query("SELECT tag FROM followed_tags") + if err != nil { + fmt.Println(err) + } + var tags []string + for rows.Next() { + var tag string + err = rows.Scan(&tag) + if err != nil { + fmt.Println(err) + } + tags = append(tags, tag) + } + return tags +} |