parcoursmob/handlers/application/solidarity_service.go

143 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/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)
if r.Method == "POST" {
DepartureAddress := r.FormValue("departure");
DestinationAddress := r.FormValue("destination");
PickupDate := r.FormValue("date");
layout := "2006-01-02T15:04"
dateParsed, err := time.Parse(layout, PickupDate)
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)
booking := solidarity_service.BookingSolidarityRequest {
PassengerId: r.FormValue("account_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,
}
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.SolidarityServiceListAvailableDrivers(w, r, drivers, accounts, &booking, cacheid)
} else {
h.Renderer.SolidarityService(w, r, accounts, cacheid)
}
}
// func (h *ApplicationHandler) CreatePendingBooking(w http.ResponseWriter, r *http.Request) {
// if r.Method == "POST" {
// formvalue := solidarity_service.BookingSolidarityRequest{
// PassengerId : "0",
// DriverId : "0",
// // Status : "0",
// DepartureAddress : booking.DepartureAddress,
// DestinationAddress : booking.DestinationAddress,
// PickupDate : booking.PickupDate,
// }
// request := &solidarity_service.BookingSolidarityRequest{
// // Id
// PassengerId : formvalue.PassengerId,
// DriverId : formvalue.DriverId,
// //Status : formvalue.Status,
// DepartureAddress : booking.DepartureAddress,
// DestinationAddress : booking.DestinationAddress,
// PickupDate : formvalue.PickupDate,
// }
// booking, err := h.services.GRPC.SolidarityService.CreateBookingSolidarity(context.TODO(), request)
// if err != nil {
// fmt.Println(err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// h.Renderer.SolidarityServiceBooking(w, r , booking)
// }
// }