68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"yintai-api/app/common/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type NewsTranslations struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewNewsTranslations(db ...*gorm.DB) *NewsTranslations {
|
|
return &NewsTranslations{db: getDB(db...)}
|
|
}
|
|
|
|
func (r *NewsTranslations) TableName() string {
|
|
return "yt_news_translations"
|
|
}
|
|
|
|
func (r *NewsTranslations) Create(m *model.NewsTranslations) error {
|
|
return r.db.Table(r.TableName()).Create(m).Error
|
|
}
|
|
|
|
func (r *NewsTranslations) Update(m *model.NewsTranslations) error {
|
|
return r.db.Table(r.TableName()).Where("news_id = ? AND lang = ?", m.NewsID, m.Lang).Save(m).Error
|
|
}
|
|
|
|
func (r *NewsTranslations) Delete(id int64) error {
|
|
return r.db.Table(r.TableName()).Where("news_id = ?", id).Delete(&NewsTranslations{}).Error
|
|
}
|
|
|
|
func (r *NewsTranslations) Get(id int64) (*model.NewsTranslations, error) {
|
|
var res model.NewsTranslations
|
|
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 *NewsTranslations) GetByMaster(masterID int64, lang string) (*model.NewsTranslations, error) {
|
|
var res model.NewsTranslations
|
|
tx := r.db.Table(r.TableName()).Where("news_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 *NewsTranslations) 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 *NewsTranslations) List(...any) ([]*model.NewsTranslations, int64, error) {
|
|
panic("implement me")
|
|
}
|