33 lines
1.3 KiB
Go
33 lines
1.3 KiB
Go
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
|
||
}
|