-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt.go
123 lines (109 loc) · 2.79 KB
/
ttt.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
// Copyright (c) 2021, David Url
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ttt
import (
"database/sql"
"fmt"
"strings"
"time"
"errors"
_ "github.com/mattn/go-sqlite3"
)
type TimeTrackingDb struct {
db *sql.DB
filename string
config timeTrackingConfig
}
type timeTrackingConfig struct {
breakThreshold time.Duration
breakDeduction time.Duration
workingHours [7]time.Duration
holidays string
}
func LoadDb(filename string) (*TimeTrackingDb, error) {
db, err := sql.Open("sqlite3", filename)
if err != nil {
return nil, err
}
err = applySchema(db)
if err != nil {
return nil, err
}
t := TimeTrackingDb{db: db, filename: filename, config: timeTrackingConfig{}}
t.loadConfig()
return &t, nil
}
func CreateDb(filename string) (*TimeTrackingDb, error) {
db, err := sql.Open("sqlite3", filename)
if err != nil {
return nil, err
}
err = initSchema(db)
if err != nil {
return nil, err
}
err = applySchema(db)
if err != nil {
return nil, err
}
t := TimeTrackingDb{db: db, filename: filename, config: timeTrackingConfig{}}
err = t.loadConfig()
return &t, err
}
func (t *TimeTrackingDb) loadConfig() error {
rows, err := t.db.Query("SELECT c.property, c.value FROM config AS c;")
if err != nil {
return err
}
defer rows.Close()
var errs []error
for rows.Next() {
var property string
var value string
err = rows.Scan(&property, &value)
if err != nil {
errs = append(errs, err)
}
switch property {
case "break_deduction":
t.config.breakDeduction, errs = parseDuration(value, errs)
case "break_threshold":
t.config.breakThreshold, errs = parseDuration(value, errs)
case "monday_hours":
t.config.workingHours[time.Monday], errs = parseDuration(value, errs)
case "tuesday_hours":
t.config.workingHours[time.Tuesday], errs = parseDuration(value, errs)
case "wednesday_hours":
t.config.workingHours[time.Wednesday], errs = parseDuration(value, errs)
case "thursday_hours":
t.config.workingHours[time.Thursday], errs = parseDuration(value, errs)
case "friday_hours":
t.config.workingHours[time.Friday], errs = parseDuration(value, errs)
case "saturdayhours":
t.config.workingHours[time.Saturday], errs = parseDuration(value, errs)
case "sunday_hours":
t.config.workingHours[time.Sunday], errs = parseDuration(value, errs)
case "holidays":
t.config.holidays = value
}
}
if len(errs) > 0 {
return errors.New(fmt.Sprint(errs))
}
return nil
}
func parseDuration(value string, errs []error) (time.Duration, []error) {
value = strings.ReplaceAll(value, " ", "")
d, err := time.ParseDuration(value)
if err != nil {
errs = append(errs, err)
}
return d, errs
}
func (t *TimeTrackingDb) Close() error {
if t.db == nil {
return nil
}
return t.db.Close()
}