first commit
This commit is contained in:
21
handler/handler.go
Normal file
21
handler/handler.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"git.coopgo.io/coopgo-platform/routing-service"
|
||||
"github.com/spf13/viper"
|
||||
"solidarity-service/storage"
|
||||
)
|
||||
|
||||
type SolidarityServiceHandler struct {
|
||||
Config *viper.Viper
|
||||
Routing routing.RoutingService
|
||||
Storage storage.Storage
|
||||
}
|
||||
|
||||
func NewSolidarityServiceHandler(cfg *viper.Viper, routing routing.RoutingService, storage storage.Storage) (*SolidarityServiceHandler, error) {
|
||||
return &SolidarityServiceHandler{
|
||||
Config: cfg,
|
||||
Routing: routing,
|
||||
Storage: storage,
|
||||
}, nil
|
||||
}
|
||||
14
handler/haversine_distance.go
Normal file
14
handler/haversine_distance.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/paulmach/orb"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"solidarity-service/servers/utils"
|
||||
)
|
||||
|
||||
func (handler *SolidarityServiceHandler) CalculateDistanceBetweenFeatures(feature1, feature2 *geojson.Feature) int64 {
|
||||
coords1 := feature1.Geometry.(orb.Point)
|
||||
coords2 := feature2.Geometry.(orb.Point)
|
||||
distance := utils.Haversine(coords1[1], coords1[0], coords2[1], coords2[0])
|
||||
return int64(distance)
|
||||
}
|
||||
23
handler/routing.go
Normal file
23
handler/routing.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/paulmach/orb"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RoutingService interface {
|
||||
Route(locations []orb.Point) (route *Route, err error)
|
||||
}
|
||||
type Route struct {
|
||||
Summary RouteSummary
|
||||
Legs []RouteLeg
|
||||
}
|
||||
|
||||
type RouteSummary struct {
|
||||
Polyline string
|
||||
}
|
||||
type RouteLeg struct {
|
||||
Distance float64
|
||||
Duration time.Duration
|
||||
Polyline string
|
||||
}
|
||||
98
handler/services.go
Normal file
98
handler/services.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"solidarity-service/internal"
|
||||
)
|
||||
|
||||
func (s *SolidarityServiceHandler) GetPassenger(ctx context.Context, id string) (passenger internal.Passenger, err error) {
|
||||
passenger, err = s.Storage.GetPassenger(id)
|
||||
if err != nil {
|
||||
return internal.Passenger{}, err
|
||||
}
|
||||
return passenger, nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) GetDriver(ctx context.Context, id string) (driver internal.Driver, err error) {
|
||||
driver, err = s.Storage.GetDriver(id)
|
||||
if err != nil {
|
||||
return internal.Driver{}, err
|
||||
}
|
||||
return driver, nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) SetDriverAvailabilities(ctx context.Context, driver internal.Driver) (err error) {
|
||||
driver.Driver.Operator = "internal-grpc"
|
||||
err = s.Storage.CreateDriver(driver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) SetPassengerTrip(ctx context.Context, passenger internal.Passenger) (err error) {
|
||||
passenger.Passenger.Operator = "internal-grpc"
|
||||
err = s.Storage.CreatePassenger(passenger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) CreateBooking(ctx context.Context, bookingRequest internal.BookingRequest) (passenger internal.Passenger, driver internal.Driver, err error) {
|
||||
bookingRequest.Operator = "internal-grpc"
|
||||
err = s.Storage.CreateBooking(bookingRequest)
|
||||
if err != nil {
|
||||
return internal.Passenger{}, internal.Driver{}, err
|
||||
}
|
||||
passenger, err = s.Storage.GetPassenger(bookingRequest.Passenger_id)
|
||||
if err != nil {
|
||||
return internal.Passenger{}, internal.Driver{}, err
|
||||
}
|
||||
driver, err = s.Storage.GetDriver(bookingRequest.Driver_id)
|
||||
if err != nil {
|
||||
return internal.Passenger{}, internal.Driver{}, err
|
||||
}
|
||||
return passenger, driver, nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) UpdateBooking(ctx context.Context, bookingId string, status internal.BookingStatus) (err error) {
|
||||
err = s.Storage.UpdateBookingStatus(bookingId, status)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) GetBooking(ctx context.Context, bookingId string) (booking internal.Booking, passenger internal.Passenger, driver internal.Driver, err error) {
|
||||
booking, err = s.Storage.GetBooking(bookingId)
|
||||
if err != nil {
|
||||
return internal.Booking{}, internal.Passenger{}, internal.Driver{}, err
|
||||
}
|
||||
passenger, err = s.Storage.GetPassenger(booking.Passenger.ID)
|
||||
if err != nil {
|
||||
return internal.Booking{}, internal.Passenger{}, internal.Driver{}, err
|
||||
}
|
||||
driver, err = s.Storage.GetDriver(booking.Driver.ID)
|
||||
if err != nil {
|
||||
return internal.Booking{}, internal.Passenger{}, internal.Driver{}, err
|
||||
}
|
||||
return booking, passenger, driver, nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) GetBookingsByStatus(ctx context.Context, status string, filterType string, id string) (bookings []internal.Booking, err error) {
|
||||
bookings, err = s.Storage.FilterUserBookingsByStatus(filterType, internal.BookingStatus(status), id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bookings, nil
|
||||
}
|
||||
|
||||
func (s *SolidarityServiceHandler) GetDriverJourneys(ctx context.Context, departure_route *geojson.Feature, departure_date int64) (drivers []internal.Driver, err error) {
|
||||
drivers, err = s.Storage.DriverJourneys(departure_route, departure_date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return drivers, nil
|
||||
}
|
||||
52
handler/trip_duration.go
Normal file
52
handler/trip_duration.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (handler *SolidarityServiceHandler) CalculateDurationBetweenFeatures(feature1, feature2 *geojson.Feature) (int64, error) {
|
||||
coords1 := feature1.Point()
|
||||
coords2 := feature2.Point()
|
||||
routing_base_url := handler.Config.GetString("routing.valhalla.base_url")
|
||||
url := fmt.Sprintf("%sroute?json=%s", routing_base_url, createJSONROUTERequest(coords1.X(), coords1.Y(), coords2.X(), coords2.Y()))
|
||||
response, err := http.Get(url)
|
||||
if err != nil {
|
||||
return 0, errors.New("routing service error")
|
||||
}
|
||||
var result map[string]interface{}
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&result); err != nil {
|
||||
return 0, errors.New("routing response decoding error")
|
||||
}
|
||||
trip, ok := result["trip"].(map[string]interface{})
|
||||
if !ok {
|
||||
return 0, errors.New("routing response decoding error")
|
||||
}
|
||||
summary, ok := trip["summary"].(map[string]interface{})
|
||||
if !ok {
|
||||
return 0, errors.New("routing response decoding error")
|
||||
}
|
||||
duration, ok := summary["time"].(float64)
|
||||
if !ok {
|
||||
return 0, errors.New("routing response decoding error")
|
||||
}
|
||||
|
||||
return int64(duration), nil
|
||||
}
|
||||
|
||||
func createJSONROUTERequest(lat1, lon1, lat2, lon2 float64) string {
|
||||
request := map[string]interface{}{
|
||||
"locations": []map[string]float64{
|
||||
{"lat": lat1, "lon": lon1},
|
||||
{"lat": lat2, "lon": lon2},
|
||||
},
|
||||
"costing": "auto",
|
||||
}
|
||||
|
||||
jsonRequest, _ := json.Marshal(request)
|
||||
return string(jsonRequest)
|
||||
}
|
||||
Reference in New Issue
Block a user