Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"elastic-service/internal"
"elastic-service/internal/handlers"
pb "elastic-service/pkg/api" // Import generated proto package
pb "elastic-service/pkg/api"
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Expand All @@ -20,7 +20,6 @@ func main() {
return
}

// Get port from command-line argument or use default
defaultPort := strconv.Itoa(internal.DefaultPort)

configuredPort := os.Getenv("PORT")
Expand Down
6 changes: 5 additions & 1 deletion internal/elastic_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ func (ec *ElasticsearchClient) SearchByName(name string) ([]*pb.City, error) {
}

// SearchByCoords searches for a city by coordinates
func (ec *ElasticsearchClient) SearchByCoords(coords *pb.Coords) ([]*pb.City, error) {
func (ec *ElasticsearchClient) SearchByCoords(coords *pb.Coords, radius string) ([]*pb.City, error) {
distance := os.Getenv(geoDistance)

if distance == "" {
distance = defaultDistance
}

if radius != "" {
distance = radius
}

query := fmt.Sprintf(`{
"query": {
"bool": {
Expand Down
4 changes: 2 additions & 2 deletions internal/elastic_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestSearchMoscowByCoords(t *testing.T) {
Lon: 37.617777777778,
}

cities, err := ecClient.SearchByCoords(moscowCoords)
cities, err := ecClient.SearchByCoords(moscowCoords, defaultDistance)
assert.NoError(t, err, "Search by coords failed")
assert.NotEmpty(t, cities, "Expected at least one city, got none")

Expand All @@ -105,7 +105,7 @@ func TestSearchByNonExistentCoords(t *testing.T) {
Lon: 1000.0,
}

cities, err := ecClient.SearchByCoords(nonExistentCoords)
cities, err := ecClient.SearchByCoords(nonExistentCoords, defaultDistance)
assert.NoError(t, err, "Search by non-existent coordinates failed")
assert.Empty(t, cities, "Expected empty result for non-existent coordinates")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/handlers/coords_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ func (s *Server) SearchByCoords(ctx context.Context, req *pb.CoordsRequest) (*pb

lat := req.GetLat()
lon := req.GetLon()
radius := req.GetRadius()

coords, err := pb.NewCoords(lat, lon)
if err != nil {
response.Code = pb.CitySearchCoordsResponse_INVALID_COORDS
return response, nil
}

cities, err := s.client.SearchByCoords(coords)
cities, err := s.client.SearchByCoords(coords, radius)
if err != nil {
response.Code = pb.CitySearchCoordsResponse_INTERNAL_ERROR_COORDS
return response, nil
Expand Down
40 changes: 26 additions & 14 deletions pkg/api/elastic_service.pb.go

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

3 changes: 1 addition & 2 deletions proto/elastic_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ message CitySearchCoordsResponse{
repeated City options = 2;
}



message CityNameRequest {
string name = 1;
}

message CoordsRequest {
float lat = 1;
float lon = 2;
optional string radius = 3;
}


Expand Down