Skip to content

Commit 2e0250d

Browse files
authored
Merge pull request #4 from pangolin-do-golang/http-request-to-services
feature: adding cleanup and loadcart
2 parents 3af1cc1 + 17ee306 commit 2e0250d

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tmp_dir = "tmp"
55
[build]
66
args_bin = []
77
bin = "./tmp/main"
8-
cmd = "go build -o ./tmp/main ./cmd/rest/main.go"
8+
cmd = "go build -o ./tmp/main ./cmd/http/main.go"
99
delay = 1000
1010
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
1111
exclude_file = []

.github/workflows/BuildApp.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ on:
66
branches:
77
- main
88
pull_request:
9-
branches:
10-
- main
9+
types: [opened, synchronize, reopened]
1110

1211

1312
jobs:
@@ -24,19 +23,32 @@ jobs:
2423
with:
2524
length: 7
2625

26+
- name: Setup GoLang
27+
uses: actions/setup-go@v3
28+
with:
29+
go-version: '1.23.3'
30+
31+
- name: Build And Test
32+
shell: bash
33+
run: |
34+
go test -cover ./internal/... -covermode=atomic -coverprofile=coverage.out
35+
2736
- name: SonarCloud Scan
2837
uses: SonarSource/sonarcloud-github-action@master
2938
env:
3039
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3140
github-token: ${{ secrets.GITHUB_TOKEN }}
3241
SHA: ${{ steps.short-sha.outputs.sha }}
3342
with:
43+
projectBaseDir: ./
3444
args: >
3545
-Dsonar.organization=pangolin-golang
3646
-Dsonar.projectKey=pangolin-do-golang_tech-challenge-cart-api
3747
-Dsonar.projectVersion=${{ env.SHA }}
48+
-Dsonar.language=go
3849
-Dsonar.qualitygate.wait=true
3950
-Dsonar.tests=internal/
51+
-Dsonar.go.coverage.reportPaths=coverage.out
4052
-Dsonar.verbose=true
4153
4254
- name: Login to GitHub Container Registry

internal/adapters/rest/controller/cart_controller.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package controller
22

33
import (
4+
"net/http"
5+
46
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/cart"
57
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/errutil"
6-
"net/http"
78

89
"github.com/gin-gonic/gin"
910
"github.com/google/uuid"
@@ -154,3 +155,51 @@ func (ctrl CartController) Overview(c *gin.Context) {
154155

155156
c.JSON(http.StatusOK, fullcart)
156157
}
158+
159+
type CleanupPayload struct {
160+
ClientID uuid.UUID `json:"client_id" binding:"required" format:"uuid"`
161+
}
162+
163+
func (ctrl CartController) Cleanup(c *gin.Context) {
164+
payload := &CleanupPayload{}
165+
166+
err := c.BindJSON(payload)
167+
168+
if err != nil {
169+
ctrl.Error(c, errutil.NewInputError(err))
170+
return
171+
}
172+
173+
err = ctrl.service.Cleanup(payload.ClientID)
174+
175+
if err != nil {
176+
ctrl.Error(c, err)
177+
return
178+
}
179+
180+
c.Status(http.StatusOK)
181+
}
182+
183+
type LoadCardPayload struct {
184+
ClientID uuid.UUID `json:"client_id" binding:"required" format:"uuid"`
185+
}
186+
187+
func (ctrl CartController) LoadCart(c *gin.Context) {
188+
payload := &LoadCardPayload{}
189+
190+
err := c.BindJSON(payload)
191+
192+
if err != nil {
193+
ctrl.Error(c, errutil.NewInputError(err))
194+
return
195+
}
196+
197+
cart, err := ctrl.service.LoadCart(payload.ClientID)
198+
199+
if err != nil {
200+
ctrl.Error(c, err)
201+
return
202+
}
203+
204+
c.JSON(http.StatusOK, cart)
205+
}

internal/adapters/rest/controller/product_controller.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package controller
22

33
import (
4+
"net/http"
5+
46
"github.com/gin-gonic/gin"
57
"github.com/google/uuid"
68
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
79
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/errutil"
8-
"net/http"
910
)
1011

1112
type ProductController struct {
@@ -67,3 +68,21 @@ func (ctrl *ProductController) Delete(c *gin.Context) {
6768

6869
c.JSON(http.StatusNoContent, gin.H{})
6970
}
71+
72+
func (ctrl *ProductController) GetById(c *gin.Context) {
73+
id, err := uuid.Parse(c.Param("id"))
74+
75+
if err != nil {
76+
ctrl.Error(c, errutil.NewInputError(err))
77+
return
78+
}
79+
80+
product, err := ctrl.service.GetByID(id)
81+
82+
if err != nil {
83+
ctrl.Error(c, err)
84+
return
85+
}
86+
87+
c.JSON(http.StatusOK, product)
88+
}

internal/adapters/rest/handler/cart.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ func RegisterCartHandlers(router *gin.Engine, service cart.IService) {
1313
router.POST("/cart/add-product", cartController.AddProduct)
1414
router.POST("/cart/remove-product", cartController.RemoveProduct)
1515
router.POST("/cart/edit-product", cartController.EditProduct)
16+
router.POST("/cart/cleanup", cartController.Cleanup)
17+
router.POST("/cart/loadcart", cartController.LoadCart)
18+
1619
}

internal/adapters/rest/handler/product.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func RegisterProductHandlers(router *gin.Engine, service product.IProductService
1010
productController := controller.NewProductController(service)
1111

1212
router.GET("/product", productController.Search)
13+
router.GET("product/:id", productController.GetById)
1314

1415
router.DELETE("/product/:id")
1516
}

0 commit comments

Comments
 (0)