seed.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package handler
  2. import (
  3. "net/http"
  4. "strconv"
  5. "spider/internal/model"
  6. "github.com/gin-gonic/gin"
  7. "gorm.io/gorm"
  8. )
  9. // SeedHandler handles managed seed CRUD.
  10. type SeedHandler struct {
  11. db *gorm.DB
  12. }
  13. // List returns seeds with optional status filter and pagination.
  14. // GET /seeds?page=1&page_size=20&status=active
  15. func (h *SeedHandler) List(c *gin.Context) {
  16. page, pageSize, offset := parsePage(c)
  17. query := h.db.Model(&model.ManagedSeed{})
  18. if status := c.Query("status"); status != "" {
  19. query = query.Where("status = ?", status)
  20. }
  21. var total int64
  22. if err := query.Count(&total).Error; err != nil {
  23. Fail(c, 500, err.Error())
  24. return
  25. }
  26. var seeds []model.ManagedSeed
  27. if err := query.Order("id DESC").Limit(pageSize).Offset(offset).Find(&seeds).Error; err != nil {
  28. Fail(c, 500, err.Error())
  29. return
  30. }
  31. PageOK(c, seeds, total, page, pageSize)
  32. }
  33. // Create creates a new seed.
  34. // POST /seeds body: {channel_name, note}
  35. func (h *SeedHandler) Create(c *gin.Context) {
  36. var body struct {
  37. ChannelName string `json:"channel_name" binding:"required"`
  38. Note string `json:"note"`
  39. }
  40. if err := c.ShouldBindJSON(&body); err != nil {
  41. Fail(c, http.StatusBadRequest, err.Error())
  42. return
  43. }
  44. seed := model.ManagedSeed{
  45. ChannelName: body.ChannelName,
  46. Note: body.Note,
  47. Status: "active",
  48. }
  49. if err := h.db.Create(&seed).Error; err != nil {
  50. Fail(c, 500, err.Error())
  51. return
  52. }
  53. OK(c, seed)
  54. }
  55. // Update modifies a seed's status and/or note.
  56. // PUT /seeds/:id body: {status, note}
  57. func (h *SeedHandler) Update(c *gin.Context) {
  58. id, err := strconv.ParseUint(c.Param("id"), 10, 64)
  59. if err != nil {
  60. Fail(c, http.StatusBadRequest, "invalid id")
  61. return
  62. }
  63. var body struct {
  64. Status string `json:"status"`
  65. Note string `json:"note"`
  66. }
  67. if err := c.ShouldBindJSON(&body); err != nil {
  68. Fail(c, http.StatusBadRequest, err.Error())
  69. return
  70. }
  71. var seed model.ManagedSeed
  72. if err := h.db.First(&seed, id).Error; err != nil {
  73. Fail(c, 404, "seed not found")
  74. return
  75. }
  76. updates := map[string]interface{}{}
  77. if body.Status != "" {
  78. updates["status"] = body.Status
  79. }
  80. updates["note"] = body.Note
  81. if err := h.db.Model(&seed).Updates(updates).Error; err != nil {
  82. Fail(c, 500, err.Error())
  83. return
  84. }
  85. h.db.First(&seed, id)
  86. OK(c, seed)
  87. }
  88. // Delete removes a seed by ID.
  89. // DELETE /seeds/:id
  90. func (h *SeedHandler) Delete(c *gin.Context) {
  91. id, err := strconv.ParseUint(c.Param("id"), 10, 64)
  92. if err != nil {
  93. Fail(c, http.StatusBadRequest, "invalid id")
  94. return
  95. }
  96. if err := h.db.Delete(&model.ManagedSeed{}, id).Error; err != nil {
  97. Fail(c, 500, err.Error())
  98. return
  99. }
  100. OK(c, nil)
  101. }