moving insert to another model and done so in database internal
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"Portifolio/internal/database"
|
||||
"Portifolio/internal/model"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func InsertRevenue(db *sql.DB, companyID int, currencyID int, categoryName string, parentID *int, value float64, period model.Period) error {
|
||||
_, err := database.GetCompanyByID(db, companyID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = database.GetCurrencyByID(db, currencyID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// checking if period is in db, in case not will insert
|
||||
_, err = database.GetPeriodByID(db, period.ID)
|
||||
if err != nil {
|
||||
err = period.Insert(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Getting Category, if error, trying to insert the category with the company.
|
||||
category, err := database.GetCategoryByName(db, companyID, categoryName)
|
||||
if err != nil {
|
||||
err := database.InsertCategory(db, model.RevenueCategory{
|
||||
CompanyID: companyID,
|
||||
ParentID: parentID,
|
||||
Name: categoryName,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
category, err = database.GetCategoryByName(db, companyID, categoryName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO revenue_entries (company_id, currency_id, category_id, period_id, value)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
companyID, currencyID, category.ID, period.ID, value,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert revenue_entries: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
CREATE TABLE IF NOT EXISTS revenue_entries (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
currency_id INTEGER NOT NULL,
|
||||
category_id INTEGER NOT NULL,
|
||||
period_id INTEGER NOT NULL,
|
||||
value REAL NOT NULL,
|
||||
FOREIGN KEY (company_id) REFERENCES companies(id),
|
||||
FOREIGN KEY (currency_id) REFERENCES currencies(id),
|
||||
FOREIGN KEY (category_id) REFERENCES category(id),
|
||||
FOREIGN KEY (period_id) REFERENCES periods(id),
|
||||
UNIQUE(company_id, category_id, period_id)
|
||||
);`
|
||||
*/
|
||||
Reference in New Issue
Block a user