-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
151 lines (126 loc) · 4.64 KB
/
Copy pathmanager.go
File metadata and controls
151 lines (126 loc) · 4.64 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
package collections
import (
"context"
"database/sql"
"github.com/maniac-en/req/internal/backend/crud"
"github.com/maniac-en/req/internal/backend/database"
"github.com/maniac-en/req/internal/log"
)
func NewCollectionsManager(db *database.Queries) *CollectionsManager {
return &CollectionsManager{DB: db}
}
func (c *CollectionsManager) Create(ctx context.Context, name string) (CollectionEntity, error) {
if err := crud.ValidateName(name); err != nil {
log.Warn("collection creation failed validation", "name", name)
return CollectionEntity{}, crud.ErrInvalidInput
}
log.Debug("creating collection", "name", name)
collection, err := c.DB.CreateCollection(ctx, name)
if err != nil {
log.Error("failed to create collection", "name", name, "error", err)
return CollectionEntity{}, err
}
log.Info("created collection", "id", collection.ID, "name", collection.Name)
return CollectionEntity{Collection: collection}, nil
}
func (c *CollectionsManager) Read(ctx context.Context, id int64) (CollectionEntity, error) {
if err := crud.ValidateID(id); err != nil {
log.Warn("collection read failed validation", "id", id)
return CollectionEntity{}, crud.ErrInvalidInput
}
log.Debug("reading collection", "id", id)
collection, err := c.DB.GetCollection(ctx, id)
if err != nil {
if err == sql.ErrNoRows {
log.Debug("collection not found", "id", id)
return CollectionEntity{}, crud.ErrNotFound
}
log.Error("failed to read collection", "id", id, "error", err)
return CollectionEntity{}, err
}
return CollectionEntity{Collection: collection}, nil
}
func (c *CollectionsManager) Update(ctx context.Context, id int64, name string) (CollectionEntity, error) {
if err := crud.ValidateID(id); err != nil {
log.Warn("collection update failed ID validation", "id", id)
return CollectionEntity{}, crud.ErrInvalidInput
}
if err := crud.ValidateName(name); err != nil {
log.Warn("collection update failed name validation", "name", name)
return CollectionEntity{}, crud.ErrInvalidInput
}
log.Debug("updating collection", "id", id, "name", name)
collection, err := c.DB.UpdateCollectionName(ctx, database.UpdateCollectionNameParams{
Name: name,
ID: id,
})
if err != nil {
if err == sql.ErrNoRows {
log.Debug("collection not found for update", "id", id)
return CollectionEntity{}, crud.ErrNotFound
}
log.Error("failed to update collection", "id", id, "name", name, "error", err)
return CollectionEntity{}, err
}
log.Info("updated collection", "id", collection.ID, "name", collection.Name)
return CollectionEntity{Collection: collection}, nil
}
func (c *CollectionsManager) Delete(ctx context.Context, id int64) error {
if err := crud.ValidateID(id); err != nil {
log.Warn("collection delete failed validation", "id", id)
return crud.ErrInvalidInput
}
log.Debug("deleting collection", "id", id)
err := c.DB.DeleteCollection(ctx, id)
if err != nil {
log.Error("failed to delete collection", "id", id, "error", err)
return err
}
log.Info("deleted collection", "id", id)
return nil
}
func (c *CollectionsManager) List(ctx context.Context) ([]CollectionEntity, error) {
collections, err := c.DB.GetCollections(ctx)
if err != nil {
return nil, err
}
entities := make([]CollectionEntity, len(collections))
for i, collection := range collections {
entities[i] = CollectionEntity{Collection: collection}
}
return entities, nil
}
func (c *CollectionsManager) ListPaginated(ctx context.Context, limit, offset int) (*PaginatedCollections, error) {
// Warn about unusual pagination parameters
if limit > 1000 {
log.Warn("large pagination limit requested", "limit", limit)
}
if offset < 0 {
log.Warn("negative pagination offset", "offset", offset)
}
log.Debug("listing paginated collections", "limit", limit, "offset", offset)
total, err := c.DB.CountCollections(ctx)
if err != nil {
log.Error("failed to count collections", "error", err)
return nil, err
}
collections, err := c.DB.GetCollectionsPaginated(ctx, database.GetCollectionsPaginatedParams{
Limit: int64(limit),
Offset: int64(offset),
})
if err != nil {
log.Error("failed to get paginated collections", "limit", limit, "offset", offset, "error", err)
return nil, err
}
entities := make([]CollectionEntity, len(collections))
for i, collection := range collections {
entities[i] = CollectionEntity{Collection: collection}
}
pagination := crud.CalculatePagination(total, limit, offset)
result := &PaginatedCollections{
Collections: entities,
PaginationMetadata: pagination,
}
log.Info("retrieved collections", "count", len(entities), "total", pagination.Total, "page", pagination.CurrentPage, "total_pages", pagination.TotalPages)
return result, nil
}