package repository import ( "context" "errors" "time" "github.com/jackc/pgx/v5" "github.com/geo-platform/tenant-api/internal/tenant/repository/generated" ) type KolPackage struct { ID int64 TenantID int64 KolProfileID int64 Name string Description *string CoverURL *string Industry *string Tags []byte PriceNote *string Status string CreatedAt time.Time UpdatedAt time.Time } type CreateKolPackageInput struct { TenantID int64 KolProfileID int64 Name string Description *string CoverURL *string Industry *string Tags []byte PriceNote *string } type UpdateKolPackageInput struct { ID int64 Name string Description *string CoverURL *string Industry *string Tags []byte PriceNote *string } type KolPackageRepository interface { Create(ctx context.Context, input CreateKolPackageInput) (*KolPackage, error) GetByID(ctx context.Context, tenantID, id int64) (*KolPackage, error) ListByProfile(ctx context.Context, tenantID, profileID int64) ([]KolPackage, error) ListPackageIDsByProfile(ctx context.Context, tenantID, profileID int64) ([]int64, error) Update(ctx context.Context, tenantID int64, input UpdateKolPackageInput) error UpdateStatus(ctx context.Context, tenantID, id int64, status string) error SoftDelete(ctx context.Context, tenantID, id int64) error } type kolPackageRepository struct { q generated.Querier } func NewKolPackageRepository(db generated.DBTX) KolPackageRepository { return &kolPackageRepository{q: newQuerier(db)} } func (r *kolPackageRepository) Create(ctx context.Context, input CreateKolPackageInput) (*KolPackage, error) { row, err := r.q.CreateKolPackage(ctx, generated.CreateKolPackageParams{ TenantID: input.TenantID, KolProfileID: input.KolProfileID, Name: input.Name, Description: pgText(input.Description), CoverUrl: pgText(input.CoverURL), Industry: pgText(input.Industry), Tags: input.Tags, PriceNote: pgText(input.PriceNote), }) if err != nil { return nil, err } return &KolPackage{ ID: row.ID, TenantID: row.TenantID, KolProfileID: row.KolProfileID, Name: row.Name, Description: nullableText(row.Description), CoverURL: nullableText(row.CoverUrl), Industry: nullableText(row.Industry), Tags: row.Tags, PriceNote: nullableText(row.PriceNote), Status: row.Status, CreatedAt: timeFromTimestamp(row.CreatedAt), UpdatedAt: timeFromTimestamp(row.UpdatedAt), }, nil } func (r *kolPackageRepository) GetByID(ctx context.Context, tenantID, id int64) (*KolPackage, error) { row, err := r.q.GetKolPackageByID(ctx, generated.GetKolPackageByIDParams{ ID: id, TenantID: tenantID, }) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, nil } return nil, err } return &KolPackage{ ID: row.ID, TenantID: row.TenantID, KolProfileID: row.KolProfileID, Name: row.Name, Description: nullableText(row.Description), CoverURL: nullableText(row.CoverUrl), Industry: nullableText(row.Industry), Tags: row.Tags, PriceNote: nullableText(row.PriceNote), Status: row.Status, CreatedAt: timeFromTimestamp(row.CreatedAt), UpdatedAt: timeFromTimestamp(row.UpdatedAt), }, nil } func (r *kolPackageRepository) ListByProfile(ctx context.Context, tenantID, profileID int64) ([]KolPackage, error) { rows, err := r.q.ListKolPackagesByProfile(ctx, generated.ListKolPackagesByProfileParams{ KolProfileID: profileID, TenantID: tenantID, }) if err != nil { return nil, err } result := make([]KolPackage, len(rows)) for i, row := range rows { result[i] = KolPackage{ ID: row.ID, TenantID: row.TenantID, KolProfileID: row.KolProfileID, Name: row.Name, Description: nullableText(row.Description), CoverURL: nullableText(row.CoverUrl), Industry: nullableText(row.Industry), Tags: row.Tags, PriceNote: nullableText(row.PriceNote), Status: row.Status, CreatedAt: timeFromTimestamp(row.CreatedAt), UpdatedAt: timeFromTimestamp(row.UpdatedAt), } } return result, nil } func (r *kolPackageRepository) ListPackageIDsByProfile(ctx context.Context, tenantID, profileID int64) ([]int64, error) { ids, err := r.q.ListKolPackageIDsByProfile(ctx, generated.ListKolPackageIDsByProfileParams{ KolProfileID: profileID, TenantID: tenantID, }) if err != nil { return nil, err } if ids == nil { return []int64{}, nil } return ids, nil } func (r *kolPackageRepository) Update(ctx context.Context, tenantID int64, input UpdateKolPackageInput) error { return r.q.UpdateKolPackage(ctx, generated.UpdateKolPackageParams{ Name: input.Name, Description: pgText(input.Description), CoverUrl: pgText(input.CoverURL), Industry: pgText(input.Industry), Tags: input.Tags, PriceNote: pgText(input.PriceNote), ID: input.ID, TenantID: tenantID, }) } func (r *kolPackageRepository) UpdateStatus(ctx context.Context, tenantID, id int64, status string) error { return r.q.UpdateKolPackageStatus(ctx, generated.UpdateKolPackageStatusParams{ Status: status, ID: id, TenantID: tenantID, }) } func (r *kolPackageRepository) SoftDelete(ctx context.Context, tenantID, id int64) error { return r.q.SoftDeleteKolPackage(ctx, generated.SoftDeleteKolPackageParams{ ID: id, TenantID: tenantID, }) }