runningwater 6 месяцев назад
Родитель
Сommit
a5264466a9

+ 1 - 1
app/models/model.go

@@ -7,7 +7,7 @@ import (
 
 // BaseModel 基础模型
 type BaseModel struct {
-	ID uint `gorm:"column:id;primaryKey;autoIncrement;" json:"id,omitempty"` // 主键ID
+	ID uint `gorm:"column:id;primaryKey;autoIncrement;comment:主键ID" json:"id,omitempty"` // 主键ID
 }
 
 type CommonTimestampsField struct {

+ 11 - 0
app/models/topic/topic_hooks.go

@@ -0,0 +1,11 @@
+package topic
+
+// func (topic *Topic) BeforeSave(tx *gorm.DB) (err error) {}
+// func (topic *Topic) BeforeCreate(tx *gorm.DB) (err error) {}
+// func (topic *Topic) AfterCreate(tx *gorm.DB) (err error) {}
+// func (topic *Topic) BeforeUpdate(tx *gorm.DB) (err error) {}
+// func (topic *Topic) AfterUpdate(tx *gorm.DB) (err error) {}
+// func (topic *Topic) AfterSave(tx *gorm.DB) (err error) {}
+// func (topic *Topic) BeforeDelete(tx *gorm.DB) (err error) {}
+// func (topic *Topic) AfterDelete(tx *gorm.DB) (err error) {}
+// func (topic *Topic) AfterFind(tx *gorm.DB) (err error) {}

+ 39 - 0
app/models/topic/topic_model.go

@@ -0,0 +1,39 @@
+// Package topic 模型
+package topic
+
+import (
+	"github.com/runningwater/gohub/app/models"
+	"github.com/runningwater/gohub/app/models/category"
+	"github.com/runningwater/gohub/app/models/user"
+	"github.com/runningwater/gohub/pkg/database"
+)
+
+type Topic struct {
+	models.BaseModel
+
+	Title      string `json:"title,omitempty"`
+	Body       string `json:"body,omitempty"`
+	UserID     string `json:"user_id,omitempty"`
+	CategoryID string `json:"category_id,omitempty"`
+
+	// 通过 user_id 关联用户
+	User user.User `json:"user,omitempty"`
+	// 通过 category_id 关联分类
+	Category category.Category `json:"category,omitempty"`
+
+	models.CommonTimestampsField
+}
+
+func (topic *Topic) Create() {
+	database.DB.Create(&topic)
+}
+
+func (topic *Topic) Save() (rowsAffected int64) {
+	result := database.DB.Save(&topic)
+	return result.RowsAffected
+}
+
+func (topic *Topic) Delete() (rowsAffected int64) {
+	result := database.DB.Delete(&topic)
+	return result.RowsAffected
+}

+ 42 - 0
app/models/topic/topic_util.go

@@ -0,0 +1,42 @@
+package topic
+
+import (
+	"github.com/gin-gonic/gin"
+
+	"github.com/runningwater/gohub/pkg/app"
+	"github.com/runningwater/gohub/pkg/database"
+	"github.com/runningwater/gohub/pkg/paginator"
+)
+
+func Get(idStr string) (topic Topic) {
+	database.DB.Where("id", idStr).First(&topic)
+	return
+}
+
+func GetBy(field, value string) (topic Topic) {
+	database.DB.Where("? = ?", field, value).First(&topic)
+	return
+}
+
+func All() (topics []Topic) {
+	database.DB.Find(&topics)
+	return
+}
+
+func IsExist(field, value string) bool {
+	var count int64
+	database.DB.Model(Topic{}).Where("? = ?", field, value).Count(&count)
+	return count > 0
+}
+
+// Paginate 分页内容
+func Paginate(c *gin.Context, pageSize int) (topics []Topic, paging paginator.Paging) {
+	paging = paginator.Paginate(
+		c,
+		database.DB.Model(Topic{}),
+		&topics,
+		app.V1URL(database.TableName(&Topic{})),
+		pageSize,
+	)
+	return
+}

+ 44 - 0
database/migrations/2025_07_16_155731_add_topics_table.go

@@ -0,0 +1,44 @@
+package migrations
+
+import (
+	"gorm.io/gorm"
+
+	"github.com/runningwater/gohub/app/models"
+	"github.com/runningwater/gohub/pkg/migrate"
+)
+
+func init() {
+
+	type User struct {
+		models.BaseModel
+	}
+
+	type Category struct {
+		models.BaseModel
+	}
+
+	type Topic struct {
+		models.BaseModel
+
+		Title      string `gorm:"type:varchar(255);not null;index;comment:标题"`
+		Body       string `gorm:"type:longtext;not null;comment:内容"`
+		UserID     uint   `gorm:"type:bigint;not null; index;comment:用户ID"`
+		CategoryID uint   `gorm:"type:bigint;not null; index;comment:分类ID"`
+
+		// 创建 user_id 和 category_id 外键关联
+		User     User
+		Category Category
+
+		models.CommonTimestampsField
+	}
+
+	up := func(migrator gorm.Migrator, DB *gorm.DB) {
+		_ = DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci").AutoMigrate(&Topic{})
+	}
+
+	down := func(migrator gorm.Migrator, DB *gorm.DB) {
+		_ = migrator.DropTable(&Topic{})
+	}
+
+	migrate.Add(up, down, "2025_07_16_155731_add_topics_table")
+}