parcoursmob/handlers/application/solidarity_service.go

247 lines
6.5 KiB
Go
Raw Normal View History

package application
2024-08-28 12:10:03 +00:00
import (
"context"
"fmt"
"net/http"
2024-12-11 14:26:18 +00:00
"strconv"
2024-08-28 12:10:03 +00:00
"time"
2024-08-02 13:02:35 +00:00
2024-12-11 14:26:18 +00:00
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
2024-08-28 12:10:03 +00:00
solidarity_service "git.coopgo.io/sbouaram/solidarity-service/servers/grpc/proto"
2024-12-11 14:26:18 +00:00
"github.com/google/uuid"
2024-11-28 10:57:36 +00:00
"github.com/gorilla/mux"
2024-08-28 12:10:03 +00:00
geojson "github.com/paulmach/go.geojson"
2024-11-22 16:26:32 +00:00
"google.golang.org/protobuf/types/known/emptypb"
2024-12-11 14:26:18 +00:00
"google.golang.org/protobuf/types/known/structpb"
2024-08-28 12:10:03 +00:00
"google.golang.org/protobuf/types/known/timestamppb"
)
2024-09-25 13:16:02 +00:00
var BookingData Booking
type Booking struct {
bookingData solidarity_service.CreateBookingSolidarityRequest
}
2024-08-28 12:10:03 +00:00
func (h *ApplicationHandler) DriversJourney(w http.ResponseWriter, r *http.Request) {
2024-09-25 13:16:02 +00:00
if r.Method == "GET" && r.FormValue("date") != ""{
DepartureAddress := r.FormValue("departure");
DestinationAddress := r.FormValue("destination");
PickupDate := r.FormValue("date");
layout := "2006-01-02T15:04"
2024-09-25 13:16:02 +00:00
dateParsed, err := time.Parse(layout, PickupDate)
2024-09-19 09:14:02 +00:00
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
2024-08-28 12:10:03 +00:00
var (
departuregeo *geojson.Feature
destinationgeo *geojson.Feature
)
timestamp := timestamppb.New(dateParsed)
if PickupDate != "" && DepartureAddress != "" && DestinationAddress != "" {
2024-08-28 12:10:03 +00:00
// searched = true
var err error
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
}
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-09-19 09:14:02 +00:00
request := solidarity_service.DriverJourneysRequest{
2024-08-28 12:10:03 +00:00
DepartureDate: timestamp,
Departure: &solidarity_service.Feature{
Lat: departuregeo.Geometry.Point[0],
Long: departuregeo.Geometry.Point[1],
Address: DepartureAddress,
2024-08-28 12:10:03 +00:00
},
}
2024-08-02 13:02:35 +00:00
2024-09-19 09:14:02 +00:00
drivers, err := h.services.GRPC.SolidarityService.DriverJourneys(context.TODO(), &request)
2024-08-28 12:10:03 +00:00
2024-09-19 09:14:02 +00:00
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
2024-08-28 12:10:03 +00:00
2024-09-25 13:16:02 +00:00
BookingData.bookingData = solidarity_service.CreateBookingSolidarityRequest {
2024-09-17 12:42:27 +00:00
Booking : &solidarity_service.BookingSolidarityRequest {
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-09-17 12:42:27 +00:00
},
}
2024-11-28 10:57:36 +00:00
h.Renderer.SolidarityServiceListAvailableDrivers(w, r, drivers, &BookingData.bookingData)
2024-09-25 13:16:02 +00:00
} else if r.Method == "POST" {
2024-11-28 10:57:36 +00:00
vars := mux.Vars(r)
beneficiaryID := vars["id"]
2024-09-25 13:16:02 +00:00
driverId := r.FormValue("driver_id")
id := uuid.New().String()
2024-09-25 13:16:02 +00:00
BookingData.bookingData.Booking.DriverId = driverId
2024-11-28 10:57:36 +00:00
BookingData.bookingData.Booking.PassengerId= beneficiaryID
2024-09-25 13:16:02 +00:00
BookingData.bookingData.Booking.Id = id
booking, err := h.services.GRPC.SolidarityService.CreateBookingSolidarity(context.TODO(), &BookingData.bookingData)
2024-09-19 09:14:02 +00:00
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
2024-11-28 10:57:36 +00:00
h.Renderer.SolidarityServiceBooking(w, r, booking)
2024-09-25 13:16:02 +00:00
}else {
2024-11-28 10:57:36 +00:00
h.Renderer.SolidarityService(w, r)
2024-09-19 09:14:02 +00:00
}
2024-11-28 10:57:36 +00:00
}
func (h *ApplicationHandler) SolidarityService (w http.ResponseWriter, r *http.Request) {
accounts, err := h.services.GRPC.SolidarityService.GetAllPassengers(context.TODO(), &emptypb.Empty{})
2024-11-29 10:43:47 +00:00
drivers, err := h.services.GRPC.SolidarityService.GetAllDrivers(context.TODO(), &emptypb.Empty{})
2024-11-28 10:57:36 +00:00
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)
2024-11-29 10:43:47 +00:00
h.Renderer.BeneficiariesSolidarity(w, r, accounts, drivers, parcourmobAccounts, cacheid)
2024-12-04 16:46:25 +00:00
}
func (h *ApplicationHandler) CreateDriver (w http.ResponseWriter, r *http.Request) {
2024-12-11 14:26:18 +00:00
if r.Method== "POST" {
dataMap, err := parseBeneficiariesForm(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
data, err := structpb.NewValue(dataMap)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
userRequest := &mobilityaccounts.RegisterRequest{
Account: &mobilityaccounts.Account{
Namespace: "silvermobi",
Data: data.GetStructValue(),
},
}
resp, err := h.services.GRPC.MobilityAccounts.Register(context.TODO(), userRequest)
fmt.Println("resp : ", resp)
address := resp.Account.Data.GetFields()["address"].GetStructValue();
geometry := address.GetFields()["geometry"].GetStructValue();
coordinates:= geometry.GetFields()["coordinates"].GetListValue();
if len(coordinates.GetValues()) < 2{
fmt.Printf("Erreur lors de la récupération des coordonées : %v", err)
return
}
radiusStr := r.PostFormValue("radius")
radius, err := strconv.Atoi(radiusStr)
if err != nil {
fmt.Printf("Erreur lors de la conversion de radius : %v", err)
radius = 0
}
driverRequest := solidarity_service.DriverRegularAvailabilities {
DriverRequest : &solidarity_service.DriverRequest {
DriverAddress: &solidarity_service.Feature{
Lat: coordinates.GetValues()[0].GetNumberValue(),
Long: coordinates.GetValues()[1].GetNumberValue(),
Address: "********",
},
DriverRadius: int32(radius),
Driver: &solidarity_service.User{
Id: resp.Account.Id,
Alias: r.PostFormValue("alias"),
FirstName: r.PostFormValue("first_name"),
LastName: r.PostFormValue("last_name"),
Grade: 1,
Picture: "h.png",
Gender: r.PostFormValue("gender"),
VerifiedIdentity: true, ///////// a Modifier !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
},
Preferences: &solidarity_service.Preferences{
Smoking: false,
Animals: false,
Music: false,
IsTalker: false,
LuggageSize: 100,
},
Car: &solidarity_service.Car{
Model: r.PostFormValue("model"),
Brand: r.PostFormValue("brand"),
},
},
DriverAvailabilities: []*solidarity_service.RegularAvailabilitySlot{
{
DayOfWeek: 4,
StartTime: "10H00",
EndTime: "11H00",
},
},
}
driverResponse, err := h.services.GRPC.SolidarityService.SetDriverRegularAvailabilities(context.TODO(), &driverRequest)
if err != nil {
fmt.Printf("erreur lors de la création du conducteur: ", err)
radius = 0
}
fmt.Println("driver: ", driverResponse)
}
2024-12-04 16:46:25 +00:00
h.Renderer.CreateDriver(w, r)
2024-12-11 14:26:18 +00:00
}