question.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package chat
  2. import (
  3. errors "confrontation-training/err"
  4. modelsChat "confrontation-training/models/chat"
  5. "confrontation-training/response"
  6. serviceChat "confrontation-training/service/chat"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. type QuestionService struct {
  12. serviceChat.QuestionService
  13. }
  14. func GetQuestionService() *QuestionService {
  15. return &QuestionService{}
  16. }
  17. // GetQuestion
  18. // PingExample confrontation-training
  19. // @Summary 查询问题
  20. // @Schemes
  21. // @Description 查询问题
  22. // @Tags 问答管理
  23. // @Param id query uint8 false "id:问题主键"
  24. // @Accept json
  25. // @Produce json
  26. // @Success 200 {string} string "ok"
  27. // @Router /v1/chat/get/chat [get]
  28. func (q *QuestionService) GetQuestion(c *gin.Context) {
  29. var id uint8
  30. if err := c.ShouldBind(&id); err != nil {
  31. log.Infof("参数绑定异常:%s", err.Error())
  32. fmt.Printf("参数绑定异常:%s", err.Error())
  33. response.Failed(errors.ParamInvalid, c)
  34. return
  35. }
  36. if id == 0 {
  37. id = 1
  38. }
  39. question, count := q.FindQuestionById(id)
  40. if count == 0 {
  41. response.Failed(errors.QuestionNotFound, c)
  42. } else {
  43. response.Success(errors.FindSuccess, question, c)
  44. }
  45. return
  46. }
  47. // CreateQuestion
  48. // PingExample confrontation-training
  49. // @Summary 录入问题
  50. // @Schemes
  51. // @Description 录入问题
  52. // @Tags 问答管理
  53. // @Param q body string true "chat:问题;nextQuestionNo:下一个问题编号:如果此编号不为空,则说明此问题为陈述,没有答案信息;questionType:题目类型:0选择题;1填空题"
  54. // @Accept json
  55. // @Produce json
  56. // @Success 200 {string} string "ok"
  57. // @Router /v1/chat/create/chat [post]
  58. func (q *QuestionService) CreateQuestion(c *gin.Context) {
  59. var param modelsChat.CreateQuestion
  60. if err := c.ShouldBindJSON(&param); err != nil {
  61. log.Infof("参数绑定异常:%s", err.Error())
  62. fmt.Printf("参数绑定异常:%s", err.Error())
  63. response.Failed(errors.ParamInvalid, c)
  64. return
  65. }
  66. question, count := q.CreateNewQuestion(param)
  67. if count == 0 {
  68. response.Failed(errors.QuestionCreateFailed, c)
  69. } else {
  70. response.Success(errors.FindSuccess, question, c)
  71. }
  72. return
  73. }