Skip to content

Commit fde1121

Browse files
authored
refactor: consolidate timestamp parsing into shared utility
2 parents be3a67e + dde0c39 commit fde1121

File tree

4 files changed

+16
-33
lines changed

4 files changed

+16
-33
lines changed

internal/collections/models.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/maniac-en/req/internal/crud"
77
"github.com/maniac-en/req/internal/database"
8-
"github.com/maniac-en/req/internal/log"
98
)
109

1110
type CollectionEntity struct {
@@ -21,21 +20,11 @@ func (c CollectionEntity) GetName() string {
2120
}
2221

2322
func (c CollectionEntity) GetCreatedAt() time.Time {
24-
parsed, err := time.Parse(time.RFC3339, c.CreatedAt)
25-
if err != nil {
26-
log.Debug("failed to parse created_at timestamp", "timestamp", c.CreatedAt, "error", err)
27-
return time.Time{}
28-
}
29-
return parsed
23+
return crud.ParseTimestamp(c.CreatedAt)
3024
}
3125

3226
func (c CollectionEntity) GetUpdatedAt() time.Time {
33-
parsed, err := time.Parse(time.RFC3339, c.UpdatedAt)
34-
if err != nil {
35-
log.Debug("failed to parse updated_at timestamp", "timestamp", c.UpdatedAt, "error", err)
36-
return time.Time{}
37-
}
38-
return parsed
27+
return crud.ParseTimestamp(c.UpdatedAt)
3928
}
4029

4130
type CollectionsManager struct {

internal/crud/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crud
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"github.com/maniac-en/req/internal/log"
89
)
@@ -27,3 +28,13 @@ func ValidateID(id int64) error {
2728
}
2829
return nil
2930
}
31+
32+
// ParseTimestamp safely parses RFC3339 timestamp strings from database
33+
func ParseTimestamp(timestamp string) time.Time {
34+
parsed, err := time.Parse(time.RFC3339, timestamp)
35+
if err != nil {
36+
log.Debug("failed to parse timestamp", "timestamp", timestamp, "error", err)
37+
return time.Time{}
38+
}
39+
return parsed
40+
}

internal/endpoints/models.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/maniac-en/req/internal/crud"
77
"github.com/maniac-en/req/internal/database"
8-
"github.com/maniac-en/req/internal/log"
98
)
109

1110
type EndpointEntity struct {
@@ -21,21 +20,11 @@ func (c EndpointEntity) GetName() string {
2120
}
2221

2322
func (c EndpointEntity) GetCreatedAt() time.Time {
24-
parsed, err := time.Parse(time.RFC3339, c.CreatedAt)
25-
if err != nil {
26-
log.Debug("failed to parse created_at timestamp", "timestamp", c.CreatedAt, "error", err)
27-
return time.Time{}
28-
}
29-
return parsed
23+
return crud.ParseTimestamp(c.CreatedAt)
3024
}
3125

3226
func (c EndpointEntity) GetUpdatedAt() time.Time {
33-
parsed, err := time.Parse(time.RFC3339, c.UpdatedAt)
34-
if err != nil {
35-
log.Debug("failed to parse updated_at timestamp", "timestamp", c.UpdatedAt, "error", err)
36-
return time.Time{}
37-
}
38-
return parsed
27+
return crud.ParseTimestamp(c.UpdatedAt)
3928
}
4029

4130
type EndpointsManager struct {

internal/history/models.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/maniac-en/req/internal/crud"
88
"github.com/maniac-en/req/internal/database"
9-
"github.com/maniac-en/req/internal/log"
109
)
1110

1211
type HistoryManager struct {
@@ -26,12 +25,7 @@ func (h HistoryEntity) GetName() string {
2625
}
2726

2827
func (h HistoryEntity) GetCreatedAt() time.Time {
29-
t, err := time.Parse(time.RFC3339, h.ExecutedAt)
30-
if err != nil {
31-
log.Error("failed to parse executed_at timestamp", "executed_at", h.ExecutedAt, "error", err)
32-
return time.Time{}
33-
}
34-
return t
28+
return crud.ParseTimestamp(h.ExecutedAt)
3529
}
3630

3731
// GetUpdatedAt returns creation time since history entries are immutable

0 commit comments

Comments
 (0)