Skip to content

Commit

Permalink
one package
Browse files Browse the repository at this point in the history
  • Loading branch information
mnvx committed Aug 13, 2020
1 parent 58db669 commit c1fb5f3
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 115 deletions.
18 changes: 18 additions & 0 deletions base_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gorm_crud

import (
"github.com/jinzhu/gorm"
)

type BaseRepositoryInterface interface {
}

type BaseRepository struct {
BaseRepositoryInterface
db *gorm.DB
logger LoggerInterface
}

func NewBaseRepository(db *gorm.DB, logger LoggerInterface) BaseRepositoryInterface {
return &BaseRepository{db: db, logger: logger}
}
14 changes: 14 additions & 0 deletions base_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gorm_crud

type BaseServiceInterface interface {
}

type BaseService struct {
BaseServiceInterface
repository BaseRepositoryInterface
logger LoggerInterface
}

func NewBaseService(repository BaseRepositoryInterface, logger LoggerInterface) *BaseService {
return &BaseService{repository: repository, logger: logger}
}
2 changes: 1 addition & 1 deletion entity/common.go → common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package entity
package gorm_crud

type InterfaceEntity interface {
}
46 changes: 22 additions & 24 deletions repository/crud_repository.go → crud_repository.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package repository
package gorm_crud

import (
"database/sql"
"fmt"
"github.com/jinzhu/gorm"
"github.com/zubroide/gorm-crud/common"
"github.com/zubroide/gorm-crud/entity"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -33,11 +31,11 @@ type ListQueryBuilderInterface interface {

type BaseListQueryBuilder struct {
db *gorm.DB
logger common.LoggerInterface
logger LoggerInterface
ListQueryBuilderInterface
}

func NewBaseListQueryBuilder(db *gorm.DB, logger common.LoggerInterface) ListQueryBuilderInterface {
func NewBaseListQueryBuilder(db *gorm.DB, logger LoggerInterface) ListQueryBuilderInterface {
return &BaseListQueryBuilder{db: db, logger: logger}
}

Expand Down Expand Up @@ -112,25 +110,25 @@ func (c BaseListQueryBuilder) ListQuery(parameters ListParametersInterface) (*go

type CrudRepositoryInterface interface {
BaseRepositoryInterface
GetModel() entity.InterfaceEntity
Find(id uint) (entity.InterfaceEntity, error)
GetModel() InterfaceEntity
Find(id uint) (InterfaceEntity, error)
PluckBy(fieldNames []string) (map[string]int64, error)
ListAll() ([]entity.InterfaceEntity, error)
List(parameters ListParametersInterface) ([]entity.InterfaceEntity, error)
Create(item entity.InterfaceEntity) entity.InterfaceEntity
CreateOrUpdateMany(item entity.InterfaceEntity, columns []string, values []map[string]interface{}, onConflict string) error
Update(item entity.InterfaceEntity) entity.InterfaceEntity
ListAll() ([]InterfaceEntity, error)
List(parameters ListParametersInterface) ([]InterfaceEntity, error)
Create(item InterfaceEntity) InterfaceEntity
CreateOrUpdateMany(item InterfaceEntity, columns []string, values []map[string]interface{}, onConflict string) error
Update(item InterfaceEntity) InterfaceEntity
Delete(id uint) error
}

type CrudRepository struct {
CrudRepositoryInterface
*BaseRepository
model entity.InterfaceEntity // Dynamic typing
model InterfaceEntity // Dynamic typing
listQueryBuilder ListQueryBuilderInterface
}

func NewCrudRepository(db *gorm.DB, model entity.InterfaceEntity, listQueryBuilder ListQueryBuilderInterface, logger common.LoggerInterface) CrudRepositoryInterface {
func NewCrudRepository(db *gorm.DB, model InterfaceEntity, listQueryBuilder ListQueryBuilderInterface, logger LoggerInterface) CrudRepositoryInterface {
repo := NewBaseRepository(db, logger).(*BaseRepository)
return &CrudRepository{
BaseRepository: repo,
Expand All @@ -139,11 +137,11 @@ func NewCrudRepository(db *gorm.DB, model entity.InterfaceEntity, listQueryBuild
}
}

func (c CrudRepository) GetModel() entity.InterfaceEntity {
func (c CrudRepository) GetModel() InterfaceEntity {
return c.model
}

func (c CrudRepository) Find(id uint) (entity.InterfaceEntity, error) {
func (c CrudRepository) Find(id uint) (InterfaceEntity, error) {
item := reflect.New(reflect.TypeOf(c.GetModel()).Elem()).Interface()
err := c.db.First(item, id).Error
return item, err
Expand Down Expand Up @@ -179,8 +177,8 @@ func (c CrudRepository) PluckBy(fieldNames []string) (map[string]int64, error) {
return res, err
}

func (c CrudRepository) ListAll() ([]entity.InterfaceEntity, error) {
entities := make([]entity.InterfaceEntity, 0)
func (c CrudRepository) ListAll() ([]InterfaceEntity, error) {
entities := make([]InterfaceEntity, 0)

page := 0
pageSize := 10000
Expand Down Expand Up @@ -212,20 +210,20 @@ func (c CrudRepository) ListAll() ([]entity.InterfaceEntity, error) {
return entities, nil
}

func (c CrudRepository) List(parameters ListParametersInterface) ([]entity.InterfaceEntity, error) {
func (c CrudRepository) List(parameters ListParametersInterface) ([]InterfaceEntity, error) {

items := reflect.New(reflect.SliceOf(reflect.TypeOf(c.GetModel()).Elem())).Interface()
query, err := c.listQueryBuilder.ListQuery(parameters)
if err != nil {
return []entity.InterfaceEntity{}, err
return []InterfaceEntity{}, err
}

err = query.Find(items).Error

entities := reflect.ValueOf(items).Elem().Interface()

// Convert entities to slice
var data []entity.InterfaceEntity
var data []InterfaceEntity
sliceValue := reflect.ValueOf(entities)
for i := 0; i < sliceValue.Len(); i++ {
data = append(data, sliceValue.Index(i).Interface())
Expand All @@ -234,7 +232,7 @@ func (c CrudRepository) List(parameters ListParametersInterface) ([]entity.Inter
return data, err
}

func (c CrudRepository) Create(item entity.InterfaceEntity) entity.InterfaceEntity {
func (c CrudRepository) Create(item InterfaceEntity) InterfaceEntity {
c.db.Create(item)
return item
}
Expand All @@ -251,7 +249,7 @@ func (c CrudRepository) prepareTime(val time.Time) string {

// CreateOrUpdateMany create or update if exists
func (c CrudRepository) CreateOrUpdateMany(
item entity.InterfaceEntity,
item InterfaceEntity,
columns []string,
values []map[string]interface{},
onConflict string,
Expand Down Expand Up @@ -302,7 +300,7 @@ func (c CrudRepository) CreateOrUpdateMany(
return c.db.Exec(query).Error
}

func (c CrudRepository) Update(item entity.InterfaceEntity) entity.InterfaceEntity {
func (c CrudRepository) Update(item InterfaceEntity) InterfaceEntity {
c.db.Save(item)
return item
}
Expand Down
45 changes: 45 additions & 0 deletions crud_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gorm_crud

type CrudServiceInterface interface {
BaseServiceInterface
GetModel() InterfaceEntity
GetItem(id uint) (InterfaceEntity, error)
GetList(parameters ListParametersInterface) ([]InterfaceEntity, error)
Create(item InterfaceEntity) InterfaceEntity
Update(item InterfaceEntity) InterfaceEntity
Delete(id uint) error
}

type CrudService struct {
*BaseService
repository CrudRepositoryInterface
}

func NewCrudService(repository CrudRepositoryInterface, logger LoggerInterface) CrudServiceInterface {
service := NewBaseService(repository, logger)
return &CrudService{service, repository}
}

func (c CrudService) GetModel() InterfaceEntity {
return c.repository.GetModel()
}

func (c CrudService) GetItem(id uint) (InterfaceEntity, error) {
return c.repository.Find(id)
}

func (c CrudService) GetList(parameters ListParametersInterface) ([]InterfaceEntity, error) {
return c.repository.List(parameters)
}

func (c CrudService) Create(item InterfaceEntity) InterfaceEntity {
return c.repository.Create(item)
}

func (c CrudService) Update(item InterfaceEntity) InterfaceEntity {
return c.repository.Update(item)
}

func (c CrudService) Delete(id uint) error {
return c.repository.Delete(id)
}
2 changes: 1 addition & 1 deletion common/logger.go → logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package gorm_crud

type LoggerInterface interface {
Error(message string)
Expand Down
19 changes: 0 additions & 19 deletions repository/base_repository.go

This file was deleted.

19 changes: 0 additions & 19 deletions service/base_service.go

This file was deleted.

51 changes: 0 additions & 51 deletions service/crud_service.go

This file was deleted.

0 comments on commit c1fb5f3

Please sign in to comment.