parcoursmob/handlers/application/solidarity_service.go

139 lines
3.5 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/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
)
var BookingData Booking
type Booking struct {
bookingData solidarity_service.CreateBookingSolidarityRequest
}
func (h *ApplicationHandler) DriversJourney(w http.ResponseWriter, r *http.Request) {
accounts, err := h.services.GRPC.SolidarityService.GetAllPassengers(context.TODO(), &emptypb.Empty{})
parcourmobAccounts, err := h.beneficiaries(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
}
cacheid := uuid.NewString()
h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour)
if r.Method == "GET" && r.FormValue("date") != ""{
DepartureAddress := r.FormValue("departure");
DestinationAddress := r.FormValue("destination");
PickupDate := r.FormValue("date");
PassengerId := r.FormValue("passenger_id")
layout := "2006-01-02T15:04"
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.bookingData = solidarity_service.CreateBookingSolidarityRequest {
Booking : &solidarity_service.BookingSolidarityRequest {
PassengerId: PassengerId,
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.bookingData, accounts, parcourmobAccounts, cacheid)
} else if r.Method == "POST" {
driverId := r.FormValue("driver_id")
id := uuid.New().String()
BookingData.bookingData.Booking.DriverId = driverId
BookingData.bookingData.Booking.Id = id
booking, err := h.services.GRPC.SolidarityService.CreateBookingSolidarity(context.TODO(), &BookingData.bookingData)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.SolidarityServiceBooking(w, r, booking, accounts, parcourmobAccounts)
}else {
h.Renderer.SolidarityService(w, r, accounts, parcourmobAccounts, cacheid)
}
}