language.go 881 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package repositories
  2. import (
  3. "context"
  4. sq "github.com/Masterminds/squirrel"
  5. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers/errors"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
  7. )
  8. const languageTableName = "language"
  9. var languageTableColumns = []string{
  10. "id",
  11. "url",
  12. "title",
  13. "is_default",
  14. }
  15. type LanguageRepository struct {
  16. db DB
  17. }
  18. func InitLanguageRepository(db DB) *LanguageRepository {
  19. return &LanguageRepository{db: db}
  20. }
  21. func (a LanguageRepository) GetAll(
  22. ctx context.Context,
  23. ) ([]models.Language, error) {
  24. var res []models.Language
  25. q, v, err := sq.Select(languageTableColumns...).
  26. From(languageTableName).
  27. ToSql()
  28. if err != nil {
  29. return nil, errors.Wrap(err, "build query")
  30. }
  31. err = a.db.SelectContext(ctx, &res, q, v...)
  32. if err != nil {
  33. return nil, errors.Wrap(err, "select")
  34. }
  35. return res, nil
  36. }