moving handlers into service. adding website

This commit is contained in:
samantha42
2026-04-29 08:38:41 +02:00
parent 57ae3cfb06
commit 22c6a22373
18 changed files with 752 additions and 586 deletions

View File

@@ -8,13 +8,14 @@ import (
_ "github.com/mattn/go-sqlite3"
)
func GetCompanyBySymbol(db *sql.DB, symbol string) (*model.Company, error) {
func getCompany(db *sql.DB, where string, arg any) (*model.Company, error) {
var c model.Company
err := db.QueryRow(
`SELECT id, symbol, shares_outstanding, price, currency_id FROM companies WHERE symbol = ?`,
symbol,
).Scan(&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID)
err := db.QueryRow(`
SELECT c.id, c.symbol, c.shares_outstanding, c.price, cur.id, cur.code
FROM companies c
JOIN currencies cur ON cur.id = c.currency_id
WHERE `+where, arg,
).Scan(&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID, &c.CurrencyCode)
if err == sql.ErrNoRows {
return nil, nil
}
@@ -24,20 +25,12 @@ func GetCompanyBySymbol(db *sql.DB, symbol string) (*model.Company, error) {
return &c, nil
}
func GetCompanyByID(db *sql.DB, id int) (*model.Company, error) {
var c model.Company
err := db.QueryRow(
`SELECT id, symbol, shares_outstanding, price, currency_id FROM companies WHERE id = ?`,
id,
).Scan(&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID)
func GetCompanyBySymbol(db *sql.DB, symbol string) (*model.Company, error) {
return getCompany(db, "c.symbol = ?", symbol)
}
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query company: %w", err)
}
return &c, nil
func GetCompanyByID(db *sql.DB, id int) (*model.Company, error) {
return getCompany(db, "c.id = ?", id)
}
func AddCompany(db *sql.DB, input model.CompanyInput) (int, error) {
@@ -66,7 +59,9 @@ func AddCompany(db *sql.DB, input model.CompanyInput) (int, error) {
func GetAllCompanies(db *sql.DB) ([]model.Company, error) {
rows, err := db.Query(`
SELECT id, symbol, shares_outstanding, price, currency_id FROM companies
SELECT c.id, c.symbol, c.shares_outstanding, c.price, cur.id, cur.code
FROM companies c
JOIN currencies cur ON cur.id = c.currency_id
`)
if err != nil {
return nil, err
@@ -77,7 +72,7 @@ func GetAllCompanies(db *sql.DB) ([]model.Company, error) {
for rows.Next() {
var c model.Company
if err := rows.Scan(
&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID,
&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID, &c.CurrencyCode,
); err != nil {
return nil, err
}