Skip to content

Commit e9f504c

Browse files
committed
add UpdateProduct method and endpoint for updating a product
1 parent def099f commit e9f504c

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
server.GET("/products", ProductController.GetProducts)
3434
server.POST("/product", ProductController.CreateProduct)
3535
server.GET("/product/:productId", ProductController.GetProductsById)
36+
server.PUT("/product", ProductController.UpdateProduct)
3637

3738
server.Run(":8000")
3839

controller/product_controller.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (p *productController) GetProductsById(ctx *gin.Context) {
5151

5252
id := ctx.Param("productId")
5353
if id == "" {
54-
response := model.Reponse{
54+
response := model.Response{
5555
Message: "Product ID cannot be null",
5656
}
5757
ctx.JSON(http.StatusBadRequest, response)
@@ -60,7 +60,7 @@ func (p *productController) GetProductsById(ctx *gin.Context) {
6060

6161
productId, err := strconv.Atoi(id)
6262
if err != nil {
63-
response := model.Reponse{
63+
response := model.Response{
6464
Message: "Product Id need to be number",
6565
}
6666
ctx.JSON(http.StatusBadRequest, response)
@@ -74,7 +74,7 @@ func (p *productController) GetProductsById(ctx *gin.Context) {
7474
}
7575

7676
if product == nil {
77-
response := model.Reponse{
77+
response := model.Response{
7878
Message: "Product not existing in database",
7979
}
8080
ctx.JSON(http.StatusNotFound, response)
@@ -83,3 +83,20 @@ func (p *productController) GetProductsById(ctx *gin.Context) {
8383

8484
ctx.JSON(http.StatusOK, product)
8585
}
86+
87+
func (p *productController) UpdateProduct(ctx *gin.Context) {
88+
var product model.Product
89+
err := ctx.BindJSON(&product)
90+
if err != nil {
91+
ctx.JSON(http.StatusBadRequest, err)
92+
return
93+
}
94+
95+
err = p.productUseCase.UpdateProduct(product)
96+
if err != nil {
97+
ctx.JSON(http.StatusInternalServerError, err)
98+
return
99+
}
100+
101+
ctx.JSON(http.StatusOK, product)
102+
}

model/response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package model
22

3-
type Reponse struct {
3+
type Response struct {
44
Message string
55
}

repository/product_repository.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,20 @@ func (pr *ProductRepository) GetProductById(id_product int) (*model.Product, err
9494
query.Close()
9595
return &produto, nil
9696
}
97+
98+
func (pr *ProductRepository) UpdateProduct(product model.Product) error {
99+
query, err := pr.connection.Prepare("UPDATE product SET product_name = $1, price = $2 WHERE id = $3")
100+
if err != nil {
101+
fmt.Println(err)
102+
return err
103+
}
104+
defer query.Close()
105+
106+
_, err = query.Exec(product.Name, product.Price, product.ID)
107+
if err != nil {
108+
fmt.Println(err)
109+
return err
110+
}
111+
112+
return nil
113+
}

usecase/product_usecase.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@ func (pu *ProductUseCase) GetProducts() ([]model.Product, error) {
2020
}
2121

2222
func (pu *ProductUseCase) CreateProduct(product model.Product) (model.Product, error) {
23-
2423
productId, err := pu.repository.CreateProduct(product)
2524
if err != nil {
2625
return model.Product{}, err
2726
}
28-
2927
product.ID = productId
30-
3128
return product, nil
3229
}
3330

3431
func (pu *ProductUseCase) GetProductById(id_product int) (*model.Product, error) {
35-
3632
product, err := pu.repository.GetProductById(id_product)
3733
if err != nil {
3834
return nil, err
3935
}
40-
4136
return product, nil
4237
}
38+
39+
func (pu *ProductUseCase) UpdateProduct(product model.Product) error {
40+
return pu.repository.UpdateProduct(product)
41+
}

0 commit comments

Comments
 (0)