package dao

import "gorm.io/gorm"

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

func (m *Mnemonic) TableName() string {
	return "key_manager_mnemonic"
}

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

func (dao *Dao) GetMnemonic(name string) (*Mnemonic, error) {
	var mnemonic Mnemonic
	err := dao.db.Table(new(Mnemonic).TableName()).Where("name=?", name).First(&mnemonic).Error
	return &mnemonic, err
}

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