-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from WilliamChang80/feature/CRUD-product
Feature/crud product
- Loading branch information
Showing
25 changed files
with
1,713 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
Oops, something went wrong.