model.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package model
  2. import (
  3. "fmt"
  4. "github.com/0xJacky/Nginx-UI/server/settings"
  5. "github.com/gin-gonic/gin"
  6. "github.com/spf13/cast"
  7. "gorm.io/driver/sqlite"
  8. "gorm.io/gen"
  9. "gorm.io/gorm"
  10. "gorm.io/gorm/logger"
  11. "log"
  12. "path"
  13. "time"
  14. )
  15. var db *gorm.DB
  16. type Model struct {
  17. ID uint `gorm:"primary_key" json:"id"`
  18. CreatedAt time.Time `json:"created_at"`
  19. UpdatedAt time.Time `json:"updated_at"`
  20. DeletedAt *time.Time `gorm:"index" json:"deleted_at"`
  21. }
  22. func GenerateAllModel() []any {
  23. return []any{
  24. ConfigBackup{},
  25. Auth{},
  26. AuthToken{},
  27. Cert{},
  28. ChatGPTLog{},
  29. }
  30. }
  31. func Init() *gorm.DB {
  32. dbPath := path.Join(path.Dir(settings.ConfPath), fmt.Sprintf("%s.db", settings.ServerSettings.Database))
  33. var err error
  34. db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{
  35. Logger: logger.Default.LogMode(logger.Info),
  36. PrepareStmt: true,
  37. DisableForeignKeyConstraintWhenMigrating: true,
  38. })
  39. if err != nil {
  40. log.Println(err)
  41. }
  42. // Migrate the schema
  43. err = db.AutoMigrate(GenerateAllModel()...)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. return db
  48. }
  49. func orderAndPaginate(c *gin.Context) func(db *gorm.DB) *gorm.DB {
  50. return func(db *gorm.DB) *gorm.DB {
  51. sort := c.DefaultQuery("sort", "desc")
  52. order := c.DefaultQuery("order_by", "id") +
  53. " " + sort
  54. page := cast.ToInt(c.Query("page"))
  55. if page == 0 {
  56. page = 1
  57. }
  58. pageSize := settings.ServerSettings.PageSize
  59. reqPageSize := c.Query("page_size")
  60. if reqPageSize != "" {
  61. pageSize = cast.ToInt(reqPageSize)
  62. }
  63. offset := (page - 1) * pageSize
  64. return db.Order(order).Offset(offset).Limit(pageSize)
  65. }
  66. }
  67. func totalPage(total int64, pageSize int) int64 {
  68. n := total / int64(pageSize)
  69. if total%int64(pageSize) > 0 {
  70. n++
  71. }
  72. return n
  73. }
  74. type Pagination struct {
  75. Total int64 `json:"total"`
  76. PerPage int `json:"per_page"`
  77. CurrentPage int `json:"current_page"`
  78. TotalPages int64 `json:"total_pages"`
  79. }
  80. type DataList struct {
  81. Data interface{} `json:"data"`
  82. Pagination Pagination `json:"pagination,omitempty"`
  83. }
  84. func GetListWithPagination(models interface{},
  85. c *gin.Context, totalRecords int64) (result DataList) {
  86. page := cast.ToInt(c.Query("page"))
  87. if page == 0 {
  88. page = 1
  89. }
  90. result = DataList{}
  91. result.Data = models
  92. pageSize := settings.ServerSettings.PageSize
  93. reqPageSize := c.Query("page_size")
  94. if reqPageSize != "" {
  95. pageSize = cast.ToInt(reqPageSize)
  96. }
  97. result.Pagination = Pagination{
  98. Total: totalRecords,
  99. PerPage: pageSize,
  100. CurrentPage: page,
  101. TotalPages: totalPage(totalRecords, pageSize),
  102. }
  103. return
  104. }
  105. type Method interface {
  106. // FirstByID Where("id=@id")
  107. FirstByID(id int) (*gen.T, error)
  108. // DeleteByID update @@table set deleted_at=NOW() where id=@id
  109. DeleteByID(id int) error
  110. }