From 4d42a08a166ddd71037c546c78b035b96890f2fa Mon Sep 17 00:00:00 2001 From: Joop Kiefte Date: Mon, 13 Dec 2021 22:29:14 +0000 Subject: Spread command over seperate files --- tags.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tags.go (limited to 'tags.go') diff --git a/tags.go b/tags.go new file mode 100644 index 0000000..03b8cbb --- /dev/null +++ b/tags.go @@ -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 +} -- cgit v1.2.3-70-g09d2