123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package repositories
- import (
- "context"
- "fmt"
- sq "github.com/Masterminds/squirrel"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
- )
- const tagTableName = "tag"
- type TagRepository struct {
- db DB
- }
- func InitTagRepository(db DB) *TagRepository {
- return &TagRepository{db: db}
- }
- func (t TagRepository) GetAllUsed(
- ctx context.Context,
- lang models.Language,
- ) ([]models.Tag, error) {
- var res []models.Tag
- q := "SELECT t.id, t.url, t.tag " +
- "FROM " + articleTagTableName + " at, " + tagTableName + " t " +
- "WHERE t.id = at.tag_id AND at.article_id IN " +
- "(SELECT id FROM " + articleTableName + " " + "WHERE is_active = true AND language = ?) " +
- "GROUP BY t.id"
- err := t.db.SelectContext(ctx, &res, q, lang)
- if err != nil {
- return nil, fmt.Errorf("select: %w", err)
- }
- return res, nil
- }
- func (t TagRepository) GetByURL(ctx context.Context, url string) (*models.Tag, error) {
- var res models.Tag
- q, v, err := sq.Select("id", "url", "tag").
- From(tagTableName).
- PlaceholderFormat(sq.Dollar).
- Where(sq.Eq{"url": url}).
- Limit(1).
- ToSql()
- if err != nil {
- return nil, fmt.Errorf("build query: %w", err)
- }
- err = t.db.GetContext(ctx, &res, q, v...)
- if err != nil {
- return nil, fmt.Errorf("get: %w", err)
- }
- return &res, nil
- }
- func (t TagRepository) GetByID(ctx context.Context, id uint64) (*models.Tag, error) {
- var res models.Tag
- q, v, err := sq.Select("id", "url", "tag").
- From(tagTableName).
- PlaceholderFormat(sq.Dollar).
- Where(sq.Eq{"id": id}).
- Limit(1).
- ToSql()
- if err != nil {
- return nil, fmt.Errorf("build query: %w", err)
- }
- err = t.db.GetContext(ctx, &res, q, v...)
- if err != nil {
- return nil, fmt.Errorf("get: %w", err)
- }
- return &res, nil
- }
- func (t TagRepository) GetAll(ctx context.Context) ([]models.Tag, error) {
- var res []models.Tag
- q, v, err := sq.Select("id", "url", "tag").
- From(tagTableName).
- ToSql()
- if err != nil {
- return nil, fmt.Errorf("build query: %w", err)
- }
- err = t.db.SelectContext(ctx, &res, q, v...)
- if err != nil {
- return nil, fmt.Errorf("select: %w", err)
- }
- return res, nil
- }
- func (t TagRepository) GetByArticleID(ctx context.Context, id uint64) ([]models.Tag, error) {
- var res []models.Tag
- q := "SELECT t.id, t.url, t.tag " +
- "FROM " + articleTagTableName + " at, " + tagTableName + " t " +
- "WHERE t.id = at.tag_id AND at.article_id = $1"
- err := t.db.SelectContext(ctx, &res, q, id)
- if err != nil {
- return nil, fmt.Errorf("select: %w", err)
- }
- return res, nil
- }
- func (t TagRepository) IsUsed(ctx context.Context, id uint64) (bool, error) {
- query, args, err := sq.Select("COUNT(tag_id)").
- From(articleTagTableName).
- PlaceholderFormat(sq.Dollar).
- Where(sq.Eq{"tag_id": id}).
- ToSql()
- if err != nil {
- return false, fmt.Errorf("build query: %w", err)
- }
- var count int
- err = t.db.QueryRowContext(ctx, query, args...).Scan(&count)
- if err != nil {
- return false, err
- }
- return count > 0, nil
- }
- func (t TagRepository) Add(ctx context.Context, req models.Tag) error {
- q, v, err := sq.Insert(tagTableName).
- PlaceholderFormat(sq.Dollar).
- Columns("tag", "url").
- Values(req.Tag, req.URL).
- ToSql()
- if err != nil {
- return fmt.Errorf("build query: %w", err)
- }
- _, err = t.db.ExecContext(ctx, q, v...)
- if err != nil {
- return fmt.Errorf("exec: %w", err)
- }
- return nil
- }
- func (t TagRepository) Update(ctx context.Context, req models.Tag) error {
- q, v, err := sq.Update(tagTableName).
- PlaceholderFormat(sq.Dollar).
- Set("tag", req.Tag).
- Set("url", req.URL).
- Where(sq.Eq{"id": req.ID}).
- ToSql()
- if err != nil {
- return fmt.Errorf("build query: %w", err)
- }
- _, err = t.db.ExecContext(ctx, q, v...)
- if err != nil {
- return fmt.Errorf("exec: %w", err)
- }
- return nil
- }
- func (t TagRepository) Delete(ctx context.Context, id uint64) error {
- q, v, err := sq.Delete(tagTableName).
- PlaceholderFormat(sq.Dollar).
- Where(sq.Eq{"id": id}).
- ToSql()
- if err != nil {
- return fmt.Errorf("build query: %w", err)
- }
- _, err = t.db.ExecContext(ctx, q, v...)
- if err != nil {
- return fmt.Errorf("exec: %w", err)
- }
- return nil
- }
|