Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

feature: adding http calls to other services #59

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/rest/main.go"
cmd = "go build -o ./tmp/main ./cmd/http/main.go"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
Expand Down
14 changes: 3 additions & 11 deletions .github/workflows/BuildApp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ jobs:
with:
args: >
-Dsonar.organization=pangolin-do-golang
-Dsonar.projectKey=pangolin-do-golang_tech-challenge
-Dsonar.projectKey=pangolin-do-golang_tech-challenger
-Dsonar.projectVersion=${{ env.SHA }}
-Dsonar.qualitygate.wait=true
-Dsonar.tests=internal/
-Dsonar.verbose=true

- name: Run Snyk to check for Vulnerabilities
uses: snyk/actions/golang@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
Expand All @@ -64,14 +58,12 @@ jobs:
uses: aquasecurity/trivy-action@0.21.0
with:
image-ref: "ghcr.io/${{ github.repository }}:${{ steps.short-sha.outputs.sha }}"
format: 'github'
format: 'table'
scan-type: 'image'
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
output: 'dependency-results.sbom.json'
github-pat: "${{ secrets.GITHUB_TOKEN }}"

- name: Push docker image to Registry
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ To correctly use the project, it is necessary to define a .env file, with the va
* DB_HOST
* DB_NAME
* DB_PORT
* CART_SERVICE_URL
* ORDER_SERVICE_URL
* CUSTOMER_SERVICE_URL

We recommend using for development the following values:

