parcoursmob/handlers/application/solidarity_service.go

133 lines
3.1 KiB
Go

package application
import (
"context"
"fmt"
"net/http"
"time"
solidarity_service "git.coopgo.io/sbouaram/solidarity-service/servers/grpc/proto"
"github.com/google/uuid"
geojson "github.com/paulmach/go.geojson"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (h *ApplicationHandler) DriversJourney(w http.ResponseWriter, r *http.Request) {
accounts, err := h.beneficiaries(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
cacheid := uuid.NewString()
h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour)
bookingData := solidarity_service.CreateBookingSolidarityRequest {
}
if r.Method != "GET" {
DepartureAddress := r.FormValue("departure");
DestinationAddress := r.FormValue("destination");
PickupDate := r.FormValue("date");
layout := "2006-01-02T15:04"
if PickupDate != "" {
fmt.Println("no date selected")
return
}
dateParsed, err := time.Parse(layout, PickupDate)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var (
departuregeo *geojson.Feature
destinationgeo *geojson.Feature
)
timestamp := timestamppb.New(dateParsed)
if PickupDate != "" && DepartureAddress != "" && DestinationAddress != "" {
// searched = true
var err error
departuregeo, err = geojson.UnmarshalFeature([]byte(DepartureAddress))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
destinationgeo, err = geojson.UnmarshalFeature([]byte(DestinationAddress))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
}
request := solidarity_service.DriverJourneysRequest{
DepartureDate: timestamp,
Departure: &solidarity_service.Feature{
Lat: departuregeo.Geometry.Point[0],
Long: departuregeo.Geometry.Point[1],
Address: DepartureAddress,
},
}
drivers, err := h.services.GRPC.SolidarityService.DriverJourneys(context.TODO(), &request)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
bookingData = solidarity_service.CreateBookingSolidarityRequest {
Booking : &solidarity_service.BookingSolidarityRequest {
PassengerId: r.FormValue("passenger_id"),
DriverId: r.FormValue("driver_id"),
DepartureAddress: &solidarity_service.Feature{
Lat: departuregeo.Geometry.Point[0],
Long: departuregeo.Geometry.Point[1],
Address: DepartureAddress,
},
DestinationAddress: &solidarity_service.Feature{
Lat: destinationgeo.Geometry.Point[0],
Long: destinationgeo.Geometry.Point[1],
Address: DepartureAddress,
},
PickupDate : timestamp,
},
}
h.Renderer.SolidarityServiceListAvailableDrivers(w, r, drivers, &bookingData, accounts, cacheid)
} else {
h.Renderer.SolidarityService(w, r, accounts, cacheid)
}
if r.Method == "POST" {
booking, err := h.services.GRPC.SolidarityService.CreateBookingSolidarity(context.TODO(), &bookingData)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.SolidarityServiceBooking(w, r, booking )
}
}