sentences_historay.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package mint
  2. import (
  3. "net/http"
  4. "strconv"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-pg/pg/v10"
  8. "github.com/go-redis/redis/v8"
  9. )
  10. type SentencesHistoray struct {
  11. Id int `form:"id" json:"id" `
  12. SentenceId int `form:"sentence_id" json:"sentence_id" `
  13. Content string `form:"content" json:"content" `
  14. ContentType string `form:"content_type" json:"content_type"`
  15. EditorId int
  16. CreatedAt time.Time
  17. }
  18. //display a list of all sentencesHistorays
  19. func SentencesHistoraiesIndex(db *pg.DB) gin.HandlerFunc {
  20. return func(c *gin.Context) {
  21. sentence_id := c.Query("sentence_id")
  22. // TODO 补充业务逻辑
  23. var sentencesHistorays []SentencesHistoray
  24. err := db.Model(&sentencesHistorays).Column("id", "sentence_id", "content", "content_type", "editor_id", "created_at").Where("sentence_id = ?", sentence_id).Select()
  25. if err != nil {
  26. panic(err)
  27. }
  28. c.JSON(http.StatusOK, gin.H{
  29. "status": "success",
  30. "data": sentencesHistorays,
  31. })
  32. }
  33. }
  34. //create a new sentencesHistoray
  35. func SentencesHistoraiesCreate(db *pg.DB) gin.HandlerFunc {
  36. return func(c *gin.Context) {
  37. var form SentencesHistoray
  38. if err := c.ShouldBindJSON(&form); err != nil {
  39. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  40. return
  41. }
  42. form.EditorId = 1
  43. //TODO补充业务逻辑
  44. _, err := db.Model(&form).Insert()
  45. if err != nil {
  46. panic(err)
  47. }
  48. //建立成功
  49. c.JSON(http.StatusOK, gin.H{
  50. "status": "success",
  51. "data": form,
  52. })
  53. }
  54. }
  55. //display a specific SentencesHistoray
  56. func SentencesHistoraiesShow(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  57. return func(c *gin.Context) {
  58. id, err := strconv.Atoi(c.Param("id"))
  59. if err != nil {
  60. panic(err)
  61. }
  62. sentencesHistoray := &SentencesHistoray{Id: id}
  63. err = db.Model(sentencesHistoray).Column("id", "sentence_id", "content", "content_type", "editor_id", "created_at").WherePK().First()
  64. if err != nil {
  65. panic(err)
  66. }
  67. c.JSON(http.StatusOK, gin.H{
  68. "status": "success",
  69. "data": sentencesHistoray,
  70. })
  71. }
  72. }