dana-medical-site/internal/storage/public_paths.go
2026-10-14 13:33:13 +00:00

33 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package storage
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
// Текущие адреса записей по id — чтобы при смене адреса в админке оставить редирект со старого.
// Возвращают "", если запись не опубликована: у скрытой записи нет адреса, который стоит сохранять.
func (s *Storage) PublishedPostSlug(ctx context.Context, kind string, id int64) (string, error) {
return s.publishedSlug(ctx, `SELECT slug FROM posts WHERE id=$1 AND kind=$2 AND published AND deleted_at IS NULL`, id, kind)
}
func (s *Storage) PublishedCheckupSlug(ctx context.Context, id int64) (string, error) {
return s.publishedSlug(ctx, `SELECT slug FROM checkups WHERE id=$1 AND published AND deleted_at IS NULL`, id)
}
func (s *Storage) PublishedDoctorSlug(ctx context.Context, id int64) (string, error) {
return s.publishedSlug(ctx, `SELECT slug FROM doctors WHERE id=$1 AND published AND deleted_at IS NULL`, id)
}
func (s *Storage) publishedSlug(ctx context.Context, sql string, args ...any) (string, error) {
var slug string
err := s.pool.QueryRow(ctx, sql, args...).Scan(&slug)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
return slug, err
}