diff --git a/domain/entity/base_aggregate_root.go b/domain/entity/base_aggregate_root.go index 6a00d21..2b6f407 100644 --- a/domain/entity/base_aggregate_root.go +++ b/domain/entity/base_aggregate_root.go @@ -8,18 +8,14 @@ type BaseAggregateRoot struct { domainEvents []event.IBaseDomainEvent } -func NewBaseAggregateRoot() (BaseAggregateRoot, error) { - base, err := NewBaseEntity() - if err != nil { - return BaseAggregateRoot{}, nil - } - +func NewBaseAggregateRoot(rootEntityId uint) BaseAggregateRoot { + base := NewBaseEntity(rootEntityId) events := make([]event.IBaseDomainEvent, 0) return BaseAggregateRoot{ BaseEntity: base, domainEvents: events, - }, nil + } } func (aggregate *BaseAggregateRoot) AddEvent(event event.IBaseDomainEvent) { diff --git a/domain/entity/base_entity.go b/domain/entity/base_entity.go index 56e986e..d89d1f4 100644 --- a/domain/entity/base_entity.go +++ b/domain/entity/base_entity.go @@ -1,17 +1,9 @@ package entity -import "github.com/google/uuid" - type BaseEntity struct { - ID uuid.UUID + ID uint } -func NewBaseEntity() (BaseEntity, error) { - id, err := uuid.NewUUID() - - if err != nil { - return BaseEntity{}, err - } - - return BaseEntity{ID: id}, nil +func NewBaseEntity(id uint) BaseEntity { + return BaseEntity{ID: id} } diff --git a/infrastructure/persistence/base_model.go b/infrastructure/persistence/base_model.go deleted file mode 100644 index ea8772b..0000000 --- a/infrastructure/persistence/base_model.go +++ /dev/null @@ -1,15 +0,0 @@ -package persistence - -import ( - "time" - - customgorm "github.com/tonybka/go-base-ddd/infrastructure/custom_gorm" - "gorm.io/gorm" -) - -type BaseModel struct { - ID customgorm.CustomTypeUUIDv1 `gorm:"primarykey;default:(UUID_TO_BIN(UUID()));"` - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt gorm.DeletedAt `gorm:"index"` -} diff --git a/infrastructure/tests/sqlite_db_connect.go b/infrastructure/tests/sqlite_db_connect.go deleted file mode 100644 index a145d56..0000000 --- a/infrastructure/tests/sqlite_db_connect.go +++ /dev/null @@ -1,61 +0,0 @@ -package tests - -import ( - "io/ioutil" - "os" - "path" - - "gorm.io/driver/sqlite" - "gorm.io/gorm" - "gorm.io/gorm/logger" -) - -type SqliteDBConnect struct { - dataDir string - dbConnection *gorm.DB -} - -func NewSqliteDBConnect() (*SqliteDBConnect, error) { - var tempDir = "" - - // Temp file setup - tempDir, err := ioutil.TempDir("", "tests-") - if err != nil { - return nil, err - } - - // Database setup - tempDir = path.Join(tempDir, "test.sqlite3") - dialector := sqlite.Open(tempDir) - - dbConn, err := gorm.Open(dialector, &gorm.Config{ - SkipDefaultTransaction: true, - Logger: logger.Default.LogMode(logger.Info), - }) - if err != nil { - return nil, err - } - - sqliteDB, err := dbConn.DB() - if err != nil { - return nil, err - } - sqliteDB.SetMaxOpenConns(1) - dbConn.Debug() - - return &SqliteDBConnect{dataDir: tempDir, dbConnection: dbConn}, nil -} - -func (conn *SqliteDBConnect) Connection() *gorm.DB { - return conn.dbConnection -} - -func (conn *SqliteDBConnect) CleanUp() error { - var err error - - if len(conn.dataDir) > 0 { - err = os.RemoveAll(conn.dataDir) - } - - return err -} diff --git a/samples/persistence/account/account_model.go b/samples/persistence/account/account_model.go deleted file mode 100644 index 956262d..0000000 --- a/samples/persistence/account/account_model.go +++ /dev/null @@ -1,11 +0,0 @@ -package account - -import ( - "github.com/tonybka/go-base-ddd/infrastructure/persistence" -) - -type AccountModel struct { - persistence.BaseModel - - AccountName string `gorm:"column:account_name;unique"` -} diff --git a/samples/persistence/account/account_repository.go b/samples/persistence/account/account_repository.go deleted file mode 100644 index 19eff7d..0000000 --- a/samples/persistence/account/account_repository.go +++ /dev/null @@ -1,45 +0,0 @@ -package account - -import ( - "github.com/google/uuid" - customgorm "github.com/tonybka/go-base-ddd/infrastructure/custom_gorm" - "gorm.io/gorm" -) - -type AccountRepository struct { - db *gorm.DB -} - -func NewAccountRepository(db *gorm.DB) *AccountRepository { - return &AccountRepository{db} -} - -// Create creates new account -func (repo *AccountRepository) Create(dataModel AccountModel) error { - if result := repo.db.Create(&dataModel); result.Error != nil { - return result.Error - } - return nil -} - -// FindById query account by it's identity -func (repo *AccountRepository) FindById(id uuid.UUID) (AccountModel, error) { - var dataModel AccountModel - - if result := repo.db.Where("id = ?", customgorm.CustomTypeUUIDv1FromString(id.String())).First(&dataModel); result.Error != nil { - return AccountModel{}, result.Error - } - - return dataModel, nil -} - -// GetAll returns all accounts in the table -func (repo *AccountRepository) GetAll() ([]AccountModel, error) { - var dataModels []AccountModel - - if result := repo.db.Find(&dataModels); result.Error != nil { - return nil, result.Error - } - - return dataModels, nil -} diff --git a/samples/persistence/account/account_repository_test.go b/samples/persistence/account/account_repository_test.go deleted file mode 100644 index c3be541..0000000 --- a/samples/persistence/account/account_repository_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package account - -import ( - "testing" - - "github.com/google/uuid" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - customgorm "github.com/tonybka/go-base-ddd/infrastructure/custom_gorm" - "github.com/tonybka/go-base-ddd/infrastructure/persistence" - "github.com/tonybka/go-base-ddd/infrastructure/tests" - "gorm.io/gorm" -) - -type AccountRepositoryTestSuite struct { - suite.Suite - - sqliteConnect *tests.SqliteDBConnect - dbConn *gorm.DB - - accountRepo *AccountRepository -} - -func (ts *AccountRepositoryTestSuite) SetupSuite() { - sqliteConn, err := tests.NewSqliteDBConnect() - require.NoError(ts.T(), err) - - ts.sqliteConnect = sqliteConn - ts.dbConn = sqliteConn.Connection() - - ts.dbConn.AutoMigrate(&AccountModel{}) - - ts.accountRepo = NewAccountRepository(ts.dbConn) -} - -func (ts *AccountRepositoryTestSuite) TestCreateAccount() { - entityId, err := uuid.NewUUID() - ts.NoError(err) - - account := AccountModel{ - BaseModel: persistence.BaseModel{ - ID: customgorm.CustomTypeUUIDv1FromString(entityId.String()), - }, - AccountName: "abc", - } - - err = ts.accountRepo.Create(account) - ts.NoError(err) - - all, err := ts.accountRepo.GetAll() - ts.NoError(err) - ts.Greater(len(all), 0) - - queriedAccount, err := ts.accountRepo.FindById(uuid.UUID(account.ID)) - ts.NoError(err) - ts.Equal(account.AccountName, queriedAccount.AccountName) - ts.Equal(account.ID, queriedAccount.ID) -} - -func (ts *AccountRepositoryTestSuite) TearDownSuite() { - err := ts.sqliteConnect.CleanUp() - ts.NoError(err) -} - -func TestSuiteRunnerAccountRepository(t *testing.T) { - ts := new(AccountRepositoryTestSuite) - suite.Run(t, ts) -} diff --git a/infrastructure/custom_gorm/custom_type_uuidv1.go b/types/custom_type_uuidv1.go similarity index 93% rename from infrastructure/custom_gorm/custom_type_uuidv1.go rename to types/custom_type_uuidv1.go index 05f36f1..aa02604 100644 --- a/infrastructure/custom_gorm/custom_type_uuidv1.go +++ b/types/custom_type_uuidv1.go @@ -1,4 +1,4 @@ -package customgorm +package types import ( "database/sql/driver" @@ -17,12 +17,12 @@ func CustomTypeUUIDv1FromString(s string) CustomTypeUUIDv1 { return CustomTypeUUIDv1(uuid.MustParse(s)) } -//String -> String Representation of Binary16 +// String -> String Representation of Binary16 func (my CustomTypeUUIDv1) String() string { return uuid.UUID(my).String() } -//GormDataType -> sets type to binary(16) +// GormDataType -> sets type to binary(16) func (my CustomTypeUUIDv1) GormDataType() string { return "binary(16)" }