From c03fcb31d6762d6cc1822fc8928ef74e28895954 Mon Sep 17 00:00:00 2001 From: Maxime Date: Thu, 12 Sep 2024 10:52:47 +0200 Subject: [PATCH] list beneficiaries + select beneficiary --- handlers/application/solidarity_service.go | 112 +++++++++++++++++---- main.go | 2 + renderer/beneficiaries.go | 2 +- renderer/solidarity_service.go | 52 +++++++++- 4 files changed, 141 insertions(+), 27 deletions(-) diff --git a/handlers/application/solidarity_service.go b/handlers/application/solidarity_service.go index c7024b0..4168d55 100644 --- a/handlers/application/solidarity_service.go +++ b/handlers/application/solidarity_service.go @@ -7,42 +7,53 @@ import ( "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) { - if r.Method == "POST" { + accounts, err := h.beneficiaries(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } - // firstname := r.FormValue("first_name") - // lastname := r.FormValue("last_name") - departure := r.FormValue("departure") - destination := r.FormValue("destination") - //date := r.FormValue("date") + 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 ) - departureTime := time.Now() + timestamp := timestamppb.New(dateParsed) - timestamp := timestamppb.New(departureTime) - - if departure != "" && destination != "" { + if PickupDate != "" && DepartureAddress != "" && DestinationAddress != "" { // searched = true var err error - departuregeo, err = geojson.UnmarshalFeature([]byte(departure)) + departuregeo, err = geojson.UnmarshalFeature([]byte(DepartureAddress)) if err != nil { fmt.Println(err) w.WriteHeader(http.StatusBadRequest) return } - destinationgeo, err = geojson.UnmarshalFeature([]byte(destination)) + destinationgeo, err = geojson.UnmarshalFeature([]byte(DestinationAddress)) if err != nil { fmt.Println(err) w.WriteHeader(http.StatusBadRequest) @@ -52,42 +63,61 @@ func (h *ApplicationHandler) DriversJourney(w http.ResponseWriter, r *http.Reque fmt.Println(departuregeo, destinationgeo, "departuregeo") } - request := &solidarity_service.DriverJourneysRequest{ DepartureDate: timestamp, Departure: &solidarity_service.Feature{ Lat: departuregeo.Geometry.Point[0], Long: departuregeo.Geometry.Point[1], - Address: departure, + 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 } - fmt.Println("drivers: ", drivers) - h.Renderer.SolidarityServiceBookingList(w, r, drivers) + fmt.Println("request: ", request) + h.Renderer.SolidarityServiceBookingListDrivers(w, r, drivers, accounts, &booking, cacheid) - } else { - departureTime := time.Now() + } else { - timestamp := timestamppb.New(departureTime) + DepartureAddress := "0 rue A"; + PickupDate := r.FormValue("date"); + + layout := "2006-01-02T15:04" + dateParsed, err := time.Parse(layout, PickupDate) + + timestamp := timestamppb.New(dateParsed) request := &solidarity_service.DriverJourneysRequest{ DepartureDate: timestamp, Departure: &solidarity_service.Feature{ Lat: 0, Long: 0, - Address: "Null Island (default)", + Address: DepartureAddress, }, } + drivers, err := h.services.GRPC.SolidarityService.DriverJourneys(context.TODO(), request) if err != nil { @@ -95,8 +125,46 @@ func (h *ApplicationHandler) DriversJourney(w http.ResponseWriter, r *http.Reque w.WriteHeader(http.StatusInternalServerError) return } - h.Renderer.SolidarityServiceBookingList(w, r, drivers) + h.Renderer.SolidarityService(w, r, drivers, 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) + +// } + + +// } diff --git a/main.go b/main.go index 2c8970d..2cc4597 100755 --- a/main.go +++ b/main.go @@ -139,6 +139,8 @@ func main() { //=================================silvermobi==================================== application.HandleFunc("/solidarity_service/", applicationHandler.DriversJourney) + //application.HandleFunc("/solidarity_service/", applicationHandler.CreatePendingBooking) + appGroup := application.PathPrefix("/group_module").Subrouter() appGroup.HandleFunc("/", applicationHandler.Groups) diff --git a/renderer/beneficiaries.go b/renderer/beneficiaries.go index 163c3a8..fa7f1a1 100755 --- a/renderer/beneficiaries.go +++ b/renderer/beneficiaries.go @@ -34,7 +34,7 @@ func (renderer *Renderer) BeneficiariesList(w http.ResponseWriter, r *http.Reque state := NewState(r, renderer.ThemeConfig, beneficiariesMenu) state.ViewState = BeneficiariesListState{ - Count: len(accounts), + Count: len(accounts), CacheId: cacheid, Beneficiaries: accounts, } diff --git a/renderer/solidarity_service.go b/renderer/solidarity_service.go index 465129a..09c04fe 100644 --- a/renderer/solidarity_service.go +++ b/renderer/solidarity_service.go @@ -1,25 +1,69 @@ package renderer import ( + "encoding/json" + "html/template" "net/http" + + mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage" solidarity_service "git.coopgo.io/sbouaram/solidarity-service/servers/grpc/proto" ) const solidarityserviceMenu = "solidarity_service" -func (renderer *Renderer) SolidarityServiceBookingList(w http.ResponseWriter, r *http.Request, drivers *solidarity_service.DriverJourneysResponse) { + +type SolidarityListState struct { + Count int `json:"count"` + CacheId string `json:"cache_id"` + Beneficiaries []mobilityaccountsstorage.Account `json:"beneficiaries"` + Drivers *solidarity_service.DriverJourneysResponse `json:"drivers"` + +} + +func (s SolidarityListState) JSON() template.JS { + result, _ := json.Marshal(s) + return template.JS(result) +} + +func (s SolidarityListState) JSONWithLimits(a int, b int) template.JS { + if b < len(s.Beneficiaries) { + s.Beneficiaries = s.Beneficiaries[a:b] + } + return s.JSON() +} + +func (renderer *Renderer) SolidarityService(w http.ResponseWriter, r *http.Request, drivers *solidarity_service.DriverJourneysResponse, accounts []mobilityaccountsstorage.Account, cacheid string) { files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.list.files") state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu) - state.ViewState = map[string]any{ - "drivers": drivers, + state.ViewState = SolidarityListState { + Count: len(accounts), + Beneficiaries: accounts, + CacheId: cacheid, + Drivers: drivers, } renderer.Render("solidarity_service", w, r, files, state) } -func (renderer *Renderer) SolidarityServiceBookingCreate(w http.ResponseWriter, r *http.Request) { +func (renderer *Renderer) SolidarityServiceBooking(w http.ResponseWriter, r *http.Request, booking *solidarity_service.BookingSolidarityRequest) { files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.list.files") state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu) + state.ViewState = map[string]any{ + "booking": booking, + } + + renderer.Render("solidarity_service", w, r, files, state) +} + +func (renderer *Renderer) SolidarityServiceBookingListDrivers(w http.ResponseWriter, r *http.Request, drivers *solidarity_service.DriverJourneysResponse, accounts []mobilityaccountsstorage.Account, booking *solidarity_service.BookingSolidarityRequest, cacheid string) { + files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.list.files") + state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu) + state.ViewState = SolidarityListState { + Count: len(accounts), + Beneficiaries: accounts, + CacheId: cacheid, + Drivers: drivers, + } renderer.Render("solidarity_service", w, r, files, state) }