-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c5351f1
Showing
10 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.pgdata | ||
.idea | ||
.env | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM golang:1.15 | ||
|
||
WORKDIR /go/src | ||
ENV PATH="/go/bin:${PATH}" | ||
ENV GO111MODULE=on | ||
ENV CGO_ENABLED=1 | ||
|
||
RUN apt-get update && \ | ||
apt-get install build-essential protobuf-compiler librdkafka-dev -y && \ | ||
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc && \ | ||
go get google.golang.org/protobuf/cmd/protoc-gen-go && \ | ||
go get github.com/spf13/cobra/ && \ | ||
wget https://github.com/ktr0731/evans/releases/download/0.9.1/evans_linux_amd64.tar.gz && \ | ||
tar -xzvf evans_linux_amd64.tar.gz && \ | ||
mv evans ../bin && rm -f evans_linux_amd64.tar.gz | ||
|
||
CMD ["tail", "-f", "/dev/null"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
version: "3" | ||
|
||
services: | ||
app: | ||
build: . | ||
ports: | ||
- "50051:50051" | ||
volumes: | ||
- .:/go/src/ | ||
extra_hosts: | ||
- "host.docker.internal:172.17.0.1" | ||
|
||
db: | ||
image: postgres:9.4 | ||
restart: always | ||
tty: true | ||
volumes: | ||
- .pgdata:/var/lib/postgresql/data | ||
environment: | ||
- POSTGRES_PASSWORD=root | ||
- POSTGRES_DB=codepix | ||
ports: | ||
- "5432:5432" | ||
|
||
pgadmin: | ||
image: dpage/pgadmin4 | ||
tty: true | ||
environment: | ||
- [email protected] | ||
- PGADMIN_DEFAULT_PASSWORD=123456 | ||
ports: | ||
- "9000:80" | ||
depends_on: | ||
- db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/asaskevich/govalidator" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type Account struct { | ||
Base `valid:"required"` | ||
OwnerName string `json:"owner_name" valid:"notnull"` | ||
Bank *Bank `valid:"-"` | ||
BankID string `gorm:"column:bank_id;type:uuid;not null" valid:"-"` | ||
Number string `json:"number" valid:"notnull"` | ||
PixKeys []*PixKey `valid:"-"` | ||
} | ||
|
||
func (account *Account) isValid() error { | ||
_, err := govalidator.ValidateStruct(account) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewAccount(bank *Bank, number string, ownerName string) (*Account, error) { | ||
account := Account{ | ||
Bank: bank, | ||
BankID: bank.ID, | ||
Number: number, | ||
OwnerName: ownerName, | ||
} | ||
|
||
account.ID = uuid.NewV4().String() | ||
account.CreatedAt = time.Now() | ||
|
||
err := account.isValid() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &account, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/asaskevich/govalidator" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type Bank struct { | ||
Base | ||
Code string `json:"code" valid:"notnull"` | ||
Name string `json:"name" valid:"notnull"` | ||
Accounts []*Account `valid:"-"` | ||
} | ||
|
||
func (bank *Bank) isValid() error { | ||
_, err := govalidator.ValidateStruct(bank) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewBank(code string, name string) (*Bank, error) { | ||
bank := Bank{ | ||
Code: code, | ||
Name: name, | ||
} | ||
|
||
bank.ID = uuid.NewV4().String() | ||
bank.CreatedAt = time.Now() | ||
|
||
err := bank.isValid() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &bank, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/asaskevich/govalidator" | ||
) | ||
|
||
func init() { | ||
govalidator.SetFieldsRequiredByDefault(true) | ||
} | ||
|
||
type Base struct { | ||
ID string `json:"id" valid:"uuid"` | ||
CreatedAt time.Time `json:"created_at" valid:"-"` | ||
UpdatedAt time.Time `json:"updated_at" valid:"-"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package model | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/asaskevich/govalidator" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type PixKeyRepositoryInterface interface { | ||
RegisterKey(pixKey *PixKey) (*PixKey, error) | ||
FindKeyByKind(key string, kind string) (*PixKey, error) | ||
AddBank(bank *Bank) error | ||
AddAccount(account *Account) error | ||
FindAccount(id string) (*Account, error) | ||
} | ||
|
||
type PixKey struct { | ||
Base `valid:"required"` | ||
Kind string `json:"kind" valid:"notnull"` | ||
Key string `json:"key" valid:"notnull"` | ||
AccountID string `json:"account_id" valid:"notnull"` | ||
Bank *Bank | ||
Account *Account `valid:"-"` | ||
Status string `json:"status" valid:"notnull"` | ||
} | ||
|
||
func (pixKey *PixKey) isValid() error { | ||
_, err := govalidator.ValidateStruct(pixKey) | ||
|
||
if pixKey.Kind != "email" && pixKey.Kind != "cpf" { | ||
return errors.New("Invalid key type") | ||
} | ||
|
||
if pixKey.Status != "active" && pixKey.Status != "inactive" { | ||
return errors.New("Invalid status") | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewPixKey(kind string, account *Account, key string) (*PixKey, error) { | ||
|
||
pixKey := PixKey{ | ||
Kind: kind, | ||
Key: key, | ||
Account: account, | ||
Status: "active", | ||
} | ||
|
||
pixKey.ID = uuid.NewV4().String() | ||
pixKey.CreatedAt = time.Now() | ||
|
||
err := pixKey.isValid() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pixKey, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package model | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/asaskevich/govalidator" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
const ( | ||
TransactionPending string = "pending" | ||
TransactionCompleted string = "completed" | ||
TransactionError string = "error" | ||
TransactionConfirmed string = "confirmed" | ||
) | ||
|
||
type TransactionRepositoryInterface interface { | ||
Register(transaction *Transaction) error | ||
Save(transaction *Transaction) error | ||
Find(id string) (*Transaction, error) | ||
} | ||
|
||
type Transactions struct { | ||
Transaction []Transaction | ||
} | ||
|
||
type Transaction struct { | ||
Base `valid:"required"` | ||
AccountFrom *Account `valid:"-"` | ||
Amount float64 `json:"amount" valid:"notnull"` | ||
PixKeyTo *PixKey `valid:"-"` | ||
Status string `json:"status" valid:"notnull"` | ||
Description string `json:"description" valid:"notnull"` | ||
CancelDescription string `json:"cancel_description" valid:"notnull"` | ||
} | ||
|
||
func (t *Transaction) isValid() error { | ||
_, err := govalidator.ValidateStruct(t) | ||
|
||
if t.Amount <= 0 { | ||
return errors.New("The amount must be greater than 0") | ||
} | ||
|
||
if t.Status != TransactionPending && t.Status != TransactionCompleted && t.Status != TransactionError { | ||
return errors.New("Invalid status for the transaction") | ||
} | ||
|
||
if t.PixKeyTo.AccountID == t.AccountFrom.ID { | ||
return errors.New("The source and destinantion account cannot be the same") | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewTransaction(accountFrom *Account, amount float64, pixKeyTo *PixKey, description string) (*Transaction, error) { | ||
transaction := Transaction{ | ||
AccountFrom: accountFrom, | ||
Amount: amount, | ||
PixKeyTo: pixKeyTo, | ||
Status: TransactionPending, | ||
Description: description, | ||
} | ||
|
||
transaction.ID = uuid.NewV4().String() | ||
transaction.CreatedAt = time.Now() | ||
|
||
err := transaction.isValid() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &transaction, nil | ||
} | ||
|
||
func (t *Transaction) Complete() error { | ||
t.Status = TransactionCompleted | ||
t.UpdatedAt = time.Now() | ||
err := t.isValid() | ||
return err | ||
} | ||
|
||
func (t *Transaction) Confirm() error { | ||
t.Status = TransactionConfirmed | ||
t.UpdatedAt = time.Now() | ||
err := t.isValid() | ||
return err | ||
} | ||
|
||
func (t *Transaction) Cancel(description string) error { | ||
t.Status = TransactionError | ||
t.UpdatedAt = time.Now() | ||
t.Description = description | ||
err := t.isValid() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/santosant/codepix-go | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d | ||
github.com/satori/go.uuid v1.2.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ= | ||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= | ||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= | ||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= |