feat: move infra to driver
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package book
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||
)
|
||||
|
||||
//inmem in memory repo
|
||||
type inmem struct {
|
||||
m map[entity.ID]*entity.Book
|
||||
}
|
||||
|
||||
//newInmem create new repository
|
||||
func newInmem() *inmem {
|
||||
var m = map[entity.ID]*entity.Book{}
|
||||
return &inmem{
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
//Create a book
|
||||
func (r *inmem) Create(e *entity.Book) (entity.ID, error) {
|
||||
r.m[e.ID] = e
|
||||
return e.ID, nil
|
||||
}
|
||||
|
||||
//Get a book
|
||||
func (r *inmem) Get(id entity.ID) (*entity.Book, error) {
|
||||
if r.m[id] == nil {
|
||||
return nil, entity.ErrNotFound
|
||||
}
|
||||
return r.m[id], nil
|
||||
}
|
||||
|
||||
//Update a book
|
||||
func (r *inmem) Update(e *entity.Book) error {
|
||||
_, err := r.Get(e.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.m[e.ID] = e
|
||||
return nil
|
||||
}
|
||||
|
||||
//Search books
|
||||
func (r *inmem) Search(query string) ([]*entity.Book, error) {
|
||||
var d []*entity.Book
|
||||
for _, j := range r.m {
|
||||
if strings.Contains(strings.ToLower(j.Title), query) {
|
||||
d = append(d, j)
|
||||
}
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
//List books
|
||||
func (r *inmem) List() ([]*entity.Book, error) {
|
||||
var d []*entity.Book
|
||||
for _, j := range r.m {
|
||||
d = append(d, j)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
//Delete a book
|
||||
func (r *inmem) Delete(id entity.ID) error {
|
||||
if r.m[id] == nil {
|
||||
return entity.ErrNotFound
|
||||
}
|
||||
r.m[id] = nil
|
||||
return nil
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -21,7 +20,7 @@ func newFixtureBook() *entity.Book {
|
||||
}
|
||||
|
||||
func Test_Create(t *testing.T) {
|
||||
repo := repository.NewBookInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u := newFixtureBook()
|
||||
_, err := m.CreateBook(u.Title, u.Author, u.Pages, u.Quantity)
|
||||
@@ -30,7 +29,7 @@ func Test_Create(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_SearchAndFind(t *testing.T) {
|
||||
repo := repository.NewBookInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u1 := newFixtureBook()
|
||||
u2 := newFixtureBook()
|
||||
@@ -63,7 +62,7 @@ func Test_SearchAndFind(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Update(t *testing.T) {
|
||||
repo := repository.NewBookInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u := newFixtureBook()
|
||||
id, err := m.CreateBook(u.Title, u.Author, u.Pages, u.Quantity)
|
||||
@@ -77,7 +76,7 @@ func Test_Update(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
repo := repository.NewBookInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u1 := newFixtureBook()
|
||||
u2 := newFixtureBook()
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||
)
|
||||
|
||||
//inmem in memory repo
|
||||
type inmem struct {
|
||||
m map[entity.ID]*entity.User
|
||||
}
|
||||
|
||||
//newInmem create new repository
|
||||
func newInmem() *inmem {
|
||||
var m = map[entity.ID]*entity.User{}
|
||||
return &inmem{
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
//Create an user
|
||||
func (r *inmem) Create(e *entity.User) (entity.ID, error) {
|
||||
r.m[e.ID] = e
|
||||
return e.ID, nil
|
||||
}
|
||||
|
||||
//Get an user
|
||||
func (r *inmem) Get(id entity.ID) (*entity.User, error) {
|
||||
if r.m[id] == nil {
|
||||
return nil, entity.ErrNotFound
|
||||
}
|
||||
return r.m[id], nil
|
||||
}
|
||||
|
||||
//Update an user
|
||||
func (r *inmem) Update(e *entity.User) error {
|
||||
_, err := r.Get(e.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.m[e.ID] = e
|
||||
return nil
|
||||
}
|
||||
|
||||
//Search users
|
||||
func (r *inmem) Search(query string) ([]*entity.User, error) {
|
||||
var d []*entity.User
|
||||
for _, j := range r.m {
|
||||
if strings.Contains(strings.ToLower(j.FirstName), query) {
|
||||
d = append(d, j)
|
||||
}
|
||||
}
|
||||
if len(d) == 0 {
|
||||
return nil, entity.ErrNotFound
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
//List users
|
||||
func (r *inmem) List() ([]*entity.User, error) {
|
||||
var d []*entity.User
|
||||
for _, j := range r.m {
|
||||
d = append(d, j)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
//Delete an user
|
||||
func (r *inmem) Delete(id entity.ID) error {
|
||||
if r.m[id] == nil {
|
||||
return fmt.Errorf("not found")
|
||||
}
|
||||
r.m[id] = nil
|
||||
return nil
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -23,7 +21,7 @@ func newFixtureUser() *entity.User {
|
||||
}
|
||||
|
||||
func Test_Create(t *testing.T) {
|
||||
repo := repository.NewUserInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u := newFixtureUser()
|
||||
_, err := m.CreateUser(u.Email, u.Password, u.FirstName, u.LastName)
|
||||
@@ -33,7 +31,7 @@ func Test_Create(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_SearchAndFind(t *testing.T) {
|
||||
repo := repository.NewUserInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u1 := newFixtureUser()
|
||||
u2 := newFixtureUser()
|
||||
@@ -66,7 +64,7 @@ func Test_SearchAndFind(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Update(t *testing.T) {
|
||||
repo := repository.NewUserInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u := newFixtureUser()
|
||||
id, err := m.CreateUser(u.Email, u.Password, u.FirstName, u.LastName)
|
||||
@@ -83,7 +81,7 @@ func Test_Update(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
repo := repository.NewUserInmem()
|
||||
repo := newInmem()
|
||||
m := NewService(repo)
|
||||
u1 := newFixtureUser()
|
||||
u2 := newFixtureUser()
|
||||
|
||||
Reference in New Issue
Block a user