GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
939 B

  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "git.aiterp.net/rpdata/api/database/postgres/psqlcore"
  7. "git.aiterp.net/rpdata/api/models"
  8. )
  9. type tagRepository struct {
  10. db *sql.DB
  11. }
  12. func (r *tagRepository) Find(ctx context.Context, kind models.TagKind, name string) (*models.Tag, error) {
  13. tag, err := psqlcore.New(r.db).SelectTagsByTag(ctx, fmt.Sprintf("%s:%s", kind, name))
  14. if err != nil {
  15. return nil, err
  16. }
  17. tag2 := &models.Tag{}
  18. err = tag2.Decode(tag[0].Tag)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return tag2, nil
  23. }
  24. func (r *tagRepository) List(ctx context.Context, filter models.TagFilter) ([]*models.Tag, error) {
  25. var tags []string
  26. var err error
  27. if filter.Kind != nil {
  28. tags, err = psqlcore.New(r.db).SelectDistinctTagsLike(ctx, string(*filter.Kind)+"%")
  29. } else {
  30. tags, err = psqlcore.New(r.db).SelectDistinctTags(ctx)
  31. }
  32. if err != nil {
  33. return nil, err
  34. }
  35. return models.DecodeTagArray(tags)
  36. }