Add db.SettingsTypeFilter struct to db package

This commit is contained in:
qowevisa 2024-11-30 16:36:55 +02:00
parent 4ed24ab555
commit 03fa9ff9bc
2 changed files with 37 additions and 0 deletions

View File

@ -72,6 +72,7 @@ func Connect() *gorm.DB {
gormDB.AutoMigrate(&Metric{}) gormDB.AutoMigrate(&Metric{})
gormDB.AutoMigrate(&Currency{}) gormDB.AutoMigrate(&Currency{})
gormDB.AutoMigrate(&ExchangeRate{}) gormDB.AutoMigrate(&ExchangeRate{})
gormDB.AutoMigrate(&SettingsTypeFilter{})
return newUDB return newUDB
} }

View File

@ -0,0 +1,36 @@
package db
import (
"errors"
"gorm.io/gorm"
)
type SettingsTypeFilter struct {
gorm.Model
UserID uint
User *User
TypeID uint
Type *Type
}
// so `SETTINGS_TYPE` is the NAME of struct that has such error
// second `TYPE` means SettingsTypeFilter's Type fields are causing errors
var (
ERROR_SETTINGS_TYPE_TYPEID_ZERO = errors.New("SettingsTypeFilter's `TypeID` is zero")
ERROR_SETTINGS_TYPE_USERID_INVALID = errors.New("SettingsTypeFilter's `UserID` and Type's `UserID` are not equal")
)
func (st *SettingsTypeFilter) BeforeSave(tx *gorm.DB) error {
if st.TypeID == 0 {
return ERROR_SETTINGS_TYPE_TYPEID_ZERO
}
dbType := &Type{}
if err := tx.Find(dbType, st.TypeID).Error; err != nil {
return err
}
if dbType.UserID != st.UserID {
return ERROR_SETTINGS_TYPE_USERID_INVALID
}
return nil
}