add revenue now workd through shell command
This commit is contained in:
54
internal/database/category.go
Normal file
54
internal/database/category.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"Portifolio/internal/model"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func InsertCategory(db *sql.DB, rc model.RevenueCategory) error {
|
||||
if err := rc.Validate(false); err != nil {
|
||||
return fmt.Errorf("failed to insert: %w", err)
|
||||
}
|
||||
|
||||
company, err := GetCompanyByID(db, rc.CompanyID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check company: %w", err)
|
||||
}
|
||||
if company == nil {
|
||||
return fmt.Errorf("company %d not found", rc.CompanyID)
|
||||
}
|
||||
|
||||
if rc.ParentID != nil && *rc.ParentID != 0 {
|
||||
parent, err := GetCategoryByID(db, rc.CompanyID, *rc.ParentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check parent category: %w", err)
|
||||
}
|
||||
if parent == nil {
|
||||
return fmt.Errorf("parent category %d not found", *rc.ParentID)
|
||||
}
|
||||
}
|
||||
|
||||
var parentID sql.NullInt64
|
||||
if rc.ParentID != nil && *rc.ParentID != 0 {
|
||||
parentID = sql.NullInt64{Int64: int64(*rc.ParentID), Valid: true}
|
||||
}
|
||||
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO category (company_id, parent_id, name) VALUES (?, ?, ?)
|
||||
ON CONFLICT(company_id, name) DO UPDATE SET parent_id=excluded.parent_id`,
|
||||
rc.CompanyID, parentID, rc.Name,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("upsert category: %w", err)
|
||||
}
|
||||
|
||||
err = db.QueryRow(
|
||||
`SELECT id FROM category WHERE company_id = ? AND name = ?`,
|
||||
rc.CompanyID, rc.Name,
|
||||
).Scan(&rc.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("select category id: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
25
internal/database/company.go
Normal file
25
internal/database/company.go
Normal file
@@ -0,0 +1,25 @@
|
||||
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
|
||||
}
|
||||
21
internal/database/currency.go
Normal file
21
internal/database/currency.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"Portifolio/internal/model"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func GetCurrencyByID(db *sql.DB, ID int) (model.Currency, error) {
|
||||
var c model.Currency
|
||||
err := db.QueryRow(
|
||||
`SELECT id, code, name, FROM currencies WHERE id = ?`,
|
||||
ID,
|
||||
).Scan(&c.ID, &c.Code, &c.Name)
|
||||
if err == sql.ErrNoRows {
|
||||
return c, fmt.Errorf("company %d not found", ID)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
@@ -9,7 +9,23 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func GetCategoryByID(db *sql.DB, companyID int, ID int) (*model.RevenueCategory, error) {
|
||||
var rc model.RevenueCategory
|
||||
err := db.QueryRow(
|
||||
`SELECT id, company_id, parent_id, name FROM category WHERE company_id = ? AND id = ?`,
|
||||
companyID, ID,
|
||||
).Scan(&rc.ID, &rc.CompanyID, &rc.ParentID, &rc.Name)
|
||||
if err == sql.ErrNoRows {
|
||||
return &rc, fmt.Errorf("category %q not found for company %d", ID, companyID)
|
||||
}
|
||||
if err != nil {
|
||||
return &rc, fmt.Errorf("get category by name: %w", err)
|
||||
}
|
||||
return &rc, nil
|
||||
}
|
||||
|
||||
func GetCategoryByName(db *sql.DB, companyID int, name string) (model.RevenueCategory, error) {
|
||||
|
||||
var rc model.RevenueCategory
|
||||
err := db.QueryRow(
|
||||
`SELECT id, company_id, parent_id, name FROM category WHERE company_id = ? AND name = ?`,
|
||||
|
||||
Reference in New Issue
Block a user