Skip to content

Commit

Permalink
Return empty array instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
polldo committed Sep 14, 2023
1 parent 7cc9858 commit 28dcf05
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
5 changes: 0 additions & 5 deletions core/cart/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ func HandleShow(db *sqlx.DB) web.Handler {
return err
}

// Return empty slice instead of nil.
if cart.Items == nil {
cart.Items = []Item{}
}

return web.Respond(ctx, w, cart, http.StatusOK)
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/cart/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func FetchItems(ctx context.Context, db sqlx.ExtContext, userID string) ([]Item,
ORDER BY
course_id`

var ci []Item
ci := []Item{}
if err := database.NamedQuerySlice(ctx, db, q, in, &ci); err != nil {
return nil, fmt.Errorf("selecting cart items of user[%s]: %w", userID, err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/course/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func FetchAll(ctx context.Context, db sqlx.ExtContext) ([]Course, error) {
ORDER BY
course_id`

var cs []Course
cs := []Course{}
if err := database.NamedQuerySlice(ctx, db, q, struct{}{}, &cs); err != nil {
return nil, fmt.Errorf("selecting all courses: %w", err)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func FetchByOwner(ctx context.Context, db sqlx.ExtContext, userID string) ([]Cou
ORDER BY
c.course_id`

var cs []Course
cs := []Course{}
if err := database.NamedQuerySlice(ctx, db, q, in, &cs); err != nil {
return nil, fmt.Errorf("selecting all courses: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/video/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func FetchAll(ctx context.Context, db sqlx.ExtContext) ([]Video, error) {
ORDER BY
video_id`

var videos []Video
videos := []Video{}
if err := database.NamedQuerySlice(ctx, db, q, struct{}{}, &videos); err != nil {
return nil, fmt.Errorf("selecting videos: %w", err)
}
Expand All @@ -112,7 +112,7 @@ func FetchAllByCourse(ctx context.Context, db sqlx.ExtContext, courseID string)
ORDER BY
index`

var videos []Video
videos := []Video{}
if err := database.NamedQuerySlice(ctx, db, q, in, &videos); err != nil {
return nil, fmt.Errorf("selecting videos: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ func NamedQuerySlice[T any](ctx context.Context, db sqlx.ExtContext, query strin
}
slice = append(slice, *v)
}
*dest = slice

if slice != nil {
*dest = slice
}

return nil
}
Expand Down

0 comments on commit 28dcf05

Please sign in to comment.