remove: tenantslug

This commit is contained in:
Rogee
2025-01-15 20:31:21 +08:00
parent 13566cfa38
commit dbe1e19be2
24 changed files with 521 additions and 301 deletions

View File

@@ -3,17 +3,18 @@ package posts
import (
"context"
"database/sql"
"time"
"backend/app/requests"
"backend/database"
"backend/database/models/qvyun_v2/public/model"
"backend/database/models/qvyun_v2/public/table"
"backend/providers/hashids"
"backend/providers/otel"
. "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"
)
@@ -21,7 +22,7 @@ import (
// @provider:except
type Service struct {
db *sql.DB
hashIds *hashids.HashID
hashIds *hashids.Hasher
log *log.Entry `inject:"false"`
}
@@ -114,7 +115,7 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
)
tbl := table.Posts
cond := Bool(true)
cond := tbl.DeletedAt.IS_NULL()
if filter.ID != nil {
cond = cond.AND(tbl.ID.EQ(Int64(*filter.ID)))
}
@@ -165,6 +166,24 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
return posts, count.Cnt, nil
}
// ForceGetPostByID
func (svc *Service) ForceGetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.ForceGetPostByID")
defer span.End()
span.SetAttributes(attribute.Int64("post.id", id))
tbl := table.Posts
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
var post model.Posts
if err := stmt.QueryContext(ctx, svc.db, &post); err != nil {
return nil, err
}
return &post, nil
}
// GetPostByID
func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByID")
@@ -172,7 +191,7 @@ func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, er
span.SetAttributes(attribute.Int64("post.id", id))
tbl := table.Posts
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)).AND(tbl.DeletedAt.IS_NULL()))
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
var post model.Posts
@@ -240,14 +259,76 @@ func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash stri
attribute.String("hash", hash),
)
postIDs, err := svc.hashIds.DecodeInt64WithError(hash)
postId, err := svc.hashIds.DecodeOnlyInt64(hash)
if err != nil {
return nil, err
}
if tenantID != postIDs[0] {
return nil, nil
return svc.GetPostByID(ctx, postId)
}
// Delete
func (svc *Service) Delete(ctx context.Context, tenantID, userID, postID int64) error {
_, span := otel.Start(ctx, "users.service.Delete")
defer span.End()
span.SetAttributes(
attribute.Int64("tenant.id", tenantID),
attribute.Int64("user.id", userID),
attribute.Int64("post.id", postID),
)
tbl := table.Posts
stmt := tbl.
UPDATE().
SET(
tbl.DeletedAt.SET(TimestampT(time.Now())),
).
WHERE(
tbl.ID.EQ(Int64(postID)).AND(
tbl.TenantID.EQ(Int64(tenantID)).AND(
tbl.UserID.EQ(Int64(userID)),
),
),
)
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return err
}
return svc.GetPostByID(ctx, postIDs[1])
return nil
}
// Update
func (svc *Service) Update(ctx context.Context, tenantID, userID, postID int64, post *model.Posts) error {
_, span := otel.Start(ctx, "users.service.Update")
defer span.End()
span.SetAttributes(
attribute.Int64("tenant.id", tenantID),
attribute.Int64("user.id", userID),
attribute.Int64("post.id", postID),
)
tbl := table.Posts
stmt := tbl.
UPDATE(
tbl.MutableColumns.Except(
tbl.TenantID, tbl.UserID, tbl.CreatedAt, tbl.DeletedAt,
),
).
MODEL(post).
WHERE(
tbl.ID.EQ(Int64(postID)).AND(
tbl.TenantID.EQ(Int64(tenantID)).AND(
tbl.UserID.EQ(Int64(userID)),
),
),
)
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return err
}
return nil
}