运营横幅目录(ops_banners + admin /cms),锁定 024–040 全队列。 Co-authored-by: Cursor <cursoragent@cursor.com>
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// BannerRow is OpsCMS Banner catalog row.
|
|
type BannerRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Code string `json:"code"`
|
|
Title string `json:"title"`
|
|
Placement string `json:"placement"`
|
|
ImageURL *string `json:"image_url,omitempty"`
|
|
LinkPath *string `json:"link_path,omitempty"`
|
|
SortOrder int `json:"sort_order"`
|
|
Active bool `json:"active"`
|
|
System bool `json:"system"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// ListBanners returns banner catalog.
|
|
func (r *AdminRepo) ListBanners(ctx context.Context) ([]BannerRow, error) {
|
|
rows, err := r.Pool.Query(ctx, `
|
|
SELECT id, code, title, placement, image_url, link_path, sort_order, active, system, updated_at
|
|
FROM ops_banners
|
|
ORDER BY active DESC, sort_order ASC, code ASC`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []BannerRow
|
|
for rows.Next() {
|
|
var b BannerRow
|
|
if err := rows.Scan(
|
|
&b.ID, &b.Code, &b.Title, &b.Placement, &b.ImageURL, &b.LinkPath,
|
|
&b.SortOrder, &b.Active, &b.System, &b.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, b)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// GetBanner loads one banner by id.
|
|
func (r *AdminRepo) GetBanner(ctx context.Context, id uuid.UUID) (*BannerRow, error) {
|
|
var b BannerRow
|
|
err := r.Pool.QueryRow(ctx, `
|
|
SELECT id, code, title, placement, image_url, link_path, sort_order, active, system, updated_at
|
|
FROM ops_banners WHERE id=$1`, id,
|
|
).Scan(
|
|
&b.ID, &b.Code, &b.Title, &b.Placement, &b.ImageURL, &b.LinkPath,
|
|
&b.SortOrder, &b.Active, &b.System, &b.UpdatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &b, nil
|
|
}
|