feat(trade): implement second-hand trading/flea market feature
Add complete trade functionality including: - TradeItem, TradeImage, TradeFavorite models with auto-migration - TradeRepository with CRUD and favorite operations - TradeService with business logic for listing, creating, updating, status management - TradeHandler with RESTful endpoints for trade operations - DTOs and converters for request/response handling Routes include: list, get by id, create, update, delete, status update, view recording, favorite/unfavorite.
This commit is contained in:
@@ -179,6 +179,11 @@ func autoMigrate(db *gorm.DB) error {
|
||||
|
||||
// 帖子内链引用关系
|
||||
&PostReference{},
|
||||
|
||||
// 二手交易相关
|
||||
&TradeItem{},
|
||||
&TradeImage{},
|
||||
&TradeFavorite{},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
112
internal/model/trade.go
Normal file
112
internal/model/trade.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TradeType string
|
||||
|
||||
const (
|
||||
TradeTypeSell TradeType = "sell"
|
||||
TradeTypeBuy TradeType = "buy"
|
||||
)
|
||||
|
||||
type TradeCategory string
|
||||
|
||||
const (
|
||||
TradeCategoryDigital TradeCategory = "digital"
|
||||
TradeCategoryBook TradeCategory = "book"
|
||||
TradeCategoryClothing TradeCategory = "clothing"
|
||||
TradeCategoryDaily TradeCategory = "daily"
|
||||
TradeCategoryCosmetics TradeCategory = "cosmetics"
|
||||
TradeCategorySport TradeCategory = "sport"
|
||||
TradeCategoryTicket TradeCategory = "ticket"
|
||||
TradeCategoryOther TradeCategory = "other"
|
||||
)
|
||||
|
||||
type TradeItemStatus string
|
||||
|
||||
const (
|
||||
TradeItemActive TradeItemStatus = "active"
|
||||
TradeItemSold TradeItemStatus = "sold"
|
||||
TradeItemBought TradeItemStatus = "bought"
|
||||
TradeItemOffline TradeItemStatus = "offline"
|
||||
)
|
||||
|
||||
type TradeItem struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);index;not null"`
|
||||
TradeType TradeType `json:"trade_type" gorm:"type:varchar(10);not null;index"`
|
||||
Category TradeCategory `json:"category" gorm:"type:varchar(20);not null;index"`
|
||||
Title string `json:"title" gorm:"type:varchar(100);not null"`
|
||||
Content string `json:"content" gorm:"type:text"`
|
||||
Segments MessageSegments `json:"segments,omitempty" gorm:"type:text"`
|
||||
User *User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
Images []TradeImage `json:"images" gorm:"foreignKey:TradeItemID"`
|
||||
Price *float64 `json:"price,omitempty" gorm:"type:decimal(10,2)"`
|
||||
OriginalPrice *float64 `json:"original_price,omitempty" gorm:"type:decimal(10,2)"`
|
||||
Condition string `json:"condition,omitempty" gorm:"type:varchar(20)"`
|
||||
Location string `json:"location,omitempty" gorm:"type:varchar(100)"`
|
||||
Status TradeItemStatus `json:"status" gorm:"type:varchar(10);not null;default:'active';index"`
|
||||
ViewsCount int `json:"views_count" gorm:"default:0"`
|
||||
FavoritesCount int `json:"favorites_count" gorm:"default:0"`
|
||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime:false"`
|
||||
}
|
||||
|
||||
func (t *TradeItem) BeforeCreate(tx *gorm.DB) error {
|
||||
if t.ID == "" {
|
||||
t.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (TradeItem) TableName() string {
|
||||
return "trade_items"
|
||||
}
|
||||
|
||||
type TradeImage struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
TradeItemID string `json:"trade_item_id" gorm:"type:varchar(36);not null;index"`
|
||||
URL string `json:"url" gorm:"type:text;not null"`
|
||||
ThumbnailURL string `json:"thumbnail_url" gorm:"type:text"`
|
||||
PreviewURL string `json:"preview_url" gorm:"type:text"`
|
||||
PreviewURLLarge string `json:"preview_url_large" gorm:"type:text"`
|
||||
Width int `json:"width" gorm:"default:0"`
|
||||
Height int `json:"height" gorm:"default:0"`
|
||||
SortOrder int `json:"sort_order" gorm:"default:0"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
}
|
||||
|
||||
func (ti *TradeImage) BeforeCreate(tx *gorm.DB) error {
|
||||
if ti.ID == "" {
|
||||
ti.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (TradeImage) TableName() string {
|
||||
return "trade_images"
|
||||
}
|
||||
|
||||
type TradeFavorite struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
TradeItemID string `json:"trade_item_id" gorm:"type:varchar(36);not null;index;uniqueIndex:idx_trade_fav_item_user,priority:1"`
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index;uniqueIndex:idx_trade_fav_item_user,priority:2"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
}
|
||||
|
||||
func (f *TradeFavorite) BeforeCreate(tx *gorm.DB) error {
|
||||
if f.ID == "" {
|
||||
f.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (TradeFavorite) TableName() string {
|
||||
return "trade_favorites"
|
||||
}
|
||||
Reference in New Issue
Block a user