feat: move infra to driver

This commit is contained in:
Elton Minetto
2020-10-12 08:02:35 -03:00
parent 3e4eb2e616
commit 34b252f708
11 changed files with 36 additions and 39 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"strconv"
"time"
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
"github.com/eminetto/clean-architecture-go-v2/driver/repository"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"log"
"os"
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
"github.com/eminetto/clean-architecture-go-v2/driver/repository"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/config"
+2 -2
View File
@@ -4,7 +4,7 @@ services:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password --init-file /data/application/init.sql
volumes:
- ./infra/init.sql:/data/application/init.sql
- ./ops/init.sql:/data/application/init.sql
environment:
MYSQL_ROOT_PASSWORD: clean_architecture_go_v2
MYSQL_DATABASE: clean_architecture_go_v2
@@ -30,7 +30,7 @@ services:
command:
- --config.file=/etc/prometheus/prometheus.yml
volumes:
- ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- ./ops/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
container_name: clean-architecture-go-v2-prometheus
network_mode: "bridge"
prometheus-pushgateway:
@@ -1,4 +1,4 @@
package repository
package book
import (
"strings"
@@ -6,27 +6,27 @@ import (
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//BookInmem in memory repo
type BookInmem struct {
//inmem in memory repo
type inmem struct {
m map[entity.ID]*entity.Book
}
//NewBookInmem create new repository
func NewBookInmem() *BookInmem {
//newInmem create new repository
func newInmem() *inmem {
var m = map[entity.ID]*entity.Book{}
return &BookInmem{
return &inmem{
m: m,
}
}
//Create a book
func (r *BookInmem) Create(e *entity.Book) (entity.ID, error) {
func (r *inmem) Create(e *entity.Book) (entity.ID, error) {
r.m[e.ID] = e
return e.ID, nil
}
//Get a book
func (r *BookInmem) Get(id entity.ID) (*entity.Book, error) {
func (r *inmem) Get(id entity.ID) (*entity.Book, error) {
if r.m[id] == nil {
return nil, entity.ErrNotFound
}
@@ -34,7 +34,7 @@ func (r *BookInmem) Get(id entity.ID) (*entity.Book, error) {
}
//Update a book
func (r *BookInmem) Update(e *entity.Book) error {
func (r *inmem) Update(e *entity.Book) error {
_, err := r.Get(e.ID)
if err != nil {
return err
@@ -44,7 +44,7 @@ func (r *BookInmem) Update(e *entity.Book) error {
}
//Search books
func (r *BookInmem) Search(query string) ([]*entity.Book, error) {
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) {
@@ -55,7 +55,7 @@ func (r *BookInmem) Search(query string) ([]*entity.Book, error) {
}
//List books
func (r *BookInmem) List() ([]*entity.Book, error) {
func (r *inmem) List() ([]*entity.Book, error) {
var d []*entity.Book
for _, j := range r.m {
d = append(d, j)
@@ -64,7 +64,7 @@ func (r *BookInmem) List() ([]*entity.Book, error) {
}
//Delete a book
func (r *BookInmem) Delete(id entity.ID) error {
func (r *inmem) Delete(id entity.ID) error {
if r.m[id] == nil {
return entity.ErrNotFound
}
+4 -5
View File
@@ -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()
@@ -1,4 +1,4 @@
package repository
package user
import (
"fmt"
@@ -7,27 +7,27 @@ import (
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//UserInmem in memory repo
type UserInmem struct {
//inmem in memory repo
type inmem struct {
m map[entity.ID]*entity.User
}
//NewUserInmem create new repository
func NewUserInmem() *UserInmem {
//newInmem create new repository
func newInmem() *inmem {
var m = map[entity.ID]*entity.User{}
return &UserInmem{
return &inmem{
m: m,
}
}
//Create an user
func (r *UserInmem) Create(e *entity.User) (entity.ID, error) {
func (r *inmem) Create(e *entity.User) (entity.ID, error) {
r.m[e.ID] = e
return e.ID, nil
}
//Get an user
func (r *UserInmem) Get(id entity.ID) (*entity.User, error) {
func (r *inmem) Get(id entity.ID) (*entity.User, error) {
if r.m[id] == nil {
return nil, entity.ErrNotFound
}
@@ -35,7 +35,7 @@ func (r *UserInmem) Get(id entity.ID) (*entity.User, error) {
}
//Update an user
func (r *UserInmem) Update(e *entity.User) error {
func (r *inmem) Update(e *entity.User) error {
_, err := r.Get(e.ID)
if err != nil {
return err
@@ -45,7 +45,7 @@ func (r *UserInmem) Update(e *entity.User) error {
}
//Search users
func (r *UserInmem) Search(query string) ([]*entity.User, error) {
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) {
@@ -60,7 +60,7 @@ func (r *UserInmem) Search(query string) ([]*entity.User, error) {
}
//List users
func (r *UserInmem) List() ([]*entity.User, error) {
func (r *inmem) List() ([]*entity.User, error) {
var d []*entity.User
for _, j := range r.m {
d = append(d, j)
@@ -69,7 +69,7 @@ func (r *UserInmem) List() ([]*entity.User, error) {
}
//Delete an user
func (r *UserInmem) Delete(id entity.ID) error {
func (r *inmem) Delete(id entity.ID) error {
if r.m[id] == nil {
return fmt.Errorf("not found")
}
+4 -6
View File
@@ -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()