Repository navigation
Expand file tree
/
Copy pathdb.go
More file actions
219 lines (188 loc) · 5.25 KB
/
Copy pathdb.go
File metadata and controls
219 lines (188 loc) · 5.25 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package things3
import (
"context"
"github.com/moond4rk/things3/internal/database"
)
// db provides read-only access to the Things 3 database.
type db struct {
inner *database.DB
}
// newDB creates a new Things 3 database connection.
func newDB(opts ...database.Option) (*db, error) {
inner, err := database.Open(opts...)
if err != nil {
return nil, err
}
return &db{inner: inner}, nil
}
// Close closes the database connection.
func (d *db) Close() error {
return d.inner.Close()
}
// Filepath returns the path to the Things database file.
func (d *db) Filepath() string {
return d.inner.Filepath()
}
// Token returns the Things URL scheme authentication token.
func (d *db) Token(ctx context.Context) (string, error) {
return d.inner.AuthToken(ctx)
}
// =============================================================================
// Row-to-Model Conversion
// =============================================================================
// convertTaskRowToTodo converts an internal TaskRow to a public Todo.
func convertTaskRowToTodo(r *database.TaskRow) Todo {
todo := Todo{
UUID: r.UUID,
Title: r.Title,
Notes: r.Notes,
StartDate: r.StartDate,
Deadline: r.Deadline,
Reminder: r.ReminderTime,
CreatedAt: r.Created,
ModifiedAt: r.Modified,
Trashed: r.Trashed,
Evening: r.Evening,
Repeating: r.Repeating,
}
// Convert status string to Status enum
todo.Status = parseStatusFromString(r.Status)
// Convert start string to StartBucket enum
todo.Start = parseStartBucketFromString(r.Start)
// Convert nullable relationship fields
todo.AreaUUID = ptrToString(r.AreaUUID)
todo.AreaTitle = ptrToString(r.AreaTitle)
todo.ProjectUUID = ptrToString(r.ProjectUUID)
todo.ProjectTitle = ptrToString(r.ProjectTitle)
todo.HeadingUUID = ptrToString(r.HeadingUUID)
todo.HeadingTitle = ptrToString(r.HeadingTitle)
// Split stopDate into CompletedAt or CanceledAt based on Status
if r.StopDate != nil {
switch todo.Status {
case StatusCompleted:
todo.CompletedAt = r.StopDate
case StatusCanceled:
todo.CanceledAt = r.StopDate
}
}
// Mark if task has tags (actual tag names loaded separately)
if r.HasTags {
todo.Tags = []string{}
}
return todo
}
// convertTaskRowToProject converts an internal TaskRow to a public Project.
func convertTaskRowToProject(r *database.TaskRow) Project {
project := Project{
UUID: r.UUID,
Title: r.Title,
Notes: r.Notes,
StartDate: r.StartDate,
Deadline: r.Deadline,
Reminder: r.ReminderTime,
CreatedAt: r.Created,
ModifiedAt: r.Modified,
Trashed: r.Trashed,
Repeating: r.Repeating,
}
project.Status = parseStatusFromString(r.Status)
project.Start = parseStartBucketFromString(r.Start)
project.AreaUUID = ptrToString(r.AreaUUID)
project.AreaTitle = ptrToString(r.AreaTitle)
if r.StopDate != nil {
switch project.Status {
case StatusCompleted:
project.CompletedAt = r.StopDate
case StatusCanceled:
project.CanceledAt = r.StopDate
}
}
if r.HasTags {
project.Tags = []string{}
}
return project
}
// convertTaskRowToHeading converts an internal TaskRow to a public Heading.
func convertTaskRowToHeading(r *database.TaskRow) Heading {
return Heading{
UUID: r.UUID,
Title: r.Title,
ProjectUUID: ptrToString(r.ProjectUUID),
ProjectTitle: ptrToString(r.ProjectTitle),
}
}
// convertAreaRow converts an internal AreaRow to a public Area.
func convertAreaRow(r database.AreaRow) Area {
area := Area{
UUID: r.UUID,
Title: r.Title,
}
if r.HasTags {
area.Tags = []string{}
}
return area
}
// convertTagRow converts an internal TagRow to a public Tag.
func convertTagRow(r database.TagRow) Tag {
return Tag{
UUID: r.UUID,
Title: r.Title,
Shortcut: r.Shortcut,
}
}
// convertChecklistItemRows converts internal ChecklistItemRows to public ChecklistItems.
func convertChecklistItemRows(rows []database.ChecklistItemRow) []ChecklistItem {
items := make([]ChecklistItem, len(rows))
for i, r := range rows {
item := ChecklistItem{
UUID: r.UUID,
Title: r.Title,
Status: parseStatusFromString(r.Status),
CreatedAt: r.Created,
ModifiedAt: r.Modified,
}
if r.StopDate != nil {
switch item.Status {
case StatusCompleted:
item.CompletedAt = r.StopDate
case StatusCanceled:
item.CanceledAt = r.StopDate
}
}
items[i] = item
}
return items
}
// =============================================================================
// Conversion Helpers
// =============================================================================
// ptrToString converts a nullable string pointer to a string value.
// Returns empty string if the pointer is nil.
func ptrToString(p *string) string {
if p == nil {
return ""
}
return *p
}
// parseStatusFromString converts a database status string to a Status enum.
func parseStatusFromString(s string) Status {
switch s {
case statusStringCompleted:
return StatusCompleted
case statusStringCanceled:
return StatusCanceled
default:
return StatusIncomplete
}
}
// parseStartBucketFromString converts a database start string to a StartBucket enum.
func parseStartBucketFromString(s string) StartBucket {
switch s {
case "Anytime":
return StartAnytime
case "Someday":
return StartSomeday
default:
return StartInbox
}
}