Skip to content

Commit

Permalink
Merge pull request #11 from rarimo/fix/events_update
Browse files Browse the repository at this point in the history
Fix database query
  • Loading branch information
Zaptoss authored Jul 10, 2024
2 parents 5ef45f9 + f33f4e8 commit 8a56687
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/data/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/data/pg/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 7 additions & 1 deletion internal/service/handlers/update_event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ 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))
ape.Render(w, newEventTypeResponse(res[0]))
}

func newEventTypeResponse(evType models.EventType) resources.EventTypeResponse {
Expand Down

0 comments on commit 8a56687

Please sign in to comment.