Creating a database table for items for sale
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
@ -32,3 +33,19 @@ type Widget struct {
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
func (m *DBModel) GetWidget(id int) (Widget, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var widget Widget
|
||||
|
||||
query := `SELECT id, name FROM widgets WHERE id = ?`
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, query, id)
|
||||
err := row.Scan(&widget.ID, &widget.Name)
|
||||
if err != nil {
|
||||
return widget, err
|
||||
}
|
||||
return widget, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user