fin-check-api/db/db.go

141 lines
3.3 KiB
Go
Raw Normal View History

2024-08-01 23:02:55 +02:00
package db
import (
"errors"
"fmt"
2024-08-01 23:02:55 +02:00
"log"
"os"
"sync"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var udb *gorm.DB
var conMu sync.Mutex
var (
ERROR_DB_NOT_INIT = errors.New("Database connection is not initialized")
)
func Init() error {
dbc := Connect()
// Seeds
if err := initStateOfDb(dbc); err != nil {
return fmt.Errorf("initStateOfDb: %w", err)
}
return nil
}
2024-08-01 23:02:55 +02:00
func Connect() *gorm.DB {
conMu.Lock()
defer conMu.Unlock()
if udb != nil {
return udb
}
2024-11-08 16:05:23 +01:00
logFile, err := os.OpenFile("db.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
2024-08-01 23:02:55 +02:00
newLogger := logger.New(
2024-11-08 16:05:23 +01:00
log.New(logFile, "\r\n", log.LstdFlags),
2024-08-01 23:02:55 +02:00
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Error,
IgnoreRecordNotFoundError: true,
ParameterizedQueries: true,
Colorful: false,
},
)
gormDB, err := gorm.Open(sqlite.Open("gonuts.db"), &gorm.Config{
Logger: newLogger,
})
if err != nil {
log.Panic(err)
}
newUDB := gormDB
2024-08-03 07:05:46 +02:00
gormDB.AutoMigrate(&Card{})
2024-08-01 23:02:55 +02:00
gormDB.AutoMigrate(&Category{})
gormDB.AutoMigrate(&Item{})
gormDB.AutoMigrate(&ItemPrice{})
gormDB.AutoMigrate(&Payment{})
gormDB.AutoMigrate(&ItemBought{})
gormDB.AutoMigrate(&Income{})
gormDB.AutoMigrate(&Debt{})
2024-08-03 07:15:39 +02:00
gormDB.AutoMigrate(&Transfer{})
2024-08-02 22:37:05 +02:00
gormDB.AutoMigrate(&User{})
2024-10-29 11:24:46 +01:00
gormDB.AutoMigrate(&Type{})
2024-11-06 18:47:12 +01:00
gormDB.AutoMigrate(&Session{})
2024-11-16 10:42:28 +01:00
gormDB.AutoMigrate(&Expense{})
2024-11-19 09:12:28 +01:00
gormDB.AutoMigrate(&Metric{})
2024-11-20 17:44:23 +01:00
gormDB.AutoMigrate(&Currency{})
2024-11-20 18:19:48 +01:00
gormDB.AutoMigrate(&ExchangeRate{})
2024-08-01 23:02:55 +02:00
return newUDB
}
var (
CANT_FIND_METRIC = errors.New("Can't find proper metrics in database")
)
func checkSeededValues[T Identifiable](whatToCheck []*T, errorIfNotFound error, tx *gorm.DB) error {
var valuesInDB []T
if err := tx.Find(&valuesInDB).Error; err != nil {
return err
}
if len(valuesInDB) == 0 {
for _, v := range whatToCheck {
if err := tx.Create(v).Error; err != nil {
return err
}
}
return nil
}
for _, v := range whatToCheck {
var tmp T
if err := tx.Find(&tmp, v).Error; err != nil {
return err
}
if tmp.GetID() == 0 {
return errorIfNotFound
}
}
return nil
}
func initMetrics(tx *gorm.DB) error {
metricsThatNeeded := []*Metric{
&Metric{Name: "None", Short: "pcs", Value: 0},
&Metric{Name: "Gram", Short: "g", Value: 1},
&Metric{Name: "Kilogram", Short: "kg", Value: 2},
&Metric{Name: "Liter", Short: "l", Value: 3},
}
return checkSeededValues(metricsThatNeeded, CANT_FIND_METRIC, tx)
}
func initCurrencies(tx *gorm.DB) error {
currsThatNeeded := []*Currency{
{Name: "Dollar", Symbol: "$", ISOName: "USD"},
{Name: "Moldavian Leu", Symbol: "L", ISOName: "MDL"},
2024-11-20 19:45:56 +01:00
{Name: "Romanian Leu", Symbol: "RL", ISOName: "RON"},
{Name: "Polish Zloty", Symbol: "zł", ISOName: "PLN"},
{Name: "Ukrainian Hryvnia", Symbol: "₴", ISOName: "UAH"},
{Name: "Euro", Symbol: "€", ISOName: "EUR"},
{Name: "Russian Ruble", Symbol: "₽", ISOName: "RUB"},
{Name: "Kazakhstani Tenge", Symbol: "₸", ISOName: "KZT"},
{Name: "Chinese Yuan", Symbol: "¥", ISOName: "CNY"},
}
return checkSeededValues(currsThatNeeded, CANT_FIND_METRIC, tx)
}
func initStateOfDb(tx *gorm.DB) error {
if err := initMetrics(tx); err != nil {
return fmt.Errorf("initMetrics: %w", err)
}
if err := initCurrencies(tx); err != nil {
return fmt.Errorf("initCurrencies: %w", err)
}
return nil
}