aboutsummaryrefslogtreecommitdiff
path: root/tags.go
blob: 03b8cbb52adbec9f99987fd1aad2f5a30193d7de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
}