Add whole schema
This commit is contained in:
parent
f06c4f645a
commit
6c15e218c9
12
db/account.go
Normal file
12
db/account.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Account can be either card or wallet
|
||||
type Account struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Value uint64
|
||||
HaveCreditLine bool
|
||||
CreditLine uint64
|
||||
}
|
11
db/category.go
Normal file
11
db/category.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Category struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
// Parent is used as a infinite sub-category structure
|
||||
ParentID uint
|
||||
Parent *Category
|
||||
}
|
54
db/db.go
Normal file
54
db/db.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"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 Connect() *gorm.DB {
|
||||
conMu.Lock()
|
||||
defer conMu.Unlock()
|
||||
if udb != nil {
|
||||
return udb
|
||||
}
|
||||
newLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
||||
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
|
||||
gormDB.AutoMigrate(&Account{})
|
||||
gormDB.AutoMigrate(&Category{})
|
||||
gormDB.AutoMigrate(&Item{})
|
||||
gormDB.AutoMigrate(&ItemPrice{})
|
||||
gormDB.AutoMigrate(&Payment{})
|
||||
gormDB.AutoMigrate(&ItemBought{})
|
||||
gormDB.AutoMigrate(&Income{})
|
||||
gormDB.AutoMigrate(&Debt{})
|
||||
return newUDB
|
||||
}
|
18
db/debt.go
Normal file
18
db/debt.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Debt struct {
|
||||
gorm.Model
|
||||
AccountID uint
|
||||
Account *Account
|
||||
Value uint64
|
||||
IOwe bool
|
||||
Date time.Time
|
||||
DateEnd time.Time
|
||||
Finished bool
|
||||
}
|
15
db/income.go
Normal file
15
db/income.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Income struct {
|
||||
gorm.Model
|
||||
AccountID uint
|
||||
Account *Account
|
||||
Value uint64
|
||||
Date time.Time
|
||||
}
|
51
db/item.go
Normal file
51
db/item.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Item struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
MetricType string
|
||||
MetricValue uint64
|
||||
//
|
||||
CategoryID uint
|
||||
Category *Category
|
||||
//
|
||||
Proteins uint64
|
||||
Carbs uint64
|
||||
Fats uint64
|
||||
//
|
||||
Prices []ItemPrice
|
||||
CurrentPriceID uint
|
||||
CurrentPrice *ItemPrice
|
||||
}
|
||||
|
||||
func GetItem(id uint, preloadPrices bool) (*Item, error) {
|
||||
if udb == nil {
|
||||
return nil, ERROR_DB_NOT_INIT
|
||||
}
|
||||
db := udb
|
||||
if preloadPrices {
|
||||
db = db.Preload("Prices")
|
||||
}
|
||||
var item Item
|
||||
err := db.Preload("Category").Preload("CurrentPrice").First(&item, id).Error
|
||||
return &item, err
|
||||
}
|
||||
|
||||
func GetItemToRootCat(id uint, preloadPrices bool) (*Item, error) {
|
||||
if udb == nil {
|
||||
return nil, ERROR_DB_NOT_INIT
|
||||
}
|
||||
db := udb
|
||||
if preloadPrices {
|
||||
db = db.Preload("Prices")
|
||||
}
|
||||
var item Item
|
||||
err := db.Preload("Category.Parent", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("Parent", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("Parent")
|
||||
})
|
||||
}).Preload("CurrentPrice").First(&item, id).Error
|
||||
return &item, err
|
||||
}
|
12
db/item_bought.go
Normal file
12
db/item_bought.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type ItemBought struct {
|
||||
gorm.Model
|
||||
ItemID uint
|
||||
Item *Item
|
||||
Quantity uint
|
||||
PaymentID uint
|
||||
Payment *Payment
|
||||
}
|
28
db/item_price.go
Normal file
28
db/item_price.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ItemPrice struct {
|
||||
gorm.Model
|
||||
ItemID uint
|
||||
Item *Item
|
||||
Price uint64
|
||||
ValidFrom time.Time
|
||||
IsValid bool `gorm:"default:true"`
|
||||
}
|
||||
|
||||
var (
|
||||
ERROR_ITEMPRICE_VALID_FROM_ERR = errors.New("ValidFrom shall be initiated when created")
|
||||
)
|
||||
|
||||
func (i *ItemPrice) BeforeCreate(tx *gorm.DB) error {
|
||||
if i.ValidFrom.IsZero() {
|
||||
i.ValidFrom = time.Now()
|
||||
}
|
||||
return nil
|
||||
}
|
21
db/payment.go
Normal file
21
db/payment.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// For grocery payment
|
||||
type Payment struct {
|
||||
gorm.Model
|
||||
AccountID uint
|
||||
Account *Account
|
||||
CategoryID uint
|
||||
Category *Category
|
||||
Name string
|
||||
Descr string
|
||||
Note string
|
||||
Items []ItemBought
|
||||
Date time.Time
|
||||
}
|
Loading…
Reference in New Issue
Block a user