Add db.SettingsTypeFilter struct to db package
This commit is contained in:
parent
4ed24ab555
commit
03fa9ff9bc
1
db/db.go
1
db/db.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
36
db/settings_type_filter.go
Normal file
36
db/settings_type_filter.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user