package repository import ( "context" "next-terminal/server/model" ) var CertificateRepository = new(certificateRepository) type certificateRepository struct { baseRepository } func (r certificateRepository) FindAll(c context.Context) (o []model.Certificate, err error) { err = r.GetDB(c).Order("not_after asc, created asc").Find(&o).Error return } func (r certificateRepository) Find(c context.Context, pageIndex, pageSize int, keyword string) (o []model.Certificate, total int64, err error) { db := r.GetDB(c).Model(&model.Certificate{}) if len(keyword) > 0 { db = db.Where("common_name like ? or subject like ?", "%"+keyword+"%", "%"+keyword+"%") } err = db.Count(&total).Error if err != nil { return nil, 0, err } query := r.GetDB(c).Model(&model.Certificate{}) if len(keyword) > 0 { query = query.Where("common_name like ? or subject like ?", "%"+keyword+"%", "%"+keyword+"%") } err = query.Order("not_after asc, created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error if o == nil { o = make([]model.Certificate, 0) } return } func (r certificateRepository) Create(c context.Context, o *model.Certificate) error { return r.GetDB(c).Create(o).Error } func (r certificateRepository) UpdateById(c context.Context, o *model.Certificate, id string) error { o.ID = id return r.GetDB(c).Where("id = ?", id).Updates(o).Error } func (r certificateRepository) DeleteById(c context.Context, id string) error { return r.GetDB(c).Where("id = ?", id).Delete(&model.Certificate{}).Error } func (r certificateRepository) FindById(c context.Context, id string) (o model.Certificate, err error) { err = r.GetDB(c).Where("id = ?", id).First(&o).Error return }