package dao

import "gorm.io/gorm"

type Whitelist struct {
	gorm.Model
	Name         string `gorm:"type:varchar(200);column:name;uniqueIndex" json:"name"`
	WhiteAddress string `gorm:"type:varchar(200);column:white_address;uniqueIndex" json:"white_address"`
}

func (w *Whitelist) TableName() string {
	return "key_manager_whitelist"
}

func (dao *Dao) CreateWhitelist(data *Whitelist) error {
	return dao.db.Create(&data).Error
}

func (dao *Dao) DeleteWhitelist(name string) error {
	err := dao.db.Where("name=?", name).Delete(&Whitelist{}).Error
	return err
}

func (dao *Dao) GetAllWhitelists() ([]Whitelist, error) {
	var whitelists []Whitelist
	err := dao.db.Table(new(Whitelist).TableName()).Find(&whitelists).Error
	return whitelists, err
}
