1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package infrastructure
- import (
- "code.osinet.fr/fgm/kurz/domain"
- )
- 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) (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(domain.ShortNotFound, "")
- }
- 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
- }
|