package db import ( "context" "github.com/jackc/pgx/v5/pgtype" ) const createMenuItems = `-- name: CreateMenuItems :exec INSERT INTO menu_items( location_id, name, price, category, description, is_available, submitted_by ) values ( $1, $2, $3, $4, $5, $6, $7 ) RETURNING id ` type CreateMenuItemsParams struct { LocationID int32 `json:"location_id"` Name string `json:"name"` Price int32 `json:"price"` Category string `json:"category"` Description string `json:"description"` IsAvailable bool `json:"is_available"` SubmittedBy int32 `json:"submitted_by"` } const getMenuItems = `-- name: GetMenuItems :many SELECT m.id, m.location_id, m.name, m.price, m.category, m.description, m.is_available, m.submitted_by, ROUND(AVG(r.score::float8))::float8 AS avg_score FROM menu_items m LEFT JOIN menu_item_reviews r ON r.menu_item_id = m.id AND r.is_hidden = false WHERE ($1::int4 IS NULL OR m.location_id = $1::int4) GROUP BY m.id ORDER BY m.id ASC ` type GetMenuItemsRow struct { ID int32 `json:"id"` LocationID int32 `json:"location_id"` Name string `json:"name"` Price int32 `json:"price"` Category string `json:"category"` Description string `json:"description"` IsAvailable bool `json:"is_available"` SubmittedBy int32 `json:"submitted_by"` AvgScore *float64 `json:"avg_score"` } func (q *Queries) GetMenuItems(ctx context.Context, locationID pgtype.Int4) ([]GetMenuItemsRow, error) { rows, err := q.db.Query(ctx, getMenuItems, locationID) if err != nil { return nil, err } defer rows.Close() var items []GetMenuItemsRow for rows.Next() { var i GetMenuItemsRow if err := rows.Scan( &i.ID, &i.LocationID, &i.Name, &i.Price, &i.Category, &i.Description, &i.IsAvailable, &i.SubmittedBy, &i.AvgScore, ); err != nil { return nil, err } items = append(items, i) } return items, rows.Err() } func (q *Queries) CreateMenuItems(ctx context.Context, arg CreateMenuItemsParams) (int32, error) { row := q.db.QueryRow(ctx, createMenuItems, arg.LocationID, arg.Name, arg.Price, arg.Category, arg.Description, arg.IsAvailable, arg.SubmittedBy, ) var i int32 err := row.Scan(&i) return i, err }