Browse Source

Add favorite to file

Thomas Jost 3 years ago
parent
commit
1af1fcf308
Signed by: Thomas Jost <schnouki@schnouki.net> GPG Key ID: 3F3B990246141762
5 changed files with 103 additions and 3 deletions
  1. 7
    1
      cmd/add.go
  2. 4
    2
      db/stop.go
  3. 24
    0
      fav/common.go
  4. 26
    0
      fav/read.go
  5. 42
    0
      fav/write.go

+ 7
- 1
cmd/add.go View File

@@ -8,6 +8,7 @@ import (
8 8
 	"gopkg.in/dixonwille/wmenu.v4"
9 9
 
10 10
 	"islas/db"
11
+	"islas/fav"
11 12
 )
12 13
 
13 14
 func init() {
@@ -96,7 +97,12 @@ func addFavoriteStep3(conn *db.DB, line db.Line, direction db.Direction) {
96 97
 		stop := opts[0].Value.(db.Stop)
97 98
 		fmt.Printf("\nSelected stop %s on line %s to %s\n", stop.Name, line.Number, direction.Label)
98 99
 
99
-		return nil
100
+		newFav := fav.Favorite{
101
+			LineID:      line.ID,
102
+			DirectionID: direction.ID,
103
+			StopID:      stop.ID,
104
+		}
105
+		return fav.AddToFile(newFav)
100 106
 	})
101 107
 	if err := menu.Run(); err != nil {
102 108
 		log.WithError(err).Fatal("Menu error")

+ 4
- 2
db/stop.go View File

@@ -25,7 +25,8 @@ func (db *DB) GetStops(lineID, directionID int) ([]Stop, error) {
25 25
 		INNER JOIN LIGNE AS l
26 26
 			ON l.LIGNO = la.LIGNO
27 27
 		INNER JOIN LIGNE_SENS AS ls
28
-			ON ls.sens = la.sens
28
+			ON ls.SENS = la.SENS
29
+			AND ls.LIGNO = l.LIGNO
29 30
 		WHERE l.LIGNO = ? AND ls.SENS = ?
30 31
 		ORDER BY la.ALIORDRE`,
31 32
 		lineID, directionID)
@@ -44,7 +45,8 @@ func (db *DB) GetStop(lineID, directionID, stopID int) (Stop, error) {
44 45
 		INNER JOIN LIGNE AS l
45 46
 			ON l.LIGNO = la.LIGNO
46 47
 		INNER JOIN LIGNE_SENS AS ls
47
-			ON ls.sens = la.sens
48
+			ON ls.SENS = la.SENS
49
+			AND ls.LIGNO = l.LIGNO
48 50
 		WHERE l.LIGNO = ? AND ls.SENS = ? AND pa.PTANO = ?`,
49 51
 		lineID, directionID, stopID)
50 52
 	return stop, err

+ 24
- 0
fav/common.go View File

@@ -0,0 +1,24 @@
1
+package fav
2
+
3
+import (
4
+	"github.com/casimir/xdg-go"
5
+
6
+	"islas/db"
7
+)
8
+
9
+// GetPath returns the path to the favorites file.
10
+func GetPath() string {
11
+	return xdg.ConfigPath("favorites.yml")
12
+}
13
+
14
+// Favorite stores everything about a bus/tramway stop.
15
+type Favorite struct {
16
+	StopID      int
17
+	LineID      int
18
+	DirectionID int
19
+}
20
+
21
+// AsStop returns the DB stop that corresponds to this favorite.
22
+func (fav *Favorite) AsStop(conn *db.DB) (db.Stop, error) {
23
+	return conn.GetStop(fav.LineID, fav.DirectionID, fav.StopID)
24
+}

+ 26
- 0
fav/read.go View File

@@ -0,0 +1,26 @@
1
+package fav
2
+
3
+import (
4
+	"os"
5
+
6
+	"gopkg.in/yaml.v2"
7
+)
8
+
9
+// FromFile reads a list of favorites from a YAML file.
10
+func FromFile() ([]Favorite, error) {
11
+	favs := []Favorite{}
12
+
13
+	path := GetPath()
14
+	favFile, err := os.Open(path)
15
+	if err != nil {
16
+		if os.IsNotExist(err) {
17
+			return favs, nil
18
+		}
19
+		return nil, err
20
+	}
21
+	defer favFile.Close()
22
+
23
+	dec := yaml.NewDecoder(favFile)
24
+	err = dec.Decode(&favs)
25
+	return favs, err
26
+}

+ 42
- 0
fav/write.go View File

@@ -0,0 +1,42 @@
1
+package fav
2
+
3
+import (
4
+	"os"
5
+	"path"
6
+
7
+	log "github.com/sirupsen/logrus"
8
+	"gopkg.in/yaml.v2"
9
+)
10
+
11
+// AddToFile adds the given favorite to the favorites YAML file.
12
+func AddToFile(newFav Favorite) error {
13
+	// Read the current ones
14
+	favs, err := FromFile()
15
+	if err != nil {
16
+		return err
17
+	}
18
+
19
+	// Is it already in the list?
20
+	for _, fav := range favs {
21
+		if fav.StopID == newFav.StopID && fav.LineID == newFav.LineID && fav.DirectionID == newFav.DirectionID {
22
+			return nil
23
+		}
24
+	}
25
+
26
+	// Nope: add it!
27
+	favs = append(favs, newFav)
28
+
29
+	// And save the file…
30
+	favPath := GetPath()
31
+	if err := os.MkdirAll(path.Dir(favPath), 0700); err != nil {
32
+		log.WithError(err).Fatal("Cannot create the favorites file parent directory")
33
+	}
34
+	favFile, err := os.Create(favPath)
35
+	if err != nil {
36
+		return err
37
+	}
38
+	defer favFile.Close()
39
+
40
+	enc := yaml.NewEncoder(favFile)
41
+	return enc.Encode(favs)
42
+}

Loading…
Cancel
Save