Skip to content

Commit d098bb7

Browse files
committed
changes:
- new update func for ep for updating just names - changed the create function for eps to allow creation without url - completed ep view
1 parent 2664cda commit d098bb7

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

db/queries/endpoints.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ LIMIT ? OFFSET ?;
3131
SELECT COUNT(*) FROM endpoints
3232
WHERE collection_id = ?;
3333

34+
-- name: UpdateEndpointName :one
35+
UPDATE endpoints
36+
SET
37+
name = ?
38+
WHERE
39+
id = ?
40+
RETURNING *;
41+
3442
-- name: UpdateEndpoint :one
3543
UPDATE endpoints
3644
SET

internal/backend/database/endpoints.sql.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/backend/endpoints/manager.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ func (e *EndpointsManager) CreateEndpoint(ctx context.Context, data EndpointData
135135
log.Warn("endpoint creation failed name validation", "name", data.Name)
136136
return EndpointEntity{}, crud.ErrInvalidInput
137137
}
138-
if data.Method == "" || data.URL == "" {
139-
log.Warn("endpoint creation failed - method and URL required", "method", data.Method, "url", data.URL)
138+
if data.Method == "" {
139+
log.Warn("endpoint creation failed - method required", "method", data.Method)
140140
return EndpointEntity{}, crud.ErrInvalidInput
141141
}
142142

@@ -174,6 +174,37 @@ func (e *EndpointsManager) CreateEndpoint(ctx context.Context, data EndpointData
174174
return EndpointEntity{Endpoint: endpoint}, nil
175175
}
176176

177+
func (e *EndpointsManager) UpdateEndpointName(ctx context.Context, id int64, name string) (EndpointEntity, error) {
178+
if err := crud.ValidateID(id); err != nil {
179+
log.Warn("endpoint update failed ID validation", "id", id)
180+
return EndpointEntity{}, crud.ErrInvalidInput
181+
}
182+
183+
if err := crud.ValidateName(name); err != nil {
184+
log.Warn("endpoint update failed name validation", "name", name)
185+
return EndpointEntity{}, crud.ErrInvalidInput
186+
}
187+
188+
log.Debug("updating endpoint name", "id", id, "name", name)
189+
190+
endpoint, err := e.DB.UpdateEndpointName(ctx, database.UpdateEndpointNameParams{
191+
Name: name,
192+
ID: id,
193+
})
194+
195+
if err != nil {
196+
if err == sql.ErrNoRows {
197+
log.Debug("endpoint not found for update", "id", id)
198+
return EndpointEntity{}, crud.ErrNotFound
199+
}
200+
log.Error("failed to update endpoint", "id", id, "name", name, "error", err)
201+
return EndpointEntity{}, err
202+
}
203+
204+
log.Info("updated endpoint", "id", endpoint.ID, "name", endpoint.Name)
205+
return EndpointEntity{Endpoint: endpoint}, nil
206+
}
207+
177208
func (e *EndpointsManager) UpdateEndpoint(ctx context.Context, id int64, data EndpointData) (EndpointEntity, error) {
178209
if err := crud.ValidateID(id); err != nil {
179210
log.Warn("endpoint update failed ID validation", "id", id)
@@ -183,8 +214,8 @@ func (e *EndpointsManager) UpdateEndpoint(ctx context.Context, id int64, data En
183214
log.Warn("endpoint update failed name validation", "name", data.Name)
184215
return EndpointEntity{}, crud.ErrInvalidInput
185216
}
186-
if data.Method == "" || data.URL == "" {
187-
log.Warn("endpoint update failed - method and URL required", "method", data.Method, "url", data.URL)
217+
if data.Method == "" {
218+
log.Warn("endpoint update failed - method required", "method", data.Method)
188219
return EndpointEntity{}, crud.ErrInvalidInput
189220
}
190221

internal/tui/views/endpoints-view.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/maniac-en/req/internal/backend/endpoints"
1313
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
1414
"github.com/maniac-en/req/internal/tui/keybinds"
15+
"github.com/maniac-en/req/internal/tui/messages"
1516
)
1617

1718
type EndpointsView struct {
@@ -32,7 +33,7 @@ func (e *EndpointsView) Name() string {
3233
}
3334

3435
func (e *EndpointsView) Help() []key.Binding {
35-
return []key.Binding{}
36+
return e.list.Help()
3637
}
3738

3839
func (e *EndpointsView) GetFooterSegment() string {
@@ -48,7 +49,22 @@ func (e *EndpointsView) Update(msg tea.Msg) (ViewInterface, tea.Cmd) {
4849
e.width = msg.Width
4950
e.list, cmd = e.list.Update(msg)
5051
cmds = append(cmds, cmd)
52+
case messages.ItemAdded:
53+
e.manager.CreateEndpoint(context.Background(), endpoints.EndpointData{
54+
CollectionID: e.collection.ID,
55+
Name: msg.Item,
56+
Method: "GET",
57+
})
58+
case messages.ItemEdited:
59+
e.manager.UpdateEndpointName(context.Background(), msg.ItemID, msg.Item)
60+
case messages.DeleteItem:
61+
e.manager.Delete(context.Background(), msg.ItemID)
62+
e.list.RefreshItems()
5163
}
64+
65+
e.list, cmd = e.list.Update(msg)
66+
cmds = append(cmds, cmd)
67+
5268
return e, tea.Batch(cmds...)
5369
}
5470

0 commit comments

Comments
 (0)