Skip to content

Commit 2b6c442

Browse files
committed
add product controller and endpoint to retrieve products
1 parent 3e5411e commit 2b6c442

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cmd/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package main
22

33
import (
4+
"api-go/controller"
5+
46
"github.com/gin-gonic/gin"
57
)
68

79
func main() {
810

911
server := gin.Default()
1012

13+
ProductController := controller.NewProductController()
14+
1115
server.GET("/ping", func(ctx *gin.Context) {
1216
ctx.JSON(200, gin.H{
1317
"message": "primeiros testes",
1418
})
1519
})
1620

21+
server.GET("/products", ProductController.GetProducts)
22+
1723
server.Run(":8000")
1824

1925
}

controller/product_controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package controller
2+
3+
import (
4+
"api-go/model"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
type productController struct{}
11+
12+
func NewProductController() *productController {
13+
return &productController{}
14+
}
15+
16+
func (p *productController) GetProducts(ctx *gin.Context) {
17+
products := []model.Product{
18+
{ID: 1, Name: "Produto 1", Price: 100.00},
19+
}
20+
ctx.JSON(http.StatusOK, products)
21+
}

0 commit comments

Comments
 (0)