Skip to content

Commit 878ab3a

Browse files
committed
add GetProductsById method and endpoint for retrieving a product by ID
1 parent 6c023c4 commit 878ab3a

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func main() {
3232

3333
server.GET("/products", ProductController.GetProducts)
3434
server.POST("/product", ProductController.CreateProduct)
35+
server.GET("/product/:productId", ProductController.GetProductsById)
3536

3637
server.Run(":8000")
3738

controller/product_controller.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"api-go/model"
55
"api-go/usecase"
66
"net/http"
7+
"strconv"
78

89
"github.com/gin-gonic/gin"
910
)
@@ -45,3 +46,40 @@ func (p *productController) CreateProduct(ctx *gin.Context) {
4546

4647
ctx.JSON(http.StatusCreated, insertedProduct)
4748
}
49+
50+
func (p *productController) GetProductsById(ctx *gin.Context) {
51+
52+
id := ctx.Param("productId")
53+
if id == "" {
54+
response := model.Reponse{
55+
Message: "Product ID cannot be null",
56+
}
57+
ctx.JSON(http.StatusBadRequest, response)
58+
return
59+
}
60+
61+
productId, err := strconv.Atoi(id)
62+
if err != nil {
63+
response := model.Reponse{
64+
Message: "Product Id need to be number",
65+
}
66+
ctx.JSON(http.StatusBadRequest, response)
67+
return
68+
}
69+
70+
product, err := p.productUseCase.GetProductById(productId)
71+
if err != nil {
72+
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
73+
return
74+
}
75+
76+
if product == nil {
77+
response := model.Reponse{
78+
Message: "Product not existing in database",
79+
}
80+
ctx.JSON(http.StatusNotFound, response)
81+
return
82+
}
83+
84+
ctx.JSON(http.StatusOK, product)
85+
}

model/response.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package model
2+
3+
type Reponse struct {
4+
Message string
5+
}

repository/product_repository.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,31 @@ func (pr *ProductRepository) CreateProduct(product model.Product) (int, error) {
6666
query.Close()
6767
return id, nil
6868
}
69+
70+
func (pr *ProductRepository) GetProductById(id_product int) (*model.Product, error) {
71+
72+
query, err := pr.connection.Prepare("SELECT * FROM product WHERE od = $1")
73+
if err != nil {
74+
fmt.Println(err)
75+
return nil, err
76+
}
77+
78+
var produto model.Product
79+
80+
err = query.QueryRow(id_product).Scan(
81+
&produto.ID,
82+
&produto.Name,
83+
&produto.Price,
84+
)
85+
86+
if err != nil {
87+
if err == sql.ErrNoRows {
88+
return nil, nil
89+
}
90+
91+
return nil, err
92+
}
93+
94+
query.Close()
95+
return &produto, nil
96+
}

usecase/product_usecase.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ func (pu *ProductUseCase) CreateProduct(product model.Product) (model.Product, e
3030

3131
return product, nil
3232
}
33+
34+
func (pu *ProductUseCase) GetProductById(id_product int) (*model.Product, error) {
35+
36+
product, err := pu.repository.GetProductById(id_product)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return product, nil
42+
}

0 commit comments

Comments
 (0)