Creating a database table for items for sale

This commit is contained in:
2024-08-06 21:35:28 +02:00
parent 90918f25ae
commit ab4fda918a
5 changed files with 42 additions and 0 deletions

View File

@ -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
}