Add event service, caddy test domain

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-27 03:45:31 +08:00
parent 2b99d415de
commit afc62f311b
12 changed files with 145 additions and 12 deletions

View File

@@ -1,11 +1,17 @@
package data
import (
"context"
"errors"
"fmt"
"math/rand"
"strings"
"time"
"github.com/go-viper/mapstructure/v2"
"github.com/google/uuid"
"github.com/meilisearch/meilisearch-go"
"github.com/spf13/viper"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -157,3 +163,62 @@ func (self *User) FastListUsers(limit, offset int64) (*[]UserSearchDoc, error) {
}
return &list, nil
}
func (self *User) GenCheckinCode(eventId uuid.UUID) (*string, error) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNumber := rng.Intn(900000) + 100000
randNumString := fmt.Sprintf("%06d", randomNumber)
ctx := context.Background()
ttl := viper.GetDuration("ttl.checkin_code_ttl")
Redis.Set(
ctx,
"checkin_code:"+randNumString,
self.UserId.String()+":"+eventId.String(),
ttl,
)
return &randNumString, nil
}
func (self *User) VerifyCheckinCode(checkinCode string) (*uuid.UUID, error) {
ctx := context.Background()
result := Redis.Get(ctx, "checkin_code:"+checkinCode).String()
if result == "" {
return nil, errors.New("invalid or expired checkin code")
}
split := strings.Split(result, ":")
if len(split) < 2 {
return nil, errors.New("invalid checkin code format")
}
userId := split[0]
eventId := split[1]
var returnedUserId uuid.UUID
err := Database.Transaction(func(tx *gorm.DB) error {
checkinData := map[string]interface{}{
eventId: time.Now(),
}
if err := tx.Model(&User{}).Where("user_id = ?", userId).Updates(map[string]interface{}{
"checkin": checkinData,
}).Error; err != nil {
return err
}
parsedUserId, err := uuid.Parse(userId)
if err != nil {
return err
}
returnedUserId = parsedUserId
return nil
})
if err != nil {
return nil, err
}
return &returnedUserId, nil
}