Expand All @@ -36,6 +39,9 @@ DB_PASSWORD=pass
DB_HOST=pgsql
DB_NAME=postgres
DB_PORT=5432
CART_SERVICE_URL=http://localhost:8081 # depends on how you start other services
ORDER_SERVICE_URL=http://localhost:8082 # depends on how you start other services
CUSTOMER_SERVICE_URL=http://localhost:8083 # depends on how you start other services
```

## Executing with Docker (Compose)
Expand Down
12 changes: 4 additions & 8 deletions cmd/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@ func main() {
panic(err)
}

customerRepository := dbAdapter.NewPostgresCustomerRepository(db)
customerService := customer.NewService(customerRepository)
customerService := customer.NewService()

productRepository := dbAdapter.NewPostgresProductRepository(db)
productService := product.NewProductService(productRepository)

cartRepository := dbAdapter.NewPostgresCartRepository(db)
cartProductsRepository := dbAdapter.NewPostgresCartProductsRepository(db)
cartService := cart.NewService(cartRepository, cartProductsRepository)
cartService := cart.NewService()

orderRepository := dbAdapter.NewPostgresOrderRepository(db)
orderProductRepository := dbAdapter.NewPostgresOrderProductsRepository(db)
orderService := order.NewOrderService(orderRepository, orderProductRepository, cartService, productService)
orderService := order.NewOrderService(cartService, productService)

restServer := server.NewRestServer(&server.RestServerOptions{
OrderService: orderService,
Expand All @@ -55,6 +50,7 @@ func main() {

func initDb() (*gorm.DB, error) {
_ = godotenv.Load()

dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable TimeZone=America/Sao_Paulo",
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.2

require (
github.com/gin-gonic/gin v1.10.0
github.com/go-resty/resty/v2 v2.16.2
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/swaggo/files v1.0.1
Expand Down Expand Up @@ -50,12 +51,12 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
34 changes: 18 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg=
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -127,18 +129,18 @@ golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
Expand All @@ -151,8 +153,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand All @@ -161,16 +163,16 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
133 changes: 84 additions & 49 deletions internal/core/cart/cart_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,143 @@ package cart

import (
"context"
"errors"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"github.com/pangolin-do-golang/tech-challenge/internal/errutil"
"os"
)

type Service struct {
CartRepository ICartRepository
CartProductsRepository ICartProductRepository
HttpClient resty.Client
}

func NewService(cartRepository ICartRepository, cartProductsRepository ICartProductRepository) IService {
func NewService() IService {
client := resty.New()
client.SetBaseURL(os.Getenv("CART_SERVICE_URL"))
return &Service{
CartRepository: cartRepository,
CartProductsRepository: cartProductsRepository,
HttpClient: *client,
}
}

func (s *Service) LoadCart(_ context.Context, clientID uuid.UUID) (*Cart, error) {
cart, err := s.CartRepository.Get(clientID)
var cart *Cart

_, err := s.
HttpClient.
R().
SetBody(LoadCardPayload{
ClientID: clientID,
}).
SetResult(&cart).
Post("/cart/loadcart")

if err != nil {
if !errors.Is(err, errutil.ErrRecordNotFound) {
return nil, err
}

cart, err = s.CartRepository.Create(clientID)
if err != nil {
return nil, err
}
return nil, err
}

return cart, nil

}

func (s *Service) GetFullCart(clientID uuid.UUID) (*Cart, error) {
cart, err := s.CartRepository.Get(clientID)
if err != nil {
return nil, err
}
var cart *Cart
_, err := s.
HttpClient.
R().
SetResult(&cart).
Get("/cart/overview")

products, err := s.CartProductsRepository.GetByCartID(context.Background(), cart.ID)
if err != nil {
return nil, err
}

cart.Products = products

return cart, nil
}

type CleanupPayload struct {
ClientID uuid.UUID `json:"client_id" binding:"required" format:"uuid"`
}

func (s *Service) Cleanup(clientID uuid.UUID) error {
cart, err := s.LoadCart(context.Background(), clientID)

_, err := s.HttpClient.R().SetBody(CleanupPayload{
ClientID: clientID,
}).Post("/cart/cleanup")

if err != nil {
return err
}

products, err := s.CartProductsRepository.GetByCartID(context.Background(), cart.ID)
for _, p := range products {
err = s.CartProductsRepository.DeleteByProductID(context.Background(), cart.ID, p.ProductID)
if err != nil {
return err
}
}

return nil
}

type AddProductPayload struct {
ClientID uuid.UUID `json:"client_id" binding:"required" format:"uuid"`
ProductID uuid.UUID `json:"product_id" binding:"required" format:"uuid"`
Quantity int `json:"quantity" binding:"required,min=1" example:"1"`
Comments string `json:"comments"`
}

func (s *Service) AddProduct(ctx context.Context, clientID uuid.UUID, product *Product) error {
cart, err := s.LoadCart(ctx, clientID)

_, err := s.HttpClient.R().SetBody(AddProductPayload{
ClientID: clientID,
ProductID: product.ProductID,
Quantity: product.Quantity,
Comments: product.Comments,
}).Post("/cart/add-product")

if err != nil {
return err
}

// TODO verificar se produto já tá no carrinho/colocar índice de unicidade
return s.CartProductsRepository.Create(ctx, cart.ID, product)
return nil
}

type RemoveProductPayload struct {
clientID uuid.UUID `json:"client_id"`
productID uuid.UUID `json:"product_id"`
}

type LoadCardPayload struct {
ClientID uuid.UUID `json:"client_id" binding:"required" format:"uuid"`
}

func (s *Service) RemoveProduct(ctx context.Context, clientID uuid.UUID, productID uuid.UUID) error {
cart, err := s.LoadCart(ctx, clientID)
if err != nil {
return err
}
_, err := s.HttpClient.R().SetBody(RemoveProductPayload{
productID: productID,
clientID: clientID,
}).Post("/cart/remove-product")

products, err := s.CartProductsRepository.GetByCartID(ctx, cart.ID)
if err != nil {
return err
}

for _, product := range products {
if product.ProductID == productID {
return s.CartProductsRepository.DeleteByProductID(ctx, cart.ID, productID)
}
}

return nil
}

type EditProductPayload struct {
ClientID uuid.UUID `json:"client_id" binding:"required" format:"uuid"`
ProductID uuid.UUID `json:"product_id" binding:"required" format:"uuid"`
Quantity int `json:"quantity" binding:"required" example:"2"`
Comments string `json:"comments"`
}

func (s *Service) EditProduct(ctx context.Context, clientID uuid.UUID, product *Product) error {
cart, err := s.LoadCart(ctx, clientID)

_, err := s.
HttpClient.
R().
SetBody(EditProductPayload{
ClientID: clientID,
ProductID: product.ProductID,
Quantity: product.Quantity,
Comments: product.Comments,
}).
Post("/cart/edit-product")

if err != nil {
return err
return nil
}

return s.CartProductsRepository.UpdateProductByProductID(ctx, cart.ID, product.ProductID, product)
return nil
}
Loading