model.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. Site{},
  30. }
  31. }
  32. func Init() *gorm.DB {
  33. dbPath := path.Join(path.Dir(settings.ConfPath), fmt.Sprintf("%s.db", settings.ServerSettings.Database))
  34. var err error
  35. db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{
  36. Logger: logger.Default.LogMode(logger.Info),
  37. PrepareStmt: true,
  38. DisableForeignKeyConstraintWhenMigrating: true,
  39. })
  40. if err != nil {
  41. log.Println(err)
  42. }
  43. // Migrate the schema
  44. err = db.AutoMigrate(GenerateAllModel()...)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. return db
  49. }
  50. func orderAndPaginate(c *gin.Context) func(db *gorm.DB) *gorm.DB {
  51. return func(db *gorm.DB) *gorm.DB {
  52. sort := c.DefaultQuery("sort", "desc")
  53. order := c.DefaultQuery("order_by", "id") +
  54. " " + sort
  55. page := cast.ToInt(c.Query("page"))
  56. if page == 0 {
  57. page = 1
  58. }
  59. pageSize := settings.ServerSettings.PageSize
  60. reqPageSize := c.Query("page_size")
  61. if reqPageSize != "" {
  62. pageSize = cast.ToInt(reqPageSize)
  63. }
  64. offset := (page - 1) * pageSize
  65. return db.Order(order).Offset(offset).Limit(pageSize)
  66. }
  67. }
  68. func totalPage(total int64, pageSize int) int64 {
  69. n := total / int64(pageSize)
  70. if total%int64(pageSize) > 0 {
  71. n++
  72. }
  73. return n
  74. }
  75. type Pagination struct {
  76. Total int64 `json:"total"`
  77. PerPage int `json:"per_page"`
  78. CurrentPage int `json:"current_page"`
  79. TotalPages int64 `json:"total_pages"`
  80. }
  81. type DataList struct {
  82. Data interface{} `json:"data"`
  83. Pagination Pagination `json:"pagination,omitempty"`
  84. }
  85. func GetListWithPagination(models interface{},
  86. c *gin.Context, totalRecords int64) (result DataList) {
  87. page := cast.ToInt(c.Query("page"))
  88. if page == 0 {
  89. page = 1
  90. }
  91. result = DataList{}
  92. result.Data = models
  93. pageSize := settings.ServerSettings.PageSize
  94. reqPageSize := c.Query("page_size")
  95. if reqPageSize != "" {
  96. pageSize = cast.ToInt(reqPageSize)
  97. }
  98. result.Pagination = Pagination{
  99. Total: totalRecords,
  100. PerPage: pageSize,
  101. CurrentPage: page,
  102. TotalPages: totalPage(totalRecords, pageSize),
  103. }
  104. return
  105. }
  106. type Method interface {
  107. // FirstByID Where("id=@id")
  108. FirstByID(id int) (*gen.T, error)
  109. // DeleteByID update @@table set deleted_at=NOW() where id=@id
  110. DeleteByID(id int) error
  111. }