yintai2/app/common/dao/job_translations.go

68 lines
1.7 KiB
Go

package dao
import (
"errors"
"yintai-api/app/common/model"
"gorm.io/gorm"
)
type JobTranslations struct {
db *gorm.DB
}
func NewJobTranslations(db ...*gorm.DB) *JobTranslations {
return &JobTranslations{db: getDB(db...)}
}
func (r *JobTranslations) TableName() string {
return "yt_job_translations"
}
func (r *JobTranslations) Create(m *model.JobTranslations) error {
return r.db.Table(r.TableName()).Create(m).Error
}
func (r *JobTranslations) Update(m *model.JobTranslations) error {
return r.db.Table(r.TableName()).Where("job_id = ? AND lang = ?", m.JobID, m.Lang).Save(m).Error
}
func (r *JobTranslations) Delete(id int64) error {
return r.db.Table(r.TableName()).Where("job_id = ?", id).Delete(&JobTranslations{}).Error
}
func (r *JobTranslations) Get(id int64) (*model.JobTranslations, error) {
var res model.JobTranslations
if err := r.db.Table(r.TableName()).Where("id = ?", id).First(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return &res, nil
}
func (r *JobTranslations) GetByMaster(masterID int64, lang string) (*model.JobTranslations, error) {
var res model.JobTranslations
tx := r.db.Table(r.TableName()).Where("job_id = ? AND lang = ?", masterID, lang).First(&res)
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, tx.Error
}
return &res, nil
}
func (r *JobTranslations) Exists(id int64) (bool, error) {
var count int64
if err := r.db.Table(r.TableName()).Where("id = ?", id).Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
func (r *JobTranslations) List(...any) ([]*model.JobTranslations, int64, error) {
panic("implement me")
}