5 分钟发布一次

This commit is contained in:
yml2213
2026-06-27 22:26:37 +08:00
parent 3abd513543
commit fed7d6af71
10 changed files with 261 additions and 9 deletions
+34 -2
View File
@@ -11,12 +11,17 @@ import (
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func (r *Repository) Create(ctx context.Context, ownerID uint64, req CreateRequest, reviewRequired bool) (*ListingDTO, error) {
func (r *Repository) Create(ctx context.Context, ownerID uint64, req CreateRequest, reviewRequired bool, publishCooldown time.Duration) (*ListingDTO, error) {
var dto *ListingDTO
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
listingNo, err := r.nextListingNo(tx, time.Now())
now := time.Now()
if err := r.ensureCreateCooldown(tx, ownerID, publishCooldown, now); err != nil {
return err
}
listingNo, err := r.nextListingNo(tx, now)
if err != nil {
return err
}
@@ -77,6 +82,33 @@ func (r *Repository) Create(ctx context.Context, ownerID uint64, req CreateReque
return dto, err
}
func (r *Repository) ensureCreateCooldown(tx *gorm.DB, ownerID uint64, cooldown time.Duration, now time.Time) error {
if cooldown <= 0 {
return nil
}
var user model.User
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Select("id").First(&user, ownerID).Error; err != nil {
return err
}
var latest model.RentalListing
err := tx.Where("owner_id = ?", ownerID).
Order("created_at DESC, id DESC").
First(&latest).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
remaining := latest.CreatedAt.Add(cooldown).Sub(now)
if remaining > 0 {
return PublishCooldownError{Cooldown: cooldown, Remaining: remaining}
}
return nil
}
type externalUploadCreate struct {
UploaderName string
ClientUploadTime *time.Time