2024-04-24 14:05:21 +00:00
|
|
|
package application
|
2024-04-24 08:22:23 +00:00
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2024-08-02 13:02:35 +00:00
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
solidarity_service "git.coopgo.io/sbouaram/solidarity-service/servers/grpc/proto"
|
2024-09-12 08:52:47 +00:00
|
|
|
"github.com/google/uuid"
|
2024-08-28 12:10:03 +00:00
|
|
|
geojson "github.com/paulmach/go.geojson"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
)
|
2024-04-24 08:22:23 +00:00
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
func (h *ApplicationHandler) DriversJourney(w http.ResponseWriter, r *http.Request) {
|
2024-04-24 14:05:21 +00:00
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
accounts, err := h.beneficiaries(r)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-24 08:22:23 +00:00
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
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)
|
2024-06-07 09:51:01 +00:00
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
var (
|
|
|
|
departuregeo *geojson.Feature
|
|
|
|
destinationgeo *geojson.Feature
|
|
|
|
)
|
2024-04-24 08:22:23 +00:00
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
timestamp := timestamppb.New(dateParsed)
|
2024-04-24 08:22:23 +00:00
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
if PickupDate != "" && DepartureAddress != "" && DestinationAddress != "" {
|
2024-08-28 12:10:03 +00:00
|
|
|
// searched = true
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
departuregeo, err = geojson.UnmarshalFeature([]byte(DepartureAddress))
|
2024-08-28 12:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
destinationgeo, err = geojson.UnmarshalFeature([]byte(DestinationAddress))
|
2024-08-28 12:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-08-02 13:02:35 +00:00
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
request := &solidarity_service.DriverJourneysRequest{
|
|
|
|
DepartureDate: timestamp,
|
|
|
|
Departure: &solidarity_service.Feature{
|
|
|
|
Lat: departuregeo.Geometry.Point[0],
|
|
|
|
Long: departuregeo.Geometry.Point[1],
|
2024-09-12 08:52:47 +00:00
|
|
|
Address: DepartureAddress,
|
2024-08-28 12:10:03 +00:00
|
|
|
},
|
|
|
|
}
|
2024-08-02 13:02:35 +00:00
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
drivers, err := h.services.GRPC.SolidarityService.DriverJourneys(context.TODO(), request)
|
|
|
|
|
|
|
|
|
2024-09-12 08:52:47 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-08-28 12:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-17 08:30:29 +00:00
|
|
|
h.Renderer.SolidarityServiceListAvailableDrivers(w, r, drivers, accounts, &booking, cacheid)
|
2024-09-12 08:52:47 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2024-09-17 08:30:29 +00:00
|
|
|
h.Renderer.SolidarityService(w, r, accounts, cacheid)
|
2024-08-28 12:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-09-12 08:52:47 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// }
|