Skip to content

Commit cabead8

Browse files
committed
modified search
1 parent 5ed13ec commit cabead8

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

internal/delivery/handlers/sight/sight.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ func NewSightsHandler(usecase usecase.SightUseCaseInterface) *SightHandler {
2828
// @Success 200 {array} sight.Sight
2929
// @Router /sights [get]
3030
func (h *SightHandler) GetSights(ctx context.Context, _ entities.Sight) (entities.Sights, error) {
31-
params := httputils.GetQueryParamsFromCtx(ctx)
32-
categoryID, _ := strconv.Atoi(params["category_id"])
33-
sights, err := h.SightUseCase.GetSightsList(categoryID)
31+
sights, err := h.SightUseCase.GetSightsList()
3432
if err != nil {
3533
return entities.Sights{}, err
3634
}
@@ -62,8 +60,8 @@ func (h *SightHandler) GetSight(ctx context.Context, _ entities.Sight) (entities
6260
return entities.SightComments{Sight: sight, Comms: comments}, nil
6361
}
6462

65-
func (h *SightHandler) SearchSights(_ context.Context, requestData entities.Sight) (entities.Sights, error) {
66-
sights, err := h.SightUseCase.SearchSights(requestData.Name)
63+
func (h *SightHandler) SearchSights(ctx context.Context, requestData entities.Sight) (entities.Sights, error) {
64+
sights, err := h.SightUseCase.SearchSights(httputils.GetQueryParamsFromCtx(ctx))
6765
if err != nil {
6866
return entities.Sights{}, err
6967
}

internal/storage/postgres/sight/sight_storage.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sight
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/go-park-mail-ru/2024_1_ResCogitans/internal/entities"
89
storage "github.com/go-park-mail-ru/2024_1_ResCogitans/internal/storage/storage_interfaces"
@@ -24,7 +25,7 @@ func NewSightStorage(db *pgxpool.Pool) storage.SightStorageInterface {
2425
}
2526
}
2627

27-
func (ss *SightStorage) GetSightsList(categoryID int) ([]entities.Sight, error) {
28+
func (ss *SightStorage) GetSightsList() ([]entities.Sight, error) {
2829
var sights []*entities.Sight
2930
ctx := context.Background()
3031

@@ -40,13 +41,7 @@ func (ss *SightStorage) GetSightsList(categoryID int) ([]entities.Sight, error)
4041
INNER JOIN image_data AS im
4142
ON sight.id = im.sight_id`
4243

43-
args := make([]interface{}, 0)
44-
if categoryID != 0 {
45-
query += ` WHERE category_id = $1`
46-
args = append(args, categoryID)
47-
}
48-
49-
err := pgxscan.Select(ctx, ss.db, &sights, query, args...)
44+
err := pgxscan.Select(ctx, ss.db, &sights, query)
5045
if err != nil {
5146
logger.Logger().Error(err.Error())
5247
return nil, err
@@ -92,13 +87,25 @@ func (ss *SightStorage) GetSight(id int) (entities.Sight, error) {
9287
return *sight[0], nil
9388
}
9489

95-
func (ss *SightStorage) SearchSights(str string) (entities.Sights, error) {
96-
query := `
97-
SELECT id, rating, name, description, city_id, country_id
98-
FROM sight
99-
WHERE LOWER(name) LIKE LOWER($1)
100-
`
101-
rows, err := ss.db.Query(context.Background(), query, "%"+str+"%")
90+
func (ss *SightStorage) SearchSights(searchParams map[string]string) (entities.Sights, error) {
91+
var query strings.Builder
92+
query.WriteString("SELECT id, rating, name, description, city_id, country_id, category_id FROM sight WHERE ")
93+
94+
first := true
95+
for key, value := range searchParams {
96+
if !first {
97+
query.WriteString(" AND ")
98+
} else {
99+
first = false
100+
}
101+
if key == "rating" || key == "city_id" || key == "country_id" || key == "category_id" {
102+
query.WriteString(fmt.Sprintf("%s = %s", key, value))
103+
} else {
104+
query.WriteString(fmt.Sprintf("LOWER(%s) LIKE LOWER('%%%s%%')", key, value))
105+
}
106+
}
107+
108+
rows, err := ss.db.Query(context.Background(), query.String())
102109
if err != nil {
103110
return entities.Sights{}, err
104111
}
@@ -107,7 +114,7 @@ func (ss *SightStorage) SearchSights(str string) (entities.Sights, error) {
107114
var sights entities.Sights
108115
for rows.Next() {
109116
var sight entities.Sight
110-
err := rows.Scan(&sight.ID, &sight.Rating, &sight.Name, &sight.Description, &sight.CityID, &sight.CountryID)
117+
err := rows.Scan(&sight.ID, &sight.Rating, &sight.Name, &sight.Description, &sight.CityID, &sight.CountryID, &sight.CategoryID)
111118
if err != nil {
112119
return entities.Sights{}, err
113120
}

internal/storage/storage_interfaces/sight_interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
type SightStorageInterface interface {
8-
GetSightsList(categoryID int) ([]entities.Sight, error)
8+
GetSightsList() ([]entities.Sight, error)
99
GetSight(sightID int) (entities.Sight, error)
10-
SearchSights(str string) (entities.Sights, error)
10+
SearchSights(searchParams map[string]string) (entities.Sights, error)
1111
GetCommentsBySightID(commentID int) ([]entities.Comment, error)
1212
GetCommentsByUserID(userID int) ([]entities.Comment, error)
1313
CreateCommentBySightID(sightID int, comment entities.Comment) error

internal/usecase/sight_usecase.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ type SightUseCaseInterface interface {
99
GetSightByID(sightID int) (entities.Sight, error)
1010
GetCommentsBySightID(commentID int) ([]entities.Comment, error)
1111
GetCommentsByUserID(userID int) ([]entities.Comment, error)
12-
GetSightsList(categoryID int) ([]entities.Sight, error)
13-
SearchSights(str string) (entities.Sights, error)
12+
GetSightsList() ([]entities.Sight, error)
13+
SearchSights(searchParams map[string]string) (entities.Sights, error)
1414
}
1515

1616
type SightUseCase struct {
@@ -35,10 +35,10 @@ func (su *SightUseCase) GetCommentsByUserID(userID int) ([]entities.Comment, err
3535
return su.SightStorage.GetCommentsByUserID(userID)
3636
}
3737

38-
func (su *SightUseCase) GetSightsList(categoryID int) ([]entities.Sight, error) {
39-
return su.SightStorage.GetSightsList(categoryID)
38+
func (su *SightUseCase) GetSightsList() ([]entities.Sight, error) {
39+
return su.SightStorage.GetSightsList()
4040
}
4141

42-
func (su *SightUseCase) SearchSights(str string) (entities.Sights, error) {
43-
return su.SightStorage.SearchSights(str)
42+
func (su *SightUseCase) SearchSights(searchParams map[string]string) (entities.Sights, error) {
43+
return su.SightStorage.SearchSights(searchParams)
4444
}

0 commit comments

Comments
 (0)