feat(kol): repository layer for all KOL tables
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
||||
)
|
||||
|
||||
type KolProfile struct {
|
||||
ID int64
|
||||
TenantID int64
|
||||
UserID int64
|
||||
DisplayName string
|
||||
Bio *string
|
||||
AvatarURL *string
|
||||
MarketEnabled bool
|
||||
Status string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type UpdateKolProfileInput struct {
|
||||
ID int64
|
||||
DisplayName string
|
||||
Bio *string
|
||||
AvatarURL *string
|
||||
}
|
||||
|
||||
type KolProfileRepository interface {
|
||||
GetByUser(ctx context.Context, tenantID, userID int64) (*KolProfile, error)
|
||||
GetByID(ctx context.Context, id int64) (*KolProfile, error)
|
||||
Update(ctx context.Context, tenantID int64, input UpdateKolProfileInput) error
|
||||
}
|
||||
|
||||
type kolProfileRepository struct {
|
||||
q generated.Querier
|
||||
}
|
||||
|
||||
func NewKolProfileRepository(db generated.DBTX) KolProfileRepository {
|
||||
return &kolProfileRepository{q: newQuerier(db)}
|
||||
}
|
||||
|
||||
func (r *kolProfileRepository) GetByUser(ctx context.Context, tenantID, userID int64) (*KolProfile, error) {
|
||||
row, err := r.q.GetKolProfileByUser(ctx, generated.GetKolProfileByUserParams{
|
||||
UserID: userID,
|
||||
TenantID: tenantID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &KolProfile{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
UserID: row.UserID,
|
||||
DisplayName: row.DisplayName,
|
||||
Bio: nullableText(row.Bio),
|
||||
AvatarURL: nullableText(row.AvatarUrl),
|
||||
MarketEnabled: row.MarketEnabled,
|
||||
Status: row.Status,
|
||||
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
||||
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *kolProfileRepository) GetByID(ctx context.Context, id int64) (*KolProfile, error) {
|
||||
row, err := r.q.GetKolProfileByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &KolProfile{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
UserID: row.UserID,
|
||||
DisplayName: row.DisplayName,
|
||||
Bio: nullableText(row.Bio),
|
||||
AvatarURL: nullableText(row.AvatarUrl),
|
||||
MarketEnabled: row.MarketEnabled,
|
||||
Status: row.Status,
|
||||
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
||||
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *kolProfileRepository) Update(ctx context.Context, tenantID int64, input UpdateKolProfileInput) error {
|
||||
return r.q.UpdateKolProfile(ctx, generated.UpdateKolProfileParams{
|
||||
DisplayName: input.DisplayName,
|
||||
Bio: pgText(input.Bio),
|
||||
AvatarUrl: pgText(input.AvatarURL),
|
||||
ID: input.ID,
|
||||
TenantID: tenantID,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user