123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package infrastructure
- import (
- "code.osinet.fr/fgm/kurz/domain"
- "github.com/nicksnyder/go-i18n/v2/i18n"
- )
- type urlmap map[domain.URL]domain.URL
- var shortRepo = MemoryShortURLRepository{make(urlmap)}
- var targetRepo = MemoryTargetURLRepository{make(urlmap)}
- type MemoryShortURLRepository struct {
- targets urlmap
- }
- type MemoryTargetURLRepository struct {
- shorts urlmap
- }
- func (sr MemoryShortURLRepository) GetTarget(su domain.ShortURL, localizer *i18n.Localizer) (domain.TargetURL, error) {
- var tu domain.TargetURL
- var err error
- if t, ok := sr.targets[su.URL]; ok {
- tu = domain.TargetURL{URL: t}
- } else {
- err = domain.MakeError(nil, domain.ShortNotFound.ID, "")
- }
- return tu, err
- }
- func (tr MemoryTargetURLRepository) GetShort(tu domain.TargetURL) (su domain.ShortURL, isNew bool, err error) {
- // If short exists, just return it.
- if s, ok := tr.shorts[tu.URL]; ok {
- su = domain.ShortURL{URL: s}
- return su, false, err
- }
- // If it doesn't exist, attempt to create it.
- su, err = domain.NewUnspecifiedShortURL(tu)
- if err != nil {
- // Creation failed.
- return su, false, err
- }
- tr.shorts[tu.URL] = su.URL
- shortRepo.targets[su.URL] = tu.URL
- return su, true, err
- }
|