forked from kevinschoon/pomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
152 lines (141 loc) · 3.17 KB
/
store.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"os"
"strings"
"time"
)
// 2018-01-16 19:05:21.752851759+08:00
const datetimeFmt = "2006-01-02 15:04:05.999999999-07:00"
type Store struct {
db *sql.DB
}
func NewStore(path string) (*Store, error) {
os.Mkdir(path, 0755)
db, err := sql.Open("sqlite3", path+"/pomo.db")
if err != nil {
return nil, err
}
return &Store{db: db}, nil
}
func (s Store) CreateTask(task Task) (int, error) {
var taskID int
tx, err := s.db.Begin()
if err != nil {
return -1, err
}
_, err = tx.Exec(
"INSERT INTO task (message,pomodoros,duration,tags) VALUES ($1,$2,$3,$4)",
task.Message, task.NPomodoros, task.Duration.String(), strings.Join(task.Tags, ","))
if err != nil {
tx.Rollback()
return -1, err
}
err = tx.QueryRow("SELECT last_insert_rowid() FROM task").Scan(&taskID)
if err != nil {
tx.Rollback()
return -1, err
}
return taskID, tx.Commit()
}
func (s Store) CreatePomodoro(taskID int, pomodoro Pomodoro) error {
_, err := s.db.Exec(
`INSERT INTO pomodoro (task_id, start, end) VALUES ($1, $2, $3)`,
taskID,
pomodoro.Start,
pomodoro.End,
)
return err
}
func (s Store) ReadTasks() ([]*Task, error) {
rows, err := s.db.Query(`SELECT rowid,message,pomodoros,duration,tags FROM task`)
if err != nil {
return nil, err
}
tasks := []*Task{}
for rows.Next() {
var (
tags string
strDuration string
)
task := &Task{Pomodoros: []*Pomodoro{}}
err = rows.Scan(&task.ID, &task.Message, &task.NPomodoros, &strDuration, &tags)
if err != nil {
return nil, err
}
duration, _ := time.ParseDuration(strDuration)
task.Duration = duration
if tags != "" {
task.Tags = strings.Split(tags, ",")
}
pomodoros, err := s.ReadPomodoros(task.ID)
if err != nil {
return nil, err
}
for _, pomodoro := range pomodoros {
task.Pomodoros = append(task.Pomodoros, pomodoro)
}
tasks = append(tasks, task)
}
return tasks, nil
}
func (s Store) ReadPomodoros(taskID int) ([]*Pomodoro, error) {
rows, err := s.db.Query(`SELECT start,end FROM pomodoro WHERE task_id = $1`, &taskID)
if err != nil {
return nil, err
}
pomodoros := []*Pomodoro{}
for rows.Next() {
var (
startStr string
endStr string
)
pomodoro := &Pomodoro{}
err = rows.Scan(&startStr, &endStr)
if err != nil {
return nil, err
}
start, _ := time.Parse(datetimeFmt, startStr)
end, _ := time.Parse(datetimeFmt, endStr)
pomodoro.Start = start
pomodoro.End = end
pomodoros = append(pomodoros, pomodoro)
}
return pomodoros, nil
}
func (s Store) DeleteTask(taskID int) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM task WHERE rowid = $1", &taskID)
if err != nil {
tx.Rollback()
return err
}
_, err = tx.Exec("DELETE FROM pomodoro WHERE task_id = $1", &taskID)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
func (s Store) Close() error { return s.db.Close() }
func initDB(db *Store) error {
stmt := `
CREATE TABLE task (
message TEXT,
pomodoros INTEGER,
duration TEXT,
tags TEXT
);
CREATE TABLE pomodoro (
task_id INTEGER,
start DATETTIME,
end DATETTIME
);
`
_, err := db.db.Exec(stmt)
return err
}