Skip to content

Commit

Permalink
Merge pull request #13 from Ulas-Scan/development
Browse files Browse the repository at this point in the history
feat: filter hstory by product name
  • Loading branch information
iyoubee authored Jun 15, 2024
2 parents 54a1add + a0b7454 commit 071e677
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 5 additions & 2 deletions controller/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ func (c *historyController) GetHistories(ctx *gin.Context) {
return
}

productName := ctx.Query("product_name")

req := dto.HistoriesGetRequest{
Page: page,
Limit: limit,
Page: page,
Limit: limit,
ProductName: productName,
}

result, err := c.historyService.GetHistories(ctx.Request.Context(), req, userId)
Expand Down
5 changes: 3 additions & 2 deletions dto/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ var (

type (
HistoriesGetRequest struct {
Page int `json:"page"`
Limit int `json:"limit"`
Page int `json:"page"`
Limit int `json:"limit"`
ProductName string `json:"product_name"`
}

HistoriesResponse struct {
Expand Down
7 changes: 6 additions & 1 deletion repository/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ func (r *historyRepository) GetHistories(ctx context.Context, tx *gorm.DB, dto d
return []entity.History{}, 0, err
}

scope := tx.WithContext(ctx)
if dto.ProductName != "" {
scope = scope.Where("product_name = ?", dto.ProductName)
}

// Query the paginated records
err = tx.WithContext(ctx).
err = scope.
Where("user_id = ?", userId).
Limit(limit).Offset(offset).
Find(&histories).Error
Expand Down

0 comments on commit 071e677

Please sign in to comment.