This commit is contained in:
parent
c35fe67be9
commit
1c88d31dc2
|
@ -8,8 +8,7 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Repository interface {
|
Repository interface {
|
||||||
Create(ctx context.Context, client domain.Client) error
|
Fetch(ctx context.Context, cid domain.ClientID) (*domain.Client, error)
|
||||||
Get(ctx context.Context, cid domain.ClientID) (*domain.Client, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dummyClientRepository struct{}
|
dummyClientRepository struct{}
|
||||||
|
@ -32,11 +31,7 @@ func NewDummyClientRepository() Repository {
|
||||||
return dummyClientRepository{}
|
return dummyClientRepository{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dummyClientRepository) Create(_ context.Context, _ domain.Client) error {
|
func (dummyClientRepository) Fetch(_ context.Context, _ domain.ClientID) (*domain.Client, error) {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dummyClientRepository) Get(_ context.Context, _ domain.ClientID) (*domain.Client, error) {
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,10 +44,6 @@ func NewStubClientRepository(client *domain.Client, err error) Repository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *stubClientRepository) Create(_ context.Context, _ domain.Client) error {
|
func (repo *stubClientRepository) Fetch(_ context.Context, _ domain.ClientID) (*domain.Client, error) {
|
||||||
return repo.error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *stubClientRepository) Get(_ context.Context, _ domain.ClientID) (*domain.Client, error) {
|
|
||||||
return repo.client, repo.error
|
return repo.client, repo.error
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,12 +55,7 @@ func NewHTTPClientRepository(c *http.Client) client.Repository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARN(toby3d): not implemented.
|
func (repo *httpClientRepository) Fetch(ctx context.Context, cid domain.ClientID) (*domain.Client, error) {
|
||||||
func (httpClientRepository) Create(_ context.Context, _ domain.Client) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo httpClientRepository) Get(ctx context.Context, cid domain.ClientID) (*domain.Client, error) {
|
|
||||||
out := &domain.Client{
|
out := &domain.Client{
|
||||||
ID: cid,
|
ID: cid,
|
||||||
RedirectURI: make([]*url.URL, 0),
|
RedirectURI: make([]*url.URL, 0),
|
||||||
|
|
|
@ -44,7 +44,7 @@ func TestGet(t *testing.T) {
|
||||||
client.ID = *domain.TestClientID(t, srv.URL+"/")
|
client.ID = *domain.TestClientID(t, srv.URL+"/")
|
||||||
clients := repository.NewHTTPClientRepository(srv.Client())
|
clients := repository.NewHTTPClientRepository(srv.Client())
|
||||||
|
|
||||||
result, err := clients.Get(context.Background(), client.ID)
|
result, err := clients.Fetch(context.Background(), client.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
package memory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"source.toby3d.me/toby3d/auth/internal/client"
|
|
||||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
type memoryClientRepository struct {
|
|
||||||
mutex *sync.RWMutex
|
|
||||||
clients map[string]domain.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMemoryClientRepository() client.Repository {
|
|
||||||
return &memoryClientRepository{
|
|
||||||
mutex: new(sync.RWMutex),
|
|
||||||
clients: make(map[string]domain.Client),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo memoryClientRepository) Create(ctx context.Context, client domain.Client) error {
|
|
||||||
repo.mutex.RLock()
|
|
||||||
defer repo.mutex.RUnlock()
|
|
||||||
|
|
||||||
repo.clients[client.ID.String()] = client
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo memoryClientRepository) Get(ctx context.Context, cid domain.ClientID) (*domain.Client, error) {
|
|
||||||
repo.mutex.RLock()
|
|
||||||
defer repo.mutex.RUnlock()
|
|
||||||
|
|
||||||
if c, ok := repo.clients[cid.String()]; ok {
|
|
||||||
return &c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, client.ErrNotExist
|
|
||||||
}
|
|
|
@ -19,7 +19,7 @@ func NewClientUseCase(repo client.Repository) client.UseCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (useCase *clientUseCase) Discovery(ctx context.Context, id domain.ClientID) (*domain.Client, error) {
|
func (useCase *clientUseCase) Discovery(ctx context.Context, id domain.ClientID) (*domain.Client, error) {
|
||||||
c, err := useCase.repo.Get(ctx, id)
|
c, err := useCase.repo.Fetch(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot discovery client by id: %w", err)
|
return nil, fmt.Errorf("cannot discovery client by id: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user