domain_api.go 718 B

1234567891011121314151617181920212223242526272829303132
  1. package domain
  2. import (
  3. "github.com/nicksnyder/go-i18n/v2/i18n"
  4. )
  5. func GetTargetURL(shortURL string, localizer *i18n.Localizer) (target string, err error) {
  6. su := ShortURL{URL: URL(shortURL)}
  7. tu, err := shortURLRepository.GetTarget(su, localizer)
  8. if err != nil {
  9. target = ""
  10. } else {
  11. target = string(tu.URL)
  12. }
  13. return
  14. }
  15. func GetShortURL(targetURL string, localizer *i18n.Localizer) (short string, isNew bool, err error) {
  16. tu := TargetURL{URL: URL(targetURL)}
  17. if tu.IsEmpty() {
  18. // Zero values are OK for short and isNew.
  19. err = MakeError(localizer, TargetInvalid.ID, "empty URL")
  20. return
  21. }
  22. su, isNew, err := targetURLRepository.GetShort(tu)
  23. if err == nil {
  24. short = string(su.URL)
  25. }
  26. return
  27. }