@@ -3,6 +3,7 @@ package sight
33import (
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 }
0 commit comments