This commit is contained in:
@@ -240,6 +240,11 @@ func validateAdminPost(ap *adminPost) error {
|
||||
if ap.Status == "" {
|
||||
ap.Status = "draft"
|
||||
}
|
||||
normalized, err := normalizeAdminPostTags(ap.Tags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ap.Tags = normalized
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -284,40 +289,41 @@ func loadAttachmentsByPostIDs(ctx context.Context, pool *pgxpool.Pool, ids []uui
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func loadPostTagNames(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID) ([]string, error) {
|
||||
func loadPostTagNames(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID, lang string) ([]string, error) {
|
||||
rows, err := pool.Query(ctx, `
|
||||
SELECT t.name FROM post_tags pt JOIN tags t ON t.id = pt.tag_id WHERE pt.post_id = $1 ORDER BY t.name`, postID)
|
||||
SELECT t.name, COALESCE(t.name_en,''), COALESCE(t.name_ja,''), COALESCE(t.name_ko,''),
|
||||
COALESCE(t.name_vi,''), COALESCE(t.name_id,''), COALESCE(t.name_ms,''), t.slug
|
||||
FROM post_tags pt JOIN tags t ON t.id = pt.tag_id
|
||||
WHERE pt.post_id = $1 ORDER BY t.slug`, postID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var names []string
|
||||
for rows.Next() {
|
||||
var n string
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
var name, en, ja, ko, vi, id, ms, slug string
|
||||
if err := rows.Scan(&name, &en, &ja, &ko, &vi, &id, &ms, &slug); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names = append(names, n)
|
||||
names = append(names, tagDisplayName(name, en, ja, ko, vi, id, ms, lang))
|
||||
}
|
||||
return names, rows.Err()
|
||||
}
|
||||
|
||||
func replacePostTags(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID, tags []string) error {
|
||||
func replacePostTags(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID, tagSlugs []string) error {
|
||||
_, err := pool.Exec(ctx, `DELETE FROM post_tags WHERE post_id = $1`, postID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range tags {
|
||||
t = strings.TrimSpace(t)
|
||||
if t == "" {
|
||||
for _, slug := range tagSlugs {
|
||||
slug = strings.TrimSpace(slug)
|
||||
if slug == "" {
|
||||
continue
|
||||
}
|
||||
var tid int
|
||||
slug := tagSlug(t)
|
||||
err := pool.QueryRow(ctx, `INSERT INTO tags (name, slug) VALUES ($1, $2)
|
||||
ON CONFLICT (slug) DO UPDATE SET name = EXCLUDED.name RETURNING id`, t, slug).Scan(&tid)
|
||||
err := pool.QueryRow(ctx, `SELECT id FROM tags WHERE slug = $1`, slug).Scan(&tid)
|
||||
if err != nil {
|
||||
_ = pool.QueryRow(ctx, `SELECT id FROM tags WHERE slug = $1`, slug).Scan(&tid)
|
||||
continue
|
||||
}
|
||||
_, _ = pool.Exec(ctx, `INSERT INTO post_tags (post_id, tag_id) VALUES ($1,$2) ON CONFLICT DO NOTHING`, postID, tid)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user