fix: issues

This commit is contained in:
Rogee
2025-01-14 19:48:23 +08:00
parent b429f30916
commit 48a0a6c195
14 changed files with 1037 additions and 195 deletions

View File

@@ -13,14 +13,16 @@ import (
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/speps/go-hashids/v2"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
// @provider:except
type Service struct {
db *sql.DB
log *log.Entry `inject:"false"`
db *sql.DB
hashIds *hashids.HashID
log *log.Entry `inject:"false"`
}
func (svc *Service) Prepare() error {
@@ -163,22 +165,14 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
return posts, count.Cnt, nil
}
// GetPostByHash
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByHash")
// GetPostByID
func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByID")
defer span.End()
span.SetAttributes(
attribute.String("hash", hash),
)
span.SetAttributes(attribute.Int64("post.id", id))
tbl := table.Posts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.Hash.EQ(String(hash)).AND(
tbl.TenantID.EQ(Int64(tenantID)),
),
)
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
var post model.Posts
@@ -220,3 +214,40 @@ func (svc *Service) GetUserBoughtIDs(ctx context.Context, tenantID, userID int64
return item.PostID
}), nil
}
// Create
func (svc *Service) Create(ctx context.Context, tenant *model.Tenants, user *model.Users, post *model.Posts) error {
_, span := otel.Start(ctx, "users.service.Create")
defer span.End()
tbl := table.Posts
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(post)
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return err
}
return nil
}
// GetPostByHash
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByHash")
defer span.End()
span.SetAttributes(
attribute.Int64("tenant.id", tenantID),
attribute.String("hash", hash),
)
postIDs, err := svc.hashIds.DecodeInt64WithError(hash)
if err != nil {
return nil, err
}
if tenantID != postIDs[0] {
return nil, nil
}
return svc.GetPostByID(ctx, postIDs[1])
}