faq_controller.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package controller
  2. import (
  3. "log"
  4. "net/http"
  5. "strconv"
  6. "github.com/2930134478/AI-CS/backend/service"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // FAQController 负责处理 FAQ(常见问题)相关的 HTTP 请求。
  10. type FAQController struct {
  11. faqService *service.FAQService
  12. }
  13. // NewFAQController 创建 FAQController 实例。
  14. func NewFAQController(faqService *service.FAQService) *FAQController {
  15. return &FAQController{faqService: faqService}
  16. }
  17. // ListFAQs 获取 FAQ 列表,支持关键词搜索。
  18. // GET /faqs?query=openai%api%调用
  19. func (f *FAQController) ListFAQs(c *gin.Context) {
  20. // 获取查询参数
  21. query := c.Query("query")
  22. // 查询 FAQ 列表
  23. faqs, err := f.faqService.ListFAQs(query)
  24. if err != nil {
  25. log.Printf("查询 FAQ 列表失败: %v", err)
  26. c.JSON(http.StatusInternalServerError, gin.H{"error": "查询 FAQ 列表失败"})
  27. return
  28. }
  29. c.JSON(http.StatusOK, gin.H{
  30. "faqs": faqs,
  31. })
  32. }
  33. // GetFAQ 获取 FAQ 详情。
  34. // GET /faqs/:id
  35. func (f *FAQController) GetFAQ(c *gin.Context) {
  36. // 获取 ID
  37. idStr := c.Param("id")
  38. id, err := strconv.ParseUint(idStr, 10, 64)
  39. if err != nil || id == 0 {
  40. c.JSON(http.StatusBadRequest, gin.H{"error": "FAQ ID 不合法"})
  41. return
  42. }
  43. // 查询 FAQ
  44. faq, err := f.faqService.GetFAQ(uint(id))
  45. if err != nil {
  46. log.Printf("查询 FAQ 失败: %v", err)
  47. c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
  48. return
  49. }
  50. c.JSON(http.StatusOK, faq)
  51. }
  52. // CreateFAQ 创建新的 FAQ 记录。
  53. // POST /faqs
  54. func (f *FAQController) CreateFAQ(c *gin.Context) {
  55. var req struct {
  56. Question string `json:"question" binding:"required"`
  57. Answer string `json:"answer" binding:"required"`
  58. Keywords string `json:"keywords"`
  59. }
  60. if err := c.ShouldBindJSON(&req); err != nil {
  61. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  62. return
  63. }
  64. // 创建 FAQ
  65. faq, err := f.faqService.CreateFAQ(service.CreateFAQInput{
  66. Question: req.Question,
  67. Answer: req.Answer,
  68. Keywords: req.Keywords,
  69. })
  70. if err != nil {
  71. log.Printf("创建 FAQ 失败: %v", err)
  72. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  73. return
  74. }
  75. c.JSON(http.StatusOK, faq)
  76. }
  77. // UpdateFAQ 更新 FAQ 记录。
  78. // PUT /faqs/:id
  79. func (f *FAQController) UpdateFAQ(c *gin.Context) {
  80. // 获取 ID
  81. idStr := c.Param("id")
  82. id, err := strconv.ParseUint(idStr, 10, 64)
  83. if err != nil || id == 0 {
  84. c.JSON(http.StatusBadRequest, gin.H{"error": "FAQ ID 不合法"})
  85. return
  86. }
  87. var req struct {
  88. Question *string `json:"question"`
  89. Answer *string `json:"answer"`
  90. Keywords *string `json:"keywords"`
  91. }
  92. if err := c.ShouldBindJSON(&req); err != nil {
  93. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  94. return
  95. }
  96. // 更新 FAQ
  97. faq, err := f.faqService.UpdateFAQ(uint(id), service.UpdateFAQInput{
  98. Question: req.Question,
  99. Answer: req.Answer,
  100. Keywords: req.Keywords,
  101. })
  102. if err != nil {
  103. log.Printf("更新 FAQ 失败: %v", err)
  104. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  105. return
  106. }
  107. c.JSON(http.StatusOK, faq)
  108. }
  109. // DeleteFAQ 删除 FAQ 记录。
  110. // DELETE /faqs/:id
  111. func (f *FAQController) DeleteFAQ(c *gin.Context) {
  112. // 获取 ID
  113. idStr := c.Param("id")
  114. id, err := strconv.ParseUint(idStr, 10, 64)
  115. if err != nil || id == 0 {
  116. c.JSON(http.StatusBadRequest, gin.H{"error": "FAQ ID 不合法"})
  117. return
  118. }
  119. // 删除 FAQ
  120. if err := f.faqService.DeleteFAQ(uint(id)); err != nil {
  121. log.Printf("删除 FAQ 失败: %v", err)
  122. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  123. return
  124. }
  125. c.JSON(http.StatusOK, gin.H{"message": "删除成功"})
  126. }