-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimesheet.go
67 lines (54 loc) · 1.55 KB
/
timesheet.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
package types
import (
"time"
"github.com/SeerUK/tid/proto"
)
// TimesheetKeyDateFmt is the date formatting string for timesheet keys in the store.
const TimesheetKeyDateFmt = "2006-01-02"
// Timesheet represents a timesheet with entries.
type Timesheet struct {
// The date of the timesheet.
Key string
// An array of entries that belong in this timesheet.
Entries []Entry
}
// NewTimesheet create a new instance of Timesheet.
func NewTimesheet() Timesheet {
return Timesheet{
Key: time.Now().Format(TimesheetKeyDateFmt),
}
}
// FromMessageWithEntries reads a `proto.TrackingTimesheet` message into this Timesheet.
func (t *Timesheet) FromMessageWithEntries(message *proto.TrackingTimesheet, entries []Entry) {
t.Key = message.Key
t.Entries = entries
}
// ToMessage converts this Timesheet to a `proto.TrackingTimesheet`.
func (t *Timesheet) ToMessage() *proto.TrackingTimesheet {
var entryKeys []string
for _, entry := range t.Entries {
entryKeys = append(entryKeys, entry.Hash)
}
return &proto.TrackingTimesheet{
Key: t.Key,
Entries: entryKeys,
}
}
// AppendEntry appends a reference to an entry to the timesheet.
func (t *Timesheet) AppendEntry(entry Entry) {
t.Entries = append(t.Entries, entry)
}
// RemoveEntry removes a reference to an entry from the timesheet.
func (t *Timesheet) RemoveEntry(entry Entry) {
index := -1
for idx, tsEntry := range t.Entries {
if tsEntry.Hash == entry.Hash {
index = idx
break
}
}
if index >= 0 {
// Remove the entry
t.Entries = append(t.Entries[:index], t.Entries[index+1:]...)
}
}