Skip to content

Commit

Permalink
fix: product controller params
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui committed Jan 9, 2024
1 parent 7ba1513 commit cd2bcbb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
14 changes: 10 additions & 4 deletions src/controllers/products_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"database/sql"
"strconv"
"strings"

Expand All @@ -12,16 +13,21 @@ import (

// [GET] /products
func (ctr Controller) FindAllProducts(c *fiber.Ctx) error {
var filter types.ProductFilter
if c.BodyParser(&filter) != nil {
return utils.FailResponse(c, "could not parse body data")
search_query := c.Query("search")
filter := types.ProductFilter{
Search: &search_query,
Limit: int32(c.QueryInt("limit")),
Page: int32(c.QueryInt("page")),
}
if err := ctr.Validator.Struct(filter); err != nil {
return utils.FailResponse(c, err.Error())
}

products, err := ctr.Querier.FilterProducts(c.Context(), database.FilterProductsParams{
Search: filter.Search,
Search: sql.NullString{
Valid: filter.Search != nil && *filter.Search != "",
String: *filter.Search,
},
Limit: filter.Limit,
Page: filter.Page,
})
Expand Down
14 changes: 6 additions & 8 deletions src/database/product.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/database/queries/product.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ FROM "product"
INNER JOIN "category"
ON "category"."id" = "product"."category_id"
WHERE
(sqlc.narg(search) IS NULL) OR
"product"."product_ts" @@ to_tsquery('english', sqlc.narg(search))
(sqlc.narg(search)::TEXT IS NULL) OR
"product"."product_ts" @@ to_tsquery('english', sqlc.narg(search)::TEXT)
ORDER BY "weight" DESC
LIMIT $1
OFFSET $1 * (sqlc.arg(page)::INTEGER - 1);
Expand Down
4 changes: 2 additions & 2 deletions src/types/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ type CreateProduct struct {
}

type ProductFilter struct {
Search string `json:"search,omitempty" validate:"omitempty"`
Search *string `json:"search,omitempty" validate:"omitempty"`
Limit int32 `json:"limit" validate:"required,min=1,max=200"`
Page int32 `json:"page" validate:"required,gte=1"`
MinPrice float32 `json:"minPrice,omitempty" validate:"omitempty,gte=1,ltefield=MaxPrice"`
MaxPrice float32 `json:"maxPrice,omitempty" validate:"omitempty,gtefield=MinPrice"`
Sort string `json:"sort,omitempty" validate:"omitempty"`
Sort *string `json:"sort,omitempty" validate:"omitempty"`
}

type Wish struct {
Expand Down

0 comments on commit cd2bcbb

Please sign in to comment.