package posts import ( "context" "database/sql" "backend/app/requests" "backend/database" "backend/database/models/qvyun_v2/public/model" "backend/database/models/qvyun_v2/public/table" "backend/providers/otel" . "github.com/go-jet/jet/v2/postgres" log "github.com/sirupsen/logrus" "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"` } func (svc *Service) Prepare() error { svc.log = log.WithField("module", "posts.service") _ = Int(1) return nil } // GetPosts func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Pagination, filter *UserPostFilter) ([]model.Posts, int64, error) { _, span := otel.Start(ctx, "users.service.GetPosts") defer span.End() span.SetAttributes( attribute.Int64("user.id", filter.UserID), attribute.Int64("page.page", pagination.Page), attribute.Int64("page.limit", pagination.Limit), ) tbl := table.Posts cond := Bool(true) if filter.ID != nil { cond = cond.AND(tbl.ID.EQ(Int64(*filter.ID))) } if filter.UserID != 0 { cond = cond.AND(tbl.UserID.EQ(Int64(filter.UserID))) } if filter.CreatedAt != nil { cond = cond.AND(tbl.CreatedAt.LT_EQ(TimestampT(*filter.CreatedAt))) } if filter.TenantID != 0 { cond = cond.AND(tbl.TenantID.EQ(Int64(filter.TenantID))) } if filter.Keyword != nil { cond = cond.AND( tbl.Title. LIKE(String(database.WrapLike(*filter.Keyword))). OR( tbl.Description.LIKE(String(database.WrapLike(*filter.Keyword))), ). OR( tbl.Content.LIKE(String(database.WrapLike(*filter.Keyword))), ), ) } cntStmt := tbl.SELECT(COUNT(tbl.ID).AS("cnt")).WHERE(cond) span.SetAttributes(semconv.DBStatementKey.String(cntStmt.DebugSql())) var count struct { Cnt int64 } if err := cntStmt.QueryContext(ctx, svc.db, &count); err != nil { return nil, 0, err } stmt := tbl. SELECT(tbl.AllColumns). ORDER_BY(tbl.ID.DESC()). LIMIT(pagination.Limit). OFFSET(pagination.Offset()) span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql())) var posts []model.Posts if err := stmt.QueryContext(ctx, svc.db, &posts); err != nil { return nil, 0, err } return posts, count.Cnt, nil }