make distance stuff greate again

This commit is contained in:
2023-03-30 00:45:18 +02:00
parent fafa58daf1
commit bf6453b963
12 changed files with 414 additions and 42 deletions

View File

@@ -10,6 +10,7 @@ import (
"time"
"github.com/gorilla/schema"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/bcrypt"
)
@@ -24,8 +25,8 @@ type Handler interface {
//Booking by API
PostBookings(ctx context.Context, booking Booking) (*Booking, error)
PatchBooking(ctx context.Context, bookingId string, status BookingStatus, message *string) error
GetBooking(ctx context.Context, bookingId string) (*Booking, error)
PatchBookings(ctx context.Context, bookingId string, status BookingStatus, message *string) error
GetBookings(ctx context.Context, bookingId string) (*Booking, error)
// Webhooks
PostBookingEvents(ctx context.Context, event CarpoolBookingEvent) error
@@ -93,6 +94,12 @@ type GetPassengerRegularTripsRequest struct {
Count *int64 `schema:"count"`
}
type PatchBookingsRequest struct {
BookingId string `json:"bookingId"`
Status string `json:"status"`
Message string `json:message"`
}
type Server struct {
Handler Handler
AuthorizedOperators []AuthorizedOperator
@@ -131,7 +138,7 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
apiKey := r.Header.Get("X-Api-Key")
operator, err := s.FindApiKey(apiKey)
if err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("unauthorized")
w.WriteHeader(http.StatusUnauthorized)
return
}
@@ -153,6 +160,18 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case r.Method == "GET" && n == 1 && p[0] == "passenger_regular_trips":
s.getPassengerRegularTrips(w, r.WithContext(ctx))
return
case r.Method == "POST" && n == 1 && p[0] == "bookings":
s.postBookings(w, r.WithContext(ctx))
return
case r.Method == "PATCH" && n == 2 && p[0] == "bookings":
s.patchBookings(w, p[1], r.WithContext(ctx))
return
case r.Method == "GET" && n == 2 && p[0] == "bookings":
s.getBookings(w, p[1], r.WithContext(ctx))
return
// case r.Method == "GET" && n == 1 && p[0] == "status":
// s.getStatus(w, r.WithContext(ctx))
// return
default:
w.WriteHeader(http.StatusNotFound)
}
@@ -164,7 +183,7 @@ func (s *Server) getDriverJourneys(w http.ResponseWriter, r *http.Request) {
var decoder = schema.NewDecoder()
if err := decoder.Decode(&request, r.URL.Query()); err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("could not parse the request")
badRequest(w, fmt.Errorf("could not parse the request : %s", err))
return
}
@@ -190,7 +209,7 @@ func (s *Server) getDriverJourneys(w http.ResponseWriter, r *http.Request) {
request.Count)
if err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("GetDriverJourneys failed")
jsonError(w, err, http.StatusInternalServerError)
return
}
@@ -204,7 +223,7 @@ func (s *Server) getPassengerJourneys(w http.ResponseWriter, r *http.Request) {
var decoder = schema.NewDecoder()
if err := decoder.Decode(&request, r.URL.Query()); err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("could not parse the request")
badRequest(w, fmt.Errorf("could not parse the request : %s", err))
return
}
@@ -230,7 +249,7 @@ func (s *Server) getPassengerJourneys(w http.ResponseWriter, r *http.Request) {
request.Count)
if err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("GetPassengerJourneys failed")
jsonError(w, err, http.StatusInternalServerError)
return
}
@@ -244,7 +263,7 @@ func (s *Server) getDriverRegularTrips(w http.ResponseWriter, r *http.Request) {
var decoder = schema.NewDecoder()
if err := decoder.Decode(&request, r.URL.Query()); err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("could not parse the request")
badRequest(w, fmt.Errorf("could not parse the request : %s", err))
return
}
@@ -284,7 +303,7 @@ func (s *Server) getDriverRegularTrips(w http.ResponseWriter, r *http.Request) {
request.Count)
if err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("GetDriverRegularTrips failed")
jsonError(w, err, http.StatusInternalServerError)
return
}
@@ -298,7 +317,7 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request
var decoder = schema.NewDecoder()
if err := decoder.Decode(&request, r.URL.Query()); err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("could not parse the request")
badRequest(w, fmt.Errorf("could not parse the request : %s", err))
return
}
@@ -338,7 +357,7 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request
request.Count)
if err != nil {
fmt.Println(err)
log.Error().Err(err).Msg("GetPassengerRegularTrips failed")
jsonError(w, err, http.StatusInternalServerError)
return
}
@@ -346,11 +365,81 @@ func (s *Server) getPassengerRegularTrips(w http.ResponseWriter, r *http.Request
jsonResponse(w, passengerTrips, http.StatusOK)
}
func (s *Server) postBookings(w http.ResponseWriter, r *http.Request) {
var request Booking
var decoder = json.NewDecoder(r.Body)
if err := decoder.Decode(&request); err != nil {
log.Error().Err(err).Msg("PostBookings - could not parse the request ")
badRequest(w, fmt.Errorf("could not parse the request : %s", err))
return
}
response, err := s.Handler.PostBookings(
r.Context(),
request,
)
if err != nil {
log.Error().Err(err).Msg("error in PostBookings")
jsonError(w, err, http.StatusInternalServerError)
return
}
jsonResponse(w, response, http.StatusOK)
}
func (s *Server) patchBookings(w http.ResponseWriter, bookingId string, r *http.Request) {
var request PatchBookingsRequest
var decoder = json.NewDecoder(r.Body)
if err := decoder.Decode(&request); err != nil {
log.Error().Err(err).Msg("PatchBookings - could not parse the request ")
badRequest(w, fmt.Errorf("could not parse the request : %s", err))
return
}
err := s.Handler.PatchBookings(
r.Context(),
bookingId,
BookingConfirmed,
&request.Message,
)
if err != nil {
log.Error().Err(err).Msg("error in PatchBookings")
jsonError(w, err, http.StatusInternalServerError)
return
}
jsonResponse(w, map[string]any{}, http.StatusOK)
}
func (s *Server) getBookings(w http.ResponseWriter, bookingId string, r *http.Request) {
booking, err := s.Handler.GetBookings(
r.Context(),
bookingId,
)
if err != nil {
log.Error().Err(err).Msg("error in PatchBookings")
jsonError(w, err, http.StatusInternalServerError)
return
}
jsonResponse(w, booking, http.StatusOK)
}
func jsonResponse(w http.ResponseWriter, response any, statuscode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statuscode)
err := json.NewEncoder(w).Encode(response)
fmt.Println(err)
if err != nil {
log.Error().Err(err).Msg("json error encoding issue")
}
}
func jsonError(w http.ResponseWriter, err error, statuscode int) {