forked from short-d/short
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_fake.go
68 lines (57 loc) · 1.38 KB
/
url_fake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package repository
import (
"errors"
"github.com/short-d/short/app/entity"
)
var _ URL = (*URLFake)(nil)
// URLFake accesses URL information in url table through SQL.
type URLFake struct {
urls map[string]entity.URL
}
// IsAliasExist checks whether a given alias exist in url table.
func (u URLFake) IsAliasExist(alias string) (bool, error) {
_, ok := u.urls[alias]
return ok, nil
}
// Create inserts a new URL into url table.
func (u *URLFake) Create(url entity.URL) error {
isExist, err := u.IsAliasExist(url.Alias)
if err != nil {
return err
}
if isExist {
return errors.New("alias exists")
}
u.urls[url.Alias] = url
return nil
}
// GetByAlias finds an URL in url table given alias.
func (u URLFake) GetByAlias(alias string) (entity.URL, error) {
isExist, err := u.IsAliasExist(alias)
if err != nil {
return entity.URL{}, err
}
if !isExist {
return entity.URL{}, errors.New("alias not found")
}
url := u.urls[alias]
return url, nil
}
// GetByAliases finds all URL for a list of aliases
func (u URLFake) GetByAliases(aliases []string) ([]entity.URL, error) {
var urls []entity.URL
for _, alias := range aliases {
url, err := u.GetByAlias(alias)
if err != nil {
return urls, err
}
urls = append(urls, url)
}
return urls, nil
}
// NewURLFake creates in memory URL repository
func NewURLFake(urls map[string]entity.URL) URLFake {
return URLFake{
urls: urls,
}
}