26 lines
528 B
Go
26 lines
528 B
Go
package database
|
|
|
|
import (
|
|
"Portifolio/internal/model"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func GetCompanyByID(db *sql.DB, id int) (*model.Company, error) {
|
|
var c model.Company
|
|
err := db.QueryRow(
|
|
`SELECT id, name, shares_outstanding, price, currency_id FROM companies WHERE id = ?`,
|
|
id,
|
|
).Scan(&c.ID, &c.Name, &c.SharesOutstanding, &c.Price, &c.CurrencyID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query company: %w", err)
|
|
}
|
|
return &c, nil
|
|
}
|