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.

wordset.go 2.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/casibase/casibase/util"
  18. "xorm.io/core"
  19. )
  20. type Wordset struct {
  21. Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
  22. Name string `xorm:"varchar(100) notnull pk" json:"name"`
  23. CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
  24. DisplayName string `xorm:"varchar(100)" json:"displayName"`
  25. DistanceLimit int `json:"distanceLimit"`
  26. Factorset string `xorm:"varchar(100)" json:"factorset"`
  27. Factors []*Factor `xorm:"mediumtext" json:"factors"`
  28. }
  29. func GetGlobalWordsets() ([]*Wordset, error) {
  30. wordsets := []*Wordset{}
  31. err := adapter.engine.Asc("owner").Desc("created_time").Find(&wordsets)
  32. if err != nil {
  33. return wordsets, err
  34. }
  35. return wordsets, nil
  36. }
  37. func GetWordsets(owner string) ([]*Wordset, error) {
  38. wordsets := []*Wordset{}
  39. err := adapter.engine.Desc("created_time").Find(&wordsets, &Wordset{Owner: owner})
  40. if err != nil {
  41. return wordsets, err
  42. }
  43. return wordsets, nil
  44. }
  45. func getWordset(owner string, name string) (*Wordset, error) {
  46. wordset := Wordset{Owner: owner, Name: name}
  47. existed, err := adapter.engine.Get(&wordset)
  48. if err != nil {
  49. return &wordset, err
  50. }
  51. if existed {
  52. return &wordset, nil
  53. } else {
  54. return nil, nil
  55. }
  56. }
  57. func GetWordset(id string) (*Wordset, error) {
  58. owner, name := util.GetOwnerAndNameFromId(id)
  59. return getWordset(owner, name)
  60. }
  61. func UpdateWordset(id string, wordset *Wordset) (bool, error) {
  62. owner, name := util.GetOwnerAndNameFromId(id)
  63. _, err := getWordset(owner, name)
  64. if err != nil {
  65. return false, err
  66. }
  67. if wordset == nil {
  68. return false, nil
  69. }
  70. _, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(wordset)
  71. if err != nil {
  72. return false, err
  73. }
  74. // return affected != 0
  75. return true, nil
  76. }
  77. func AddWordset(wordset *Wordset) (bool, error) {
  78. affected, err := adapter.engine.Insert(wordset)
  79. if err != nil {
  80. return false, err
  81. }
  82. return affected != 0, nil
  83. }
  84. func DeleteWordset(wordset *Wordset) (bool, error) {
  85. affected, err := adapter.engine.ID(core.PK{wordset.Owner, wordset.Name}).Delete(&Wordset{})
  86. if err != nil {
  87. return false, err
  88. }
  89. return affected != 0, nil
  90. }
  91. func (wordset *Wordset) GetId() string {
  92. return fmt.Sprintf("%s/%s", wordset.Owner, wordset.Name)
  93. }