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.

chat.go 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 controllers
  15. import (
  16. "encoding/json"
  17. "github.com/casbin/casibase/object"
  18. )
  19. func (c *ApiController) GetGlobalChats() {
  20. chats, err := object.GetGlobalChats()
  21. if err != nil {
  22. c.ResponseError(err.Error())
  23. return
  24. }
  25. c.ResponseOk(chats)
  26. }
  27. func (c *ApiController) GetChats() {
  28. owner := "admin"
  29. chats, err := object.GetChats(owner)
  30. if err != nil {
  31. c.ResponseError(err.Error())
  32. return
  33. }
  34. c.ResponseOk(chats)
  35. }
  36. func (c *ApiController) GetChat() {
  37. id := c.Input().Get("id")
  38. chat, err := object.GetChat(id)
  39. if err != nil {
  40. c.ResponseError(err.Error())
  41. return
  42. }
  43. c.ResponseOk(chat)
  44. }
  45. func (c *ApiController) UpdateChat() {
  46. id := c.Input().Get("id")
  47. var chat object.Chat
  48. err := json.Unmarshal(c.Ctx.Input.RequestBody, &chat)
  49. if err != nil {
  50. c.ResponseError(err.Error())
  51. return
  52. }
  53. success, err := object.UpdateChat(id, &chat)
  54. if err != nil {
  55. c.ResponseError(err.Error())
  56. return
  57. }
  58. c.ResponseOk(success)
  59. }
  60. func (c *ApiController) AddChat() {
  61. var chat object.Chat
  62. err := json.Unmarshal(c.Ctx.Input.RequestBody, &chat)
  63. if err != nil {
  64. c.ResponseError(err.Error())
  65. return
  66. }
  67. chat.Owner = "admin"
  68. success, err := object.AddChat(&chat)
  69. if err != nil {
  70. c.ResponseError(err.Error())
  71. return
  72. }
  73. c.ResponseOk(success)
  74. }
  75. func (c *ApiController) DeleteChat() {
  76. var chat object.Chat
  77. err := json.Unmarshal(c.Ctx.Input.RequestBody, &chat)
  78. if err != nil {
  79. c.ResponseError(err.Error())
  80. return
  81. }
  82. success, err := object.DeleteChat(&chat)
  83. if err != nil {
  84. c.ResponseError(err.Error())
  85. return
  86. }
  87. c.ResponseOk(success)
  88. }