forked from carrydela/mygoTgChanBot
修复转发跳收藏;修复若干小bug
This commit is contained in:
@@ -105,6 +105,62 @@ func (s *Storage) UpdateCategoryOrder(name string, order int) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Storage) RenameCategory(oldName, newName string) error {
|
||||
return s.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket(bucketCategories)
|
||||
|
||||
// 检查旧分类是否存在
|
||||
data := b.Get([]byte(oldName))
|
||||
if data == nil {
|
||||
return fmt.Errorf("category %q not found", oldName)
|
||||
}
|
||||
|
||||
// 检查新名称是否已存在
|
||||
if b.Get([]byte(newName)) != nil {
|
||||
return fmt.Errorf("category %q already exists", newName)
|
||||
}
|
||||
|
||||
// 解码旧分类
|
||||
var cat Category
|
||||
if err := decodeJSON(data, &cat); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新名称
|
||||
cat.Name = newName
|
||||
newData, err := encodeJSON(cat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除旧键,写入新键
|
||||
if err := b.Delete([]byte(oldName)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.Put([]byte(newName), newData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 同步更新所有条目的分类名
|
||||
entries := tx.Bucket(bucketEntries)
|
||||
return entries.ForEach(func(k, v []byte) error {
|
||||
var entry Entry
|
||||
if err := decodeJSON(v, &entry); err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.Category == oldName {
|
||||
entry.Category = newName
|
||||
newEntryData, err := encodeJSON(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return entries.Put(k, newEntryData)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Storage) CategoryExists(name string) bool {
|
||||
exists := false
|
||||
s.db.View(func(tx *bolt.Tx) error {
|
||||
|
||||
Reference in New Issue
Block a user