You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

store.go 3.4 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package object
  15. import (
  16. "fmt"
  17. "github.com/casbin/casibase/util"
  18. "xorm.io/core"
  19. )
  20. type File struct {
  21. Key string `xorm:"varchar(100)" json:"key"`
  22. Title string `xorm:"varchar(100)" json:"title"`
  23. Size int64 `json:"size"`
  24. CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
  25. IsLeaf bool `json:"isLeaf"`
  26. Children []*File `xorm:"varchar(1000)" json:"children"`
  27. ChildrenMap map[string]*File `xorm:"-" json:"-"`
  28. }
  29. type Properties struct {
  30. CollectedTime string `xorm:"varchar(100)" json:"collectedTime"`
  31. Subject string `xorm:"varchar(100)" json:"subject"`
  32. }
  33. type Store struct {
  34. Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
  35. Name string `xorm:"varchar(100) notnull pk" json:"name"`
  36. CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
  37. DisplayName string `xorm:"varchar(100)" json:"displayName"`
  38. Bucket string `xorm:"varchar(100)" json:"bucket"`
  39. Domain string `xorm:"varchar(100)" json:"domain"`
  40. FileTree *File `xorm:"mediumtext" json:"fileTree"`
  41. PropertiesMap map[string]*Properties `xorm:"mediumtext" json:"propertiesMap"`
  42. }
  43. func GetGlobalStores() ([]*Store, error) {
  44. stores := []*Store{}
  45. err := adapter.engine.Asc("owner").Desc("created_time").Find(&stores)
  46. if err != nil {
  47. return stores, err
  48. }
  49. return stores, nil
  50. }
  51. func GetStores(owner string) ([]*Store, error) {
  52. stores := []*Store{}
  53. err := adapter.engine.Desc("created_time").Find(&stores, &Store{Owner: owner})
  54. if err != nil {
  55. return stores, err
  56. }
  57. return stores, nil
  58. }
  59. func getStore(owner string, name string) (*Store, error) {
  60. store := Store{Owner: owner, Name: name}
  61. existed, err := adapter.engine.Get(&store)
  62. if err != nil {
  63. return &store, err
  64. }
  65. if existed {
  66. return &store, nil
  67. } else {
  68. return nil, nil
  69. }
  70. }
  71. func GetStore(id string) (*Store, error) {
  72. owner, name := util.GetOwnerAndNameFromId(id)
  73. return getStore(owner, name)
  74. }
  75. func UpdateStore(id string, store *Store) (bool, error) {
  76. owner, name := util.GetOwnerAndNameFromId(id)
  77. _, err := getStore(owner, name)
  78. if err != nil {
  79. return false, err
  80. }
  81. if store == nil {
  82. return false, nil
  83. }
  84. _, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(store)
  85. if err != nil {
  86. return false, err
  87. }
  88. //return affected != 0
  89. return true, nil
  90. }
  91. func AddStore(store *Store) (bool, error) {
  92. affected, err := adapter.engine.Insert(store)
  93. if err != nil {
  94. return false, err
  95. }
  96. return affected != 0, nil
  97. }
  98. func DeleteStore(store *Store) (bool, error) {
  99. affected, err := adapter.engine.ID(core.PK{store.Owner, store.Name}).Delete(&Store{})
  100. if err != nil {
  101. return false, err
  102. }
  103. return affected != 0, nil
  104. }
  105. func (store *Store) GetId() string {
  106. return fmt.Sprintf("%s/%s", store.Owner, store.Name)
  107. }

基于Casbin的开源AI领域知识库平台