From cc26601dc1832ff0a681b04a05ab98ce5d42f2c1 Mon Sep 17 00:00:00 2001 From: Zaptoss Date: Wed, 10 Jul 2024 13:01:32 +0300 Subject: [PATCH 1/2] Fix database query --- internal/data/event_types.go | 2 +- internal/data/pg/event_types.go | 4 ++-- internal/service/handlers/update_event_type.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/data/event_types.go b/internal/data/event_types.go index 777baa0..d48e40f 100644 --- a/internal/data/event_types.go +++ b/internal/data/event_types.go @@ -7,7 +7,7 @@ import ( type EventTypesQ interface { New() EventTypesQ Insert(...models.EventType) error - Update(fields map[string]any) (*models.EventType, error) + Update(fields map[string]any) ([]models.EventType, error) Transaction(func() error) error Select() ([]models.EventType, error) diff --git a/internal/data/pg/event_types.go b/internal/data/pg/event_types.go index 4dc434d..862f768 100644 --- a/internal/data/pg/event_types.go +++ b/internal/data/pg/event_types.go @@ -78,10 +78,10 @@ func (q *eventTypes) Insert(eventTypes ...models.EventType) error { return nil } -func (q *eventTypes) Update(fields map[string]any) (res *models.EventType, err error) { +func (q *eventTypes) Update(fields map[string]any) (res []models.EventType, err error) { stmt := q.updater.SetMap(fields).Suffix("RETURNING *") - if err = q.db.Get(&res, stmt); err != nil { + if err = q.db.Select(&res, stmt); err != nil { return nil, fmt.Errorf("update event type with map %+v: %w", fields, err) } diff --git a/internal/service/handlers/update_event_type.go b/internal/service/handlers/update_event_type.go index 2e15498..3f1a458 100644 --- a/internal/service/handlers/update_event_type.go +++ b/internal/service/handlers/update_event_type.go @@ -47,7 +47,7 @@ func UpdateEventType(w http.ResponseWriter, r *http.Request) { } EventTypes(r).Push(typeModel) - ape.Render(w, newEventTypeResponse(*res)) + ape.Render(w, newEventTypeResponse(res[0])) } func newEventTypeResponse(evType models.EventType) resources.EventTypeResponse { From f33f4e86022865230923f3f8f18eb614a9762a38 Mon Sep 17 00:00:00 2001 From: Zaptoss Date: Wed, 10 Jul 2024 13:10:04 +0300 Subject: [PATCH 2/2] Add validation --- internal/service/handlers/update_event_type.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/service/handlers/update_event_type.go b/internal/service/handlers/update_event_type.go index 3f1a458..664bd2d 100644 --- a/internal/service/handlers/update_event_type.go +++ b/internal/service/handlers/update_event_type.go @@ -46,6 +46,12 @@ func UpdateEventType(w http.ResponseWriter, r *http.Request) { return } + if len(res) == 0 { + Log(r).Error("Count of updated event_types = 0") + ape.RenderErr(w, problems.InternalError()) + return + } + EventTypes(r).Push(typeModel) ape.Render(w, newEventTypeResponse(res[0])) }