This commit is contained in:
@@ -80,9 +80,19 @@ func (s *KolPackageService) List(ctx context.Context, actor auth.Actor) ([]KolPa
|
||||
return nil, response.ErrInternal(50062, "kol_package_query_failed", err.Error())
|
||||
}
|
||||
|
||||
packageIDs := kolPackageIDs(packages)
|
||||
promptCounts, err := s.packagePromptCounts(ctx, actor.TenantID, packageIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
selfSubscriptions, err := s.packageSelfSubscriptions(ctx, actor.TenantID, packageIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]KolPackageResponse, 0, len(packages))
|
||||
for _, pkg := range packages {
|
||||
item, err := s.buildPackageResponse(ctx, profile, &pkg)
|
||||
item, err := s.buildPackageResponseWithStats(profile, &pkg, promptCounts[pkg.ID], selfSubscriptions[pkg.ID])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -299,30 +309,31 @@ func (s *KolPackageService) buildPackageResponse(
|
||||
ctx context.Context,
|
||||
profile *repository.KolProfile,
|
||||
pkg *repository.KolPackage,
|
||||
) (*KolPackageResponse, error) {
|
||||
promptCounts, err := s.packagePromptCounts(ctx, pkg.TenantID, []int64{pkg.ID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
selfSubscriptions, err := s.packageSelfSubscriptions(ctx, pkg.TenantID, []int64{pkg.ID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.buildPackageResponseWithStats(profile, pkg, promptCounts[pkg.ID], selfSubscriptions[pkg.ID])
|
||||
}
|
||||
|
||||
func (s *KolPackageService) buildPackageResponseWithStats(
|
||||
profile *repository.KolProfile,
|
||||
pkg *repository.KolPackage,
|
||||
promptCount int,
|
||||
selfSubscription *repository.KolSubscription,
|
||||
) (*KolPackageResponse, error) {
|
||||
tags, err := decodeKolStringList(pkg.Tags)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50068, "kol_package_decode_failed", err.Error())
|
||||
}
|
||||
|
||||
promptCount := 0
|
||||
if s.promptRepo != nil {
|
||||
prompts, err := s.promptRepo.ListByPackage(ctx, pkg.TenantID, pkg.ID)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50069, "kol_prompt_query_failed", err.Error())
|
||||
}
|
||||
promptCount = len(prompts)
|
||||
}
|
||||
|
||||
var selfSubscription *KolSubscriptionResponse
|
||||
if s.subRepo != nil {
|
||||
sub, err := s.subRepo.GetActiveForTenant(ctx, pkg.TenantID, pkg.ID)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50093, "kol_subscription_query_failed", err.Error())
|
||||
}
|
||||
selfSubscription = newKolSubscriptionResponse(sub)
|
||||
}
|
||||
|
||||
return &KolPackageResponse{
|
||||
ID: pkg.ID,
|
||||
KolProfileID: pkg.KolProfileID,
|
||||
@@ -337,12 +348,48 @@ func (s *KolPackageService) buildPackageResponse(
|
||||
Status: pkg.Status,
|
||||
PromptCount: promptCount,
|
||||
SubscriberCount: 0,
|
||||
SelfSubscription: selfSubscription,
|
||||
SelfSubscription: newKolSubscriptionResponse(selfSubscription),
|
||||
CreatedAt: pkg.CreatedAt,
|
||||
UpdatedAt: pkg.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *KolPackageService) packagePromptCounts(ctx context.Context, tenantID int64, packageIDs []int64) (map[int64]int, error) {
|
||||
if s.promptRepo == nil {
|
||||
return map[int64]int{}, nil
|
||||
}
|
||||
counts, err := s.promptRepo.CountByPackages(ctx, tenantID, packageIDs)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50069, "kol_prompt_query_failed", err.Error())
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func (s *KolPackageService) packageSelfSubscriptions(ctx context.Context, tenantID int64, packageIDs []int64) (map[int64]*repository.KolSubscription, error) {
|
||||
result := make(map[int64]*repository.KolSubscription, len(packageIDs))
|
||||
if s.subRepo == nil {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
subscriptions, err := s.subRepo.ListActiveForTenantByPackages(ctx, tenantID, packageIDs)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50093, "kol_subscription_query_failed", err.Error())
|
||||
}
|
||||
for packageID, sub := range subscriptions {
|
||||
subscription := sub
|
||||
result[packageID] = &subscription
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func kolPackageIDs(packages []repository.KolPackage) []int64 {
|
||||
ids := make([]int64, 0, len(packages))
|
||||
for _, pkg := range packages {
|
||||
ids = append(ids, pkg.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func normalizeKolOptionalString(value *string) *string {
|
||||
if value == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user