2022-06-25 13:36:35 +00:00
|
|
|
package db
|
2022-06-16 13:59:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetMetaByPath(path string) (*model.Meta, error) {
|
2022-12-18 11:51:20 +00:00
|
|
|
meta := model.Meta{Path: path}
|
|
|
|
if err := db.Where(meta).First(&meta).Error; err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed select meta")
|
2022-06-16 13:59:49 +00:00
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
return &meta, nil
|
2022-06-16 13:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetMetaById(id uint) (*model.Meta, error) {
|
|
|
|
var u model.Meta
|
|
|
|
if err := db.First(&u, id).Error; err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed get old meta")
|
|
|
|
}
|
|
|
|
return &u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateMeta(u *model.Meta) error {
|
|
|
|
return errors.WithStack(db.Create(u).Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateMeta(u *model.Meta) error {
|
|
|
|
return errors.WithStack(db.Save(u).Error)
|
|
|
|
}
|
|
|
|
|
2022-12-18 11:51:20 +00:00
|
|
|
func GetMetas(pageIndex, pageSize int) (metas []model.Meta, count int64, err error) {
|
2022-06-16 13:59:49 +00:00
|
|
|
metaDB := db.Model(&model.Meta{})
|
2022-12-18 11:51:20 +00:00
|
|
|
if err = metaDB.Count(&count).Error; err != nil {
|
2022-06-16 13:59:49 +00:00
|
|
|
return nil, 0, errors.Wrapf(err, "failed get metas count")
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
if err = metaDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil {
|
2022-06-16 13:59:49 +00:00
|
|
|
return nil, 0, errors.Wrapf(err, "failed get find metas")
|
|
|
|
}
|
|
|
|
return metas, count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteMetaById(id uint) error {
|
|
|
|
return errors.WithStack(db.Delete(&model.Meta{}, id).Error)
|
|
|
|
}
|