feat: add list per product Category

This commit is contained in:
saeid_01 2023-11-10 12:18:14 +03:30
parent 2885bd9a17
commit 67d79b7725
7 changed files with 61 additions and 12 deletions

View File

@ -40,6 +40,28 @@ func (c *controller) listProduct(ctx *gin.Context) {
utils.ResponseJSON(ctx, http.StatusOK, "successful", products)
}
// ListProducts godoc
// @Summary List Products
// @Description get Products
// @Tags Products
// @Accept json
// @Produce json
// @Param slug path string true "product Category Slug"
// @Param q query string false "name search by q" Format(email)
// @Success 200 {array} entity.ProductDisplay
// @Failure 400 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /products/category/:slug [get]
func (c *controller) listProductPerCategory(ctx *gin.Context) {
slug := ctx.Param("slug")
products, err := c.service.ListProductPerCategory(slug)
if err != nil {
utils.ResponseJSON(ctx, http.StatusBadRequest, err.Error(), nil)
return
}
utils.ResponseJSON(ctx, http.StatusOK, "successful", products)
}
// ShowProduct godoc
// @Summary Show an product
// @Description get string by ID

View File

@ -7,7 +7,7 @@ import (
)
func NewProductRouters(server *entity.Routers) {
productRepo := repository_product.NewProductClient(server.Database, &server.ActiveBusiness)
productRepo := repository_product.NewProductRepository(server.Database, &server.ActiveBusiness)
productService := service_product.NewProductService(productRepo)
@ -16,6 +16,7 @@ func NewProductRouters(server *entity.Routers) {
// product management
api_product := server.OpenApp.Group("products")
api_product.GET("/", productController.listProduct)
api_product.GET("/category/:slug", productController.listProductPerCategory)
api_product.GET(":id", productController.getProduct)
api_product.PUT(":id", productController.update)
api_product.DELETE(":id", productController.deleteProduct)

View File

@ -1,7 +1,7 @@
package middlewares
//func NewMiddlewareRouters(server *entity.Routers) *controller {
//productRepo := repository_product.NewProductClient(server.Database)
//productRepo := repository_product.NewProductRepository(server.Database)
//authService := service_authentication.NewAuthService(productRepo)
//return NewMiddlewareControllers(authService)

View File

@ -57,7 +57,7 @@ func FillProducts(server *entity.Routers) {
Domain: "localhost",
}
productRepo := repository_product.NewProductClient(server.Database, &activeBusiness)
productRepo := repository_product.NewProductRepository(server.Database, &activeBusiness)
productService := service_product.NewProductService(productRepo)

View File

@ -7,6 +7,7 @@ import (
type ProductRepository interface {
List() ([]*entity.ProductDisplay, error)
ListProductPerCategory(slug string) ([]*entity.ProductDisplay, error)
Create(p *entity.ProductCreateUpdate) error
GetByID(id int) (*entity.ProductDisplay, error)
SearchProduct(identifier string) (*entity.ProductDisplay, error)
@ -16,6 +17,7 @@ type ProductRepository interface {
type ProductService interface {
List() ([]*entity.ProductDisplay, error)
ListProductPerCategory(slug string) ([]*entity.ProductDisplay, error)
Create(u *entity.ProductCreateUpdate) error
GetByID(id int) (*entity.ProductDisplay, error)
SearchProduct(identifier string) (*entity.ProductDisplay, error)

View File

@ -4,23 +4,24 @@ import (
"context"
"online-order/ent"
"online-order/ent/product"
"online-order/ent/productcategory"
"online-order/entity"
)
type ProductClient struct {
type ProductRepository struct {
client *ent.Client
activeBusiness *entity.ActiveBusiness
}
func NewProductClient(client *ent.Client, activeBusiness *entity.ActiveBusiness) *ProductClient {
return &ProductClient{
func NewProductRepository(client *ent.Client, activeBusiness *entity.ActiveBusiness) *ProductRepository {
return &ProductRepository{
client: client,
activeBusiness: activeBusiness,
}
}
// List all products
func (c *ProductClient) List() ([]*entity.ProductDisplay, error) {
func (c *ProductRepository) List() ([]*entity.ProductDisplay, error) {
var u []*entity.ProductDisplay
ctx := context.Background()
@ -37,8 +38,27 @@ func (c *ProductClient) List() ([]*entity.ProductDisplay, error) {
return u, nil
}
// List all products
func (c *ProductRepository) ListProductPerCategory(slug string) ([]*entity.ProductDisplay, error) {
var u []*entity.ProductDisplay
ctx := context.Background()
err := c.client.Product.
Query().
Where(product.BusinessID(c.activeBusiness.BusinessID)).
Where(product.HasProductCategoriesWith(productcategory.Slug(slug))).
Select(product.FieldID, product.FieldName, product.FieldDescription, product.FieldOriginalPrice, product.FieldPrice, product.FieldQuantity, product.FieldStatus, product.FieldCreatedAt, product.FieldUpdatedAt).
Scan(ctx, &u)
if err != nil {
return nil, err
}
return u, nil
}
// Create a product
func (c *ProductClient) Create(p *entity.ProductCreateUpdate) error {
func (c *ProductRepository) Create(p *entity.ProductCreateUpdate) error {
ctx := context.Background()
resp, err := c.client.Product.
@ -65,7 +85,7 @@ func (c *ProductClient) Create(p *entity.ProductCreateUpdate) error {
return nil
}
func (c *ProductClient) GetByID(id int) (*entity.ProductDisplay, error) {
func (c *ProductRepository) GetByID(id int) (*entity.ProductDisplay, error) {
var p entity.ProductDisplay
ctx := context.Background()
@ -93,7 +113,7 @@ func (c *ProductClient) GetByID(id int) (*entity.ProductDisplay, error) {
}
// Update product information, except password
func (c *ProductClient) Update(p *entity.ProductCreateUpdate) error {
func (c *ProductRepository) Update(p *entity.ProductCreateUpdate) error {
ctx := context.Background()
_, err := c.client.Product.UpdateOneID(p.ID).
@ -111,7 +131,7 @@ func (c *ProductClient) Update(p *entity.ProductCreateUpdate) error {
}
// Update user information, except password
func (c *ProductClient) Delete(id int) error {
func (c *ProductRepository) Delete(id int) error {
ctx := context.Background()
err := c.client.Product.
@ -121,7 +141,7 @@ func (c *ProductClient) Delete(id int) error {
}
// Search a user information by email or username
func (c *ProductClient) SearchProduct(identifier string) (*entity.ProductDisplay, error) {
func (c *ProductRepository) SearchProduct(identifier string) (*entity.ProductDisplay, error) {
var p entity.ProductDisplay
ctx := context.Background()

View File

@ -19,6 +19,10 @@ func (s *productservice) List() ([]*entity.ProductDisplay, error) {
return s.repo.List()
}
func (s *productservice) ListProductPerCategory(slug string) ([]*entity.ProductDisplay, error) {
return s.repo.ListProductPerCategory(slug)
}
func (s *productservice) Create(p *entity.ProductCreateUpdate) error {
return s.repo.Create(p)
}