feat: scope articles by brand
This commit is contained in:
@@ -11,13 +11,18 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const countArticlesByTenant = `-- name: CountArticlesByTenant :one
|
||||
const countArticlesByTenantAndBrand = `-- name: CountArticlesByTenantAndBrand :one
|
||||
SELECT COUNT(*) FROM articles
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL
|
||||
WHERE tenant_id = $1 AND brand_id = $2 AND deleted_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) CountArticlesByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countArticlesByTenant, tenantID)
|
||||
type CountArticlesByTenantAndBrandParams struct {
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
BrandID int64 `json:"brand_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountArticlesByTenantAndBrand(ctx context.Context, arg CountArticlesByTenantAndBrandParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countArticlesByTenantAndBrand, arg.TenantID, arg.BrandID)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
@@ -35,13 +40,18 @@ func (q *Queries) CountBrandsByTenant(ctx context.Context, tenantID int64) (int6
|
||||
return count, err
|
||||
}
|
||||
|
||||
const countPublishedArticlesByTenant = `-- name: CountPublishedArticlesByTenant :one
|
||||
const countPublishedArticlesByTenantAndBrand = `-- name: CountPublishedArticlesByTenantAndBrand :one
|
||||
SELECT COUNT(*) FROM articles
|
||||
WHERE tenant_id = $1 AND publish_status = 'success' AND deleted_at IS NULL
|
||||
WHERE tenant_id = $1 AND brand_id = $2 AND publish_status = 'success' AND deleted_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) CountPublishedArticlesByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countPublishedArticlesByTenant, tenantID)
|
||||
type CountPublishedArticlesByTenantAndBrandParams struct {
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
BrandID int64 `json:"brand_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountPublishedArticlesByTenantAndBrand(ctx context.Context, arg CountPublishedArticlesByTenantAndBrandParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countPublishedArticlesByTenantAndBrand, arg.TenantID, arg.BrandID)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
@@ -93,8 +103,8 @@ func (q *Queries) GetQuotaSummary(ctx context.Context, tenantID int64) (int32, e
|
||||
return balance, err
|
||||
}
|
||||
|
||||
const getRecentArticles = `-- name: GetRecentArticles :many
|
||||
SELECT a.id, a.generate_status, a.publish_status, a.source_type, a.created_at,
|
||||
const getRecentArticlesByBrand = `-- name: GetRecentArticlesByBrand :many
|
||||
SELECT a.id, a.brand_id, a.generate_status, a.publish_status, a.source_type, a.created_at,
|
||||
av.title, av.markdown_content, av.word_count, av.source_label,
|
||||
t.template_name,
|
||||
COALESCE(
|
||||
@@ -111,13 +121,19 @@ LEFT JOIN LATERAL (
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
) gt ON true
|
||||
WHERE a.tenant_id = $1 AND a.deleted_at IS NULL
|
||||
WHERE a.tenant_id = $1 AND a.brand_id = $2 AND a.deleted_at IS NULL
|
||||
ORDER BY a.created_at DESC
|
||||
LIMIT 10
|
||||
`
|
||||
|
||||
type GetRecentArticlesRow struct {
|
||||
type GetRecentArticlesByBrandParams struct {
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
BrandID int64 `json:"brand_id"`
|
||||
}
|
||||
|
||||
type GetRecentArticlesByBrandRow struct {
|
||||
ID int64 `json:"id"`
|
||||
BrandID int64 `json:"brand_id"`
|
||||
GenerateStatus string `json:"generate_status"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
SourceType string `json:"source_type"`
|
||||
@@ -130,17 +146,18 @@ type GetRecentArticlesRow struct {
|
||||
GenerationMode interface{} `json:"generation_mode"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetRecentArticles(ctx context.Context, tenantID int64) ([]GetRecentArticlesRow, error) {
|
||||
rows, err := q.db.Query(ctx, getRecentArticles, tenantID)
|
||||
func (q *Queries) GetRecentArticlesByBrand(ctx context.Context, arg GetRecentArticlesByBrandParams) ([]GetRecentArticlesByBrandRow, error) {
|
||||
rows, err := q.db.Query(ctx, getRecentArticlesByBrand, arg.TenantID, arg.BrandID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetRecentArticlesRow
|
||||
var items []GetRecentArticlesByBrandRow
|
||||
for rows.Next() {
|
||||
var i GetRecentArticlesRow
|
||||
var i GetRecentArticlesByBrandRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.BrandID,
|
||||
&i.GenerateStatus,
|
||||
&i.PublishStatus,
|
||||
&i.SourceType,
|
||||
|
||||
Reference in New Issue
Block a user