topic_util.go 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package topic
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gorm.io/gorm/clause"
  5. "github.com/runningwater/gohub/pkg/app"
  6. "github.com/runningwater/gohub/pkg/database"
  7. "github.com/runningwater/gohub/pkg/paginator"
  8. )
  9. func Get(idStr string) (topic Topic) {
  10. database.DB.Preload(clause.Associations).Where("id", idStr).First(&topic)
  11. return
  12. }
  13. func GetBy(field, value string) (topic Topic) {
  14. database.DB.Where("? = ?", field, value).First(&topic)
  15. return
  16. }
  17. func All() (topics []Topic) {
  18. database.DB.Find(&topics)
  19. return
  20. }
  21. func IsExist(field, value string) bool {
  22. var count int64
  23. database.DB.Model(Topic{}).Where("? = ?", field, value).Count(&count)
  24. return count > 0
  25. }
  26. // Paginate 分页内容
  27. func Paginate(c *gin.Context, pageSize int) (topics []Topic, paging paginator.Paging) {
  28. paging = paginator.Paginate(
  29. c,
  30. database.DB.Model(Topic{}),
  31. &topics,
  32. app.V1URL(database.TableName(&Topic{})),
  33. pageSize,
  34. )
  35. return
  36. }