From 67d79b7725244321a003d82f55ac45944c0da683 Mon Sep 17 00:00:00 2001 From: saeid_01 Date: Fri, 10 Nov 2023 12:18:14 +0330 Subject: [PATCH] feat: add list per product Category --- api/handlers/products/product.go | 22 ++++++++++++++++++ api/handlers/products/router.go | 3 ++- api/middlewares/router.go | 2 +- database/seeds/fillTables.go | 2 +- domain/product.go | 2 ++ repository/product/product.go | 38 ++++++++++++++++++++++++-------- usecase/product/service.go | 4 ++++ 7 files changed, 61 insertions(+), 12 deletions(-) diff --git a/api/handlers/products/product.go b/api/handlers/products/product.go index 103591a..4ad48c9 100644 --- a/api/handlers/products/product.go +++ b/api/handlers/products/product.go @@ -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 diff --git a/api/handlers/products/router.go b/api/handlers/products/router.go index 8e35fb3..0983548 100644 --- a/api/handlers/products/router.go +++ b/api/handlers/products/router.go @@ -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) diff --git a/api/middlewares/router.go b/api/middlewares/router.go index cb5638a..4051ef7 100644 --- a/api/middlewares/router.go +++ b/api/middlewares/router.go @@ -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) diff --git a/database/seeds/fillTables.go b/database/seeds/fillTables.go index ca0d011..d5e6e87 100644 --- a/database/seeds/fillTables.go +++ b/database/seeds/fillTables.go @@ -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) diff --git a/domain/product.go b/domain/product.go index ca9f809..1293b2c 100644 --- a/domain/product.go +++ b/domain/product.go @@ -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) diff --git a/repository/product/product.go b/repository/product/product.go index cc93d18..3033595 100644 --- a/repository/product/product.go +++ b/repository/product/product.go @@ -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() diff --git a/usecase/product/service.go b/usecase/product/service.go index 1a22366..b490c55 100644 --- a/usecase/product/service.go +++ b/usecase/product/service.go @@ -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) }