Skip to content

Commit

Permalink
Merge pull request #2 from WilliamChang80/feature/CRUD-product
Browse files Browse the repository at this point in the history
Feature/crud product
  • Loading branch information
WilliamChang80 authored Sep 3, 2020
2 parents 98de945 + d67a833 commit dfa9aeb
Show file tree
Hide file tree
Showing 25 changed files with 1,713 additions and 8 deletions.
7 changes: 7 additions & 0 deletions common/constants/response/message_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package response

const (
SUCCESS = "success"
NOT_FOUND = "not found"
BAD_REQUEST = "bad request"
)
109 changes: 109 additions & 0 deletions controller/http/product/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package product

import (
"github.com/labstack/echo"
message "github.com/williamchang80/sea-apd/common/constants/response"
"github.com/williamchang80/sea-apd/domain/product"
"github.com/williamchang80/sea-apd/dto/domain"
request "github.com/williamchang80/sea-apd/dto/request/product"
"github.com/williamchang80/sea-apd/dto/response/base"
response "github.com/williamchang80/sea-apd/dto/response/product"
"net/http"
)

type ProductController struct {
usecase product.ProductUsecase
}

func NewProductController(e *echo.Echo, p product.ProductUsecase) product.ProductController {
c := &ProductController{
usecase: p,
}
e.GET("/products", c.GetProducts)
e.POST("/products", c.CreateProduct)
e.GET("/product", c.GetProductById)
e.PUT("/product", c.UpdateProduct)
e.DELETE("/product", c.DeleteProduct)
return c
}

func (p *ProductController) GetProducts(c echo.Context) error {
products, err := p.usecase.GetProducts()
if err != nil {
c.JSON(http.StatusNotFound, &base.BaseResponse{
Code: http.StatusNotFound,
Message: message.NOT_FOUND,
})
}

return c.JSON(http.StatusOK, &response.GetProductsResponse{
Code: http.StatusOK,
Message: message.SUCCESS,
Data: domain.ProductListDto{Products: products},
})
}

func (p *ProductController) CreateProduct(c echo.Context) error {
var productRequest request.ProductRequest
c.Bind(&productRequest)
if err := p.usecase.CreateProduct(productRequest); err != nil {
return c.JSON(http.StatusUnprocessableEntity, &base.BaseResponse{
Code: http.StatusBadRequest,
Message: message.BAD_REQUEST,
})
}
return c.JSON(http.StatusOK, &base.BaseResponse{
Code: http.StatusCreated,
Message: message.SUCCESS,
})
}

func (p *ProductController) GetProductById(context echo.Context) error {
id := context.QueryParam("productId")
product, err := p.usecase.GetProductById(id)
if err != nil {
return context.JSON(http.StatusNotFound, &base.BaseResponse{
Code: http.StatusNotFound,
Message: message.NOT_FOUND,
})
}
return context.JSON(http.StatusOK, &response.GetProductByIdResponse{
Code: http.StatusOK,
Message: message.SUCCESS,
Data: domain.ProductDto{
Product: *product,
},
})
}

func (p *ProductController) UpdateProduct(context echo.Context) error {
var productRequest request.ProductRequest
context.Bind(&productRequest)
productId := context.FormValue("productId")
err := p.usecase.UpdateProduct(productId, productRequest)
if err != nil {
return context.JSON(http.StatusBadRequest, &base.BaseResponse{
Code: http.StatusBadRequest,
Message: message.BAD_REQUEST,
})
}
return context.JSON(http.StatusOK, &base.BaseResponse{
Code: http.StatusOK,
Message: message.SUCCESS,
})
}

func (p *ProductController) DeleteProduct(context echo.Context) error {
id := context.QueryParam("productId")
err := p.usecase.DeleteProduct(id)
if err != nil {
return context.JSON(http.StatusNotFound, &base.BaseResponse{
Code: http.StatusNotFound,
Message: message.NOT_FOUND,
})
}
return context.JSON(http.StatusOK, &base.BaseResponse{
Code: http.StatusOK,
Message: message.SUCCESS,
})
}
Loading

0 comments on commit dfa9aeb

Please sign in to comment.