feat: improve repository structure
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package book
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -7,27 +7,27 @@ import (
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//InmemRepo in memory repo
|
||||
type InmemRepo struct {
|
||||
//BookInmem in memory repo
|
||||
type BookInmem struct {
|
||||
m map[entity.ID]*entity.Book
|
||||
}
|
||||
|
||||
//NewInmemRepository create new repository
|
||||
func NewInmemRepository() *InmemRepo {
|
||||
//NewBookInmem create new repository
|
||||
func NewBookInmem() *BookInmem {
|
||||
var m = map[entity.ID]*entity.Book{}
|
||||
return &InmemRepo{
|
||||
return &BookInmem{
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
//Create a book
|
||||
func (r *InmemRepo) Create(e *entity.Book) (entity.ID, error) {
|
||||
func (r *BookInmem) Create(e *entity.Book) (entity.ID, error) {
|
||||
r.m[e.ID] = e
|
||||
return e.ID, nil
|
||||
}
|
||||
|
||||
//Get a book
|
||||
func (r *InmemRepo) Get(id entity.ID) (*entity.Book, error) {
|
||||
func (r *BookInmem) Get(id entity.ID) (*entity.Book, error) {
|
||||
if r.m[id] == nil {
|
||||
// return nil, fmt.Errorf("not found")
|
||||
return nil, domain.ErrNotFound
|
||||
@@ -36,7 +36,7 @@ func (r *InmemRepo) Get(id entity.ID) (*entity.Book, error) {
|
||||
}
|
||||
|
||||
//Update a book
|
||||
func (r *InmemRepo) Update(e *entity.Book) error {
|
||||
func (r *BookInmem) Update(e *entity.Book) error {
|
||||
_, err := r.Get(e.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -46,7 +46,7 @@ func (r *InmemRepo) Update(e *entity.Book) error {
|
||||
}
|
||||
|
||||
//Search books
|
||||
func (r *InmemRepo) Search(query string) ([]*entity.Book, error) {
|
||||
func (r *BookInmem) Search(query string) ([]*entity.Book, error) {
|
||||
var d []*entity.Book
|
||||
for _, j := range r.m {
|
||||
if strings.Contains(strings.ToLower(j.Title), query) {
|
||||
@@ -57,7 +57,7 @@ func (r *InmemRepo) Search(query string) ([]*entity.Book, error) {
|
||||
}
|
||||
|
||||
//List books
|
||||
func (r *InmemRepo) List() ([]*entity.Book, error) {
|
||||
func (r *BookInmem) List() ([]*entity.Book, error) {
|
||||
var d []*entity.Book
|
||||
for _, j := range r.m {
|
||||
d = append(d, j)
|
||||
@@ -66,7 +66,7 @@ func (r *InmemRepo) List() ([]*entity.Book, error) {
|
||||
}
|
||||
|
||||
//Delete a book
|
||||
func (r *InmemRepo) Delete(id entity.ID) error {
|
||||
func (r *BookInmem) Delete(id entity.ID) error {
|
||||
if r.m[id] == nil {
|
||||
// return fmt.Errorf("not found")
|
||||
return domain.ErrNotFound
|
||||
@@ -1,4 +1,4 @@
|
||||
package book
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@@ -7,20 +7,20 @@ import (
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//MySQLRepo mysql repo
|
||||
type MySQLRepo struct {
|
||||
//BookMySQL mysql repo
|
||||
type BookMySQL struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
//NewMySQLRepository create new repository
|
||||
func NewMySQLRepository(db *sql.DB) *MySQLRepo {
|
||||
return &MySQLRepo{
|
||||
//NewBookMySQL create new repository
|
||||
func NewBookMySQL(db *sql.DB) *BookMySQL {
|
||||
return &BookMySQL{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
//Create a book
|
||||
func (r *MySQLRepo) Create(e *entity.Book) (entity.ID, error) {
|
||||
func (r *BookMySQL) Create(e *entity.Book) (entity.ID, error) {
|
||||
stmt, err := r.db.Prepare(`
|
||||
insert into book (id, title, author, pages, quantity, created_at)
|
||||
values(?,?,?,?,?,?)`)
|
||||
@@ -46,7 +46,7 @@ func (r *MySQLRepo) Create(e *entity.Book) (entity.ID, error) {
|
||||
}
|
||||
|
||||
//Get a book
|
||||
func (r *MySQLRepo) Get(id entity.ID) (*entity.Book, error) {
|
||||
func (r *BookMySQL) Get(id entity.ID) (*entity.Book, error) {
|
||||
stmt, err := r.db.Prepare(`select id, title, author, pages, quantity, created_at from book where id = ?`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -63,7 +63,7 @@ func (r *MySQLRepo) Get(id entity.ID) (*entity.Book, error) {
|
||||
}
|
||||
|
||||
//Update a book
|
||||
func (r *MySQLRepo) Update(e *entity.Book) error {
|
||||
func (r *BookMySQL) Update(e *entity.Book) error {
|
||||
e.UpdatedAt = time.Now()
|
||||
_, err := r.db.Exec("update book set title = ?, author = ?, pages = ?, quantity = ?, updated_at = ? where id = ?", e.Title, e.Author, e.Pages, e.Quantity, e.UpdatedAt.Format("2006-01-02"), e.ID)
|
||||
if err != nil {
|
||||
@@ -73,7 +73,7 @@ func (r *MySQLRepo) Update(e *entity.Book) error {
|
||||
}
|
||||
|
||||
//Search books
|
||||
func (r *MySQLRepo) Search(query string) ([]*entity.Book, error) {
|
||||
func (r *BookMySQL) Search(query string) ([]*entity.Book, error) {
|
||||
stmt, err := r.db.Prepare(`select id, title, author, pages, quantity, created_at from book where title like ?`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -96,7 +96,7 @@ func (r *MySQLRepo) Search(query string) ([]*entity.Book, error) {
|
||||
}
|
||||
|
||||
//List books
|
||||
func (r *MySQLRepo) List() ([]*entity.Book, error) {
|
||||
func (r *BookMySQL) List() ([]*entity.Book, error) {
|
||||
stmt, err := r.db.Prepare(`select id, title, author, pages, quantity, created_at from book`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -118,7 +118,7 @@ func (r *MySQLRepo) List() ([]*entity.Book, error) {
|
||||
}
|
||||
|
||||
//Delete a book
|
||||
func (r *MySQLRepo) Delete(id entity.ID) error {
|
||||
func (r *BookMySQL) Delete(id entity.ID) error {
|
||||
_, err := r.db.Exec("delete from book where id = ?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1,4 +1,4 @@
|
||||
package user
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -8,27 +8,27 @@ import (
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//IRepo in memory repo
|
||||
type IRepo struct {
|
||||
//UserInmem in memory repo
|
||||
type UserInmem struct {
|
||||
m map[entity.ID]*entity.User
|
||||
}
|
||||
|
||||
//NewInmemRepository create new repository
|
||||
func NewInmemRepository() *IRepo {
|
||||
//NewUserInmem create new repository
|
||||
func NewUserInmem() *UserInmem {
|
||||
var m = map[entity.ID]*entity.User{}
|
||||
return &IRepo{
|
||||
return &UserInmem{
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
//Create an user
|
||||
func (r *IRepo) Create(e *entity.User) (entity.ID, error) {
|
||||
func (r *UserInmem) Create(e *entity.User) (entity.ID, error) {
|
||||
r.m[e.ID] = e
|
||||
return e.ID, nil
|
||||
}
|
||||
|
||||
//Get an user
|
||||
func (r *IRepo) Get(id entity.ID) (*entity.User, error) {
|
||||
func (r *UserInmem) Get(id entity.ID) (*entity.User, error) {
|
||||
if r.m[id] == nil {
|
||||
// return nil, fmt.Errorf("not found")
|
||||
return nil, domain.ErrNotFound
|
||||
@@ -37,7 +37,7 @@ func (r *IRepo) Get(id entity.ID) (*entity.User, error) {
|
||||
}
|
||||
|
||||
//Update an user
|
||||
func (r *IRepo) Update(e *entity.User) error {
|
||||
func (r *UserInmem) Update(e *entity.User) error {
|
||||
_, err := r.Get(e.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -47,7 +47,7 @@ func (r *IRepo) Update(e *entity.User) error {
|
||||
}
|
||||
|
||||
//Search users
|
||||
func (r *IRepo) Search(query string) ([]*entity.User, error) {
|
||||
func (r *UserInmem) Search(query string) ([]*entity.User, error) {
|
||||
var d []*entity.User
|
||||
for _, j := range r.m {
|
||||
if strings.Contains(strings.ToLower(j.FirstName), query) {
|
||||
@@ -63,7 +63,7 @@ func (r *IRepo) Search(query string) ([]*entity.User, error) {
|
||||
}
|
||||
|
||||
//List users
|
||||
func (r *IRepo) List() ([]*entity.User, error) {
|
||||
func (r *UserInmem) List() ([]*entity.User, error) {
|
||||
var d []*entity.User
|
||||
for _, j := range r.m {
|
||||
d = append(d, j)
|
||||
@@ -72,7 +72,7 @@ func (r *IRepo) List() ([]*entity.User, error) {
|
||||
}
|
||||
|
||||
//Delete an user
|
||||
func (r *IRepo) Delete(id entity.ID) error {
|
||||
func (r *UserInmem) Delete(id entity.ID) error {
|
||||
if r.m[id] == nil {
|
||||
return fmt.Errorf("not found")
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package user
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@@ -8,20 +8,20 @@ import (
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//MySQLRepo mysql repo
|
||||
type MySQLRepo struct {
|
||||
//UserMySQL mysql repo
|
||||
type UserMySQL struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
//NewMySQLRepoRepository create new repository
|
||||
func NewMySQLRepoRepository(db *sql.DB) *MySQLRepo {
|
||||
return &MySQLRepo{
|
||||
//NewUserMySQL create new repository
|
||||
func NewUserMySQL(db *sql.DB) *UserMySQL {
|
||||
return &UserMySQL{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
//Create an user
|
||||
func (r *MySQLRepo) Create(e *entity.User) (entity.ID, error) {
|
||||
func (r *UserMySQL) Create(e *entity.User) (entity.ID, error) {
|
||||
stmt, err := r.db.Prepare(`
|
||||
insert into user (id, email, password, first_name, last_name, created_at)
|
||||
values(?,?,?,?,?,?)`)
|
||||
@@ -47,7 +47,7 @@ func (r *MySQLRepo) Create(e *entity.User) (entity.ID, error) {
|
||||
}
|
||||
|
||||
//Get an user
|
||||
func (r *MySQLRepo) Get(id entity.ID) (*entity.User, error) {
|
||||
func (r *UserMySQL) Get(id entity.ID) (*entity.User, error) {
|
||||
return getUser(id, r.db)
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func getUser(id entity.ID, db *sql.DB) (*entity.User, error) {
|
||||
}
|
||||
|
||||
//Update an user
|
||||
func (r *MySQLRepo) Update(e *entity.User) error {
|
||||
func (r *UserMySQL) Update(e *entity.User) error {
|
||||
e.UpdatedAt = time.Now()
|
||||
_, err := r.db.Exec("update user set email = ?, password = ?, first_name = ?, last_name = ?, updated_at = ? where id = ?", e.Email, e.Password, e.FirstName, e.LastName, e.UpdatedAt.Format("2006-01-02"), e.ID)
|
||||
if err != nil {
|
||||
@@ -101,7 +101,7 @@ func (r *MySQLRepo) Update(e *entity.User) error {
|
||||
}
|
||||
|
||||
//Search users
|
||||
func (r *MySQLRepo) Search(query string) ([]*entity.User, error) {
|
||||
func (r *UserMySQL) Search(query string) ([]*entity.User, error) {
|
||||
stmt, err := r.db.Prepare(`select id from user where name like ?`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -135,7 +135,7 @@ func (r *MySQLRepo) Search(query string) ([]*entity.User, error) {
|
||||
}
|
||||
|
||||
//List users
|
||||
func (r *MySQLRepo) List() ([]*entity.User, error) {
|
||||
func (r *UserMySQL) List() ([]*entity.User, error) {
|
||||
stmt, err := r.db.Prepare(`select id from user`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -169,7 +169,7 @@ func (r *MySQLRepo) List() ([]*entity.User, error) {
|
||||
}
|
||||
|
||||
//Delete an user
|
||||
func (r *MySQLRepo) Delete(id entity.ID) error {
|
||||
func (r *UserMySQL) Delete(id entity.ID) error {
|
||||
_, err := r.db.Exec("delete from user where id = ?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
Reference in New Issue
Block a user