moving insert to another model and done so in database internal
This commit is contained in:
BIN
Portifolio
BIN
Portifolio
Binary file not shown.
@@ -36,7 +36,7 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
t.Currency = currency
|
t.Currency = currency.Name
|
||||||
t.Ticker = *company
|
t.Ticker = *company
|
||||||
switch TypeInt {
|
switch TypeInt {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -83,7 +83,7 @@ func InsertTrade(db *sql.DB, trade model.Trade) error {
|
|||||||
_, err := db.Exec(
|
_, err := db.Exec(
|
||||||
"INSERT INTO trades (company_id, currency_id, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
"INSERT INTO trades (company_id, currency_id, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||||
trade.Ticker.ID,
|
trade.Ticker.ID,
|
||||||
trade.Currency.ID,
|
trade.Currency,
|
||||||
trade.Shares,
|
trade.Shares,
|
||||||
trade.Product,
|
trade.Product,
|
||||||
trade.Type,
|
trade.Type,
|
||||||
|
|||||||
@@ -82,3 +82,52 @@ func GetRevenueByCategory(db *sql.DB, companyID int, categoryID int) ([]model.Re
|
|||||||
|
|
||||||
return revenues, nil
|
return revenues, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InsertRevenue(db *sql.DB, rev model.RevenueInsert) error {
|
||||||
|
_, err := GetCompanyByID(db, rev.CompanyID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = GetCurrencyByID(db, rev.CurrencyID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// checking if period is in db, in case not will insert
|
||||||
|
_, err = GetPeriodByID(db, rev.Period.ID)
|
||||||
|
if err != nil {
|
||||||
|
err = rev.Period.Insert(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Getting Category, if error, trying to insert the category with the company.
|
||||||
|
category, err := GetCategoryByName(db, rev.CompanyID, rev.CategoryName)
|
||||||
|
if err != nil {
|
||||||
|
err := InsertCategory(db, model.RevenueCategory{
|
||||||
|
CompanyID: rev.CompanyID,
|
||||||
|
ParentID: &rev.ParentID,
|
||||||
|
Name: rev.CategoryName,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
category, err = GetCategoryByName(db, rev.CompanyID, rev.CategoryName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = db.Exec(
|
||||||
|
`INSERT INTO revenue_entries (company_id, currency_id, category_id, period_id, value)
|
||||||
|
VALUES (?, ?, ?, ?, ?)`,
|
||||||
|
rev.CompanyID, rev.CurrencyID, category.ID, rev.Period.ID, rev.Value,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("insert revenue_entries: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func AddTradeHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
Product: model.TradeProduct(req.Product),
|
Product: model.TradeProduct(req.Product),
|
||||||
Type: model.TradeType(req.Type),
|
Type: model.TradeType(req.Type),
|
||||||
Price: req.Price,
|
Price: req.Price,
|
||||||
Currency: currency,
|
Currency: currency.Name,
|
||||||
Date: req.Date,
|
Date: req.Date,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"Portifolio/internal/database"
|
"Portifolio/internal/database"
|
||||||
"Portifolio/internal/model"
|
"Portifolio/internal/model"
|
||||||
"Portifolio/internal/service"
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -41,7 +40,16 @@ func AddRevenueEntryHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := service.InsertRevenue(db, input.CompanyID, input.CurrencyID, input.Category, nil, input.Value, period); err != nil {
|
rev := model.RevenueInsert{
|
||||||
|
CompanyID: input.CompanyID,
|
||||||
|
CurrencyID: input.CurrencyID,
|
||||||
|
CategoryName: input.Category,
|
||||||
|
Period: period,
|
||||||
|
Value: input.Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := database.InsertRevenue(db, rev)
|
||||||
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ type Trade struct {
|
|||||||
Product TradeProduct
|
Product TradeProduct
|
||||||
Type TradeType
|
Type TradeType
|
||||||
Price float64
|
Price float64
|
||||||
Currency Currency
|
Currency string
|
||||||
Date time.Time
|
Date time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,3 +31,13 @@ type Revenue struct {
|
|||||||
Period *Period
|
Period *Period
|
||||||
Value float64
|
Value float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// made to be inserted to database
|
||||||
|
type RevenueInsert struct {
|
||||||
|
CompanyID int
|
||||||
|
CurrencyID int
|
||||||
|
CategoryName string
|
||||||
|
ParentID int
|
||||||
|
Period Period
|
||||||
|
Value float64
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
);`
|
|
||||||
*/
|
|
||||||
@@ -3,7 +3,6 @@ package shell
|
|||||||
import (
|
import (
|
||||||
"Portifolio/internal/database"
|
"Portifolio/internal/database"
|
||||||
"Portifolio/internal/model"
|
"Portifolio/internal/model"
|
||||||
"Portifolio/internal/service"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -103,7 +102,17 @@ func AddRevenue(scanner *bufio.Scanner, db *sql.DB) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := service.InsertRevenue(db, companyID, currencyID, category, parentID, value, period); err != nil {
|
rev := model.RevenueInsert{
|
||||||
|
CompanyID: companyID,
|
||||||
|
CurrencyID: currencyID,
|
||||||
|
CategoryName: category,
|
||||||
|
ParentID: *parentID,
|
||||||
|
Period: period,
|
||||||
|
Value: value,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = database.InsertRevenue(db, rev)
|
||||||
|
if err != nil {
|
||||||
fmt.Println(" ✗ Error:", err)
|
fmt.Println(" ✗ Error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -50,6 +50,7 @@ func main() {
|
|||||||
|
|
||||||
// Revenue
|
// Revenue
|
||||||
http.HandleFunc("POST /add/revenue/entry", handlers.AddRevenueEntryHandler(db))
|
http.HandleFunc("POST /add/revenue/entry", handlers.AddRevenueEntryHandler(db))
|
||||||
|
http.HandleFunc("POST /api/v1/revenue/add", handlers.AddRevenueEntryHandler(db))
|
||||||
|
|
||||||
//http.HandleFunc("GET /revenue/report", handlers.GetRevenueReportHandler(db))
|
//http.HandleFunc("GET /revenue/report", handlers.GetRevenueReportHandler(db))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user