Skip to content

Commit

Permalink
feat: add createdID on response from CreateAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoclair committed Jan 12, 2024
1 parent f7edc08 commit e36aa9b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion domain/contract/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AuthRepo interface {

type AccountRepo interface {
AddTransfer(ctx context.Context, transferUUID string, accountOriginID, accountDestinationID int64, amount float64) (err error)
CreateAccount(ctx context.Context, account entity.Account) (err error)
CreateAccount(ctx context.Context, account entity.Account) (createdID int64, err error)
GetAccountByDocument(ctx context.Context, encryptedCPF string) (account entity.Account, err error)
GetAccounts(ctx context.Context, take, skip int64) (accounts []entity.Account, totalRecords int64, err error)
GetAccountByUUID(ctx context.Context, accountUUID string) (account entity.Account, err error)
Expand Down
2 changes: 1 addition & 1 deletion domain/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *accountService) CreateAccount(ctx context.Context, account entity.Accou
}
account.UUID = uuid.NewV4().String()

err = s.svc.dm.Account().CreateAccount(ctx, account)
_, err = s.svc.dm.Account().CreateAccount(ctx, account)
if err != nil {
s.svc.log.Error(ctx, err.Error())
return err
Expand Down
15 changes: 10 additions & 5 deletions infra/data/mysql/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *accountRepo) AddTransfer(ctx context.Context, transferUUID string, acco
return nil
}

func (r *accountRepo) CreateAccount(ctx context.Context, account entity.Account) (err error) {
func (r *accountRepo) CreateAccount(ctx context.Context, account entity.Account) (createdID int64, err error) {
query := `
INSERT INTO tab_account (
account_uuid,
Expand All @@ -92,21 +92,26 @@ func (r *accountRepo) CreateAccount(ctx context.Context, account entity.Account)

stmt, err := r.db.Prepare(query)
if err != nil {
return mysqlutils.HandleMySQLError(err)
return createdID, mysqlutils.HandleMySQLError(err)
}
defer stmt.Close()

_, err = stmt.Exec(
result, err := stmt.Exec(
account.UUID,
account.Name,
account.CPF,
account.Password,
)
if err != nil {
return mysqlutils.HandleMySQLError(err)
return createdID, mysqlutils.HandleMySQLError(err)
}

return nil
createdID, err = result.LastInsertId()
if err != nil {
return createdID, mysqlutils.HandleMySQLError(err)
}

return createdID, nil
}

func (r *accountRepo) GetAccountByDocument(ctx context.Context, encryptedCPF string) (account entity.Account, err error) {
Expand Down
13 changes: 10 additions & 3 deletions infra/data/mysql/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ func createRandomAccount(t *testing.T) entity.Account {
CPF: random.RandomCPF(),
}

c := crypto.NewCrypto()
args.Password, _ = c.HashPassword(random.RandomSecret())
err := testMysql.Account().CreateAccount(context.Background(), args)
var (
c = crypto.NewCrypto()
err error
)

args.Password, err = c.HashPassword(random.RandomSecret())
require.NoError(t, err)

createID, err := testMysql.Account().CreateAccount(context.Background(), args)
require.NoError(t, err)
require.NotZero(t, createID)

account, err := testMysql.Account().GetAccountByUUID(context.Background(), args.UUID)
require.NoError(t, err)
Expand Down

0 comments on commit e36aa9b

Please sign in to comment.