Save 创建知识库
()
| 56 | |
| 57 | // Save 创建知识库 |
| 58 | func (library *Library) Save() error { |
| 59 | if library.Name == "" { |
| 60 | return errors.New("library name is required") |
| 61 | } |
| 62 | if library.LibraryKind == "" { |
| 63 | library.LibraryKind = LIBRARY_KIND_REGULAR |
| 64 | } |
| 65 | |
| 66 | // 检查同一空间下知识库名称是否重复 |
| 67 | existingLibrary, err := GetLibraryByName(library.SpaceID, library.Name) |
| 68 | if err == nil && existingLibrary != nil { |
| 69 | return errors.New("library name already exists in this space") |
| 70 | } |
| 71 | |
| 72 | // 生成UUID |
| 73 | if library.UUID == "" { |
| 74 | library.UUID = uuid.New().String() |
| 75 | } |
| 76 | |
| 77 | result := DB.Create(library) |
| 78 | if result.Error != nil { |
| 79 | return result.Error |
| 80 | } |
| 81 | invalidateLibraryCache(library.Eid) |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // SaveWithTx 使用传入事务创建知识库(避免全局DB依赖) |
| 86 | func (library *Library) SaveWithTx(tx *gorm.DB) error { |
no test coverage detected