refactoring

This commit is contained in:
2023-12-08 07:31:41 +01:00
parent d12c55d740
commit 150c913feb
19 changed files with 1175 additions and 814 deletions

View File

@@ -11,7 +11,6 @@ package openapi
import (
"context"
"fmt"
"github.com/spf13/viper"
"net/http"
"solidarity-service/handler"
@@ -42,7 +41,6 @@ func NewBookingsFilterAPIService(config *viper.Viper, handler *handler.Solidarit
func (s *BookingsFilterAPIService) FilterBookingsByStatus(ctx context.Context, operator string, status string, filterType string, id string) (ImplResponse, error) {
bookings, err := s.storage.FilterUserBookingsByStatus(filterType, internal.BookingStatus(status), id)
if err != nil {
fmt.Println(err)
if strings.Contains(err.Error(), " no rows in result set") {
return Response(http.StatusBadRequest, "no related data recheck your parameters"), nil
} else {
@@ -51,55 +49,48 @@ func (s *BookingsFilterAPIService) FilterBookingsByStatus(ctx context.Context, o
}
responses := []Booking{}
for _, v := range bookings {
passenger, err := s.storage.GetPassenger(v.Passenger.ID)
if err != nil {
return Response(http.StatusInternalServerError, nil), nil
}
driver, err := s.storage.GetDriver(v.Driver.ID)
if err != nil {
return Response(http.StatusInternalServerError, nil), nil
}
duration, err := s.handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
if err != nil {
duration = 0
return Response(http.StatusInternalServerError, err.Error()), err
}
car := driver.Car
responses = append(responses, Booking{
Id: v.ID,
Status: BookingStatus(v.Status),
Driver: User{
Id: v.Driver.ID,
Operator: driver.Driver.Operator,
Alias: driver.Driver.Alias,
FirstName: driver.Driver.FirstName,
LastName: driver.Driver.LastName,
Grade: int32(driver.Driver.Grade),
Picture: driver.Driver.Picture,
Gender: driver.Driver.Gender,
VerifiedIdentity: driver.Driver.VerifiedIdentity,
Operator: v.Driver.Operator,
Alias: v.Driver.Alias,
FirstName: v.Driver.FirstName,
LastName: v.Driver.LastName,
Grade: int32(v.Driver.Grade),
Picture: v.Driver.Picture,
Gender: v.Driver.Gender,
VerifiedIdentity: v.Driver.VerifiedIdentity,
},
Passenger: User{
Id: v.Passenger.ID,
Operator: passenger.Passenger.Operator,
Alias: passenger.Passenger.Alias,
FirstName: passenger.Passenger.FirstName,
LastName: passenger.Passenger.LastName,
Grade: int32(passenger.Passenger.Grade),
Picture: passenger.Passenger.Picture,
Gender: passenger.Passenger.Gender,
VerifiedIdentity: passenger.Passenger.VerifiedIdentity,
Operator: v.Passenger.Operator,
Alias: v.Passenger.Alias,
FirstName: v.Passenger.FirstName,
LastName: v.Passenger.LastName,
Grade: int32(v.Passenger.Grade),
Picture: v.Passenger.Picture,
Gender: v.Passenger.Gender,
VerifiedIdentity: v.Passenger.VerifiedIdentity,
},
PassengerPickupDate: int32(passenger.Passenger_pickup_date),
PassengerPickupLat: passenger.Passenger_departure_address.Point().X(),
PassengerPickupLng: passenger.Passenger_departure_address.Point().Y(),
PassengerPickupAddress: passenger.Passenger_departure_address.Properties.MustString("name"),
PassengerDropLat: passenger.Passenger_destination_address.Point().X(),
PassengerDropLng: passenger.Passenger_destination_address.Point().Y(),
PassengerDropAddress: passenger.Passenger_destination_address.Properties.MustString("name"),
Duration: int32(duration),
Distance: int32(s.handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)),
PassengerPickupDate: int32(v.Pickup_date),
PassengerPickupLat: v.PassengerPickupAddress.Point().X(),
PassengerPickupLng: v.PassengerPickupAddress.Point().Y(),
PassengerPickupAddress: v.PassengerPickupAddress.Properties.MustString("name"),
PassengerDropLat: v.PassengerDropAddress.Point().X(),
PassengerDropLng: v.PassengerDropAddress.Point().Y(),
PassengerDropAddress: v.PassengerDropAddress.Properties.MustString("name"),
Duration: int32(v.Duration),
Distance: int32(v.Distance),
Car: Car{
Model: driver.Car.Model,
Brand: driver.Car.Brand,
Model: car.Model,
Brand: car.Brand,
},
Price: Price{
Type: "FREE",

View File

@@ -19,8 +19,6 @@ import (
"solidarity-service/handler"
"solidarity-service/internal"
"solidarity-service/storage"
"solidarity-service/utils"
"strings"
)
// DriverAvailabilityAndScheduleAPIService is a service that implements the logic for the DriverAvailabilityAndScheduleAPIServicer
@@ -79,9 +77,6 @@ func (s *DriverAvailabilityAndScheduleAPIService) DriverPunctualAvailabilitiesPo
driver.Car.Model = driverPunctualAvailabilitiesRequest.Car.Model
err := s.storage.CreateDriver(driver)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
} else {
return Response(201, "Punctual driver availabilities set successfully"), nil
@@ -125,9 +120,6 @@ func (s *DriverAvailabilityAndScheduleAPIService) DriverRegularAvailabilitiesPos
driver.Car.Model = driverRegularAvailabilitiesRequest.Car.Model
err := s.storage.CreateDriver(driver)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
} else {
return Response(201, "Regular driver availabilities set successfully"), nil

View File

@@ -13,6 +13,8 @@ import (
"context"
"errors"
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/spf13/viper"
"net/http"
"solidarity-service/handler"
@@ -50,63 +52,45 @@ func (s *InteractAPIService) GetBookings(ctx context.Context, operator string, b
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
}
}
passenger, err := s.storage.GetPassenger(booking.Passenger.ID)
if err != nil {
return Response(http.StatusInternalServerError, nil), nil
}
driver, err := s.storage.GetDriver(booking.Driver.ID)
if err != nil {
return Response(http.StatusInternalServerError, nil), nil
}
duration, err := s.handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
if err != nil {
duration = 0
}
response := Booking{
Id: booking.ID,
Driver: User{
Id: driver.Driver.ID,
Operator: driver.Driver.Operator,
Alias: driver.Driver.Alias,
FirstName: driver.Driver.FirstName,
LastName: driver.Driver.LastName,
Grade: int32(driver.Driver.Grade),
Picture: driver.Driver.Picture,
Gender: driver.Driver.Gender,
VerifiedIdentity: driver.Driver.VerifiedIdentity,
Id: booking.Driver.ID,
Operator: booking.Driver.Operator,
Alias: booking.Driver.Alias,
FirstName: booking.Driver.FirstName,
LastName: booking.Driver.LastName,
Grade: int32(booking.Driver.Grade),
Picture: booking.Driver.Picture,
Gender: booking.Driver.Gender,
VerifiedIdentity: booking.Driver.VerifiedIdentity,
},
Passenger: User{
Id: passenger.Passenger.ID,
Operator: passenger.Passenger.Operator,
Alias: passenger.Passenger.Alias,
FirstName: passenger.Passenger.FirstName,
LastName: passenger.Passenger.LastName,
Grade: int32(passenger.Passenger.Grade),
Picture: passenger.Passenger.Picture,
Gender: passenger.Passenger.Gender,
VerifiedIdentity: passenger.Passenger.VerifiedIdentity,
Id: booking.Passenger.ID,
Operator: booking.Passenger.Operator,
Alias: booking.Passenger.Alias,
FirstName: booking.Passenger.FirstName,
LastName: booking.Passenger.LastName,
Grade: int32(booking.Passenger.Grade),
Picture: booking.Passenger.Picture,
Gender: booking.Passenger.Gender,
VerifiedIdentity: booking.Passenger.VerifiedIdentity,
},
PassengerPickupDate: int32(passenger.Passenger_pickup_date),
PassengerPickupLat: passenger.Passenger_departure_address.Point().X(),
PassengerPickupLng: passenger.Passenger_departure_address.Point().Y(),
PassengerPickupAddress: passenger.Passenger_departure_address.Properties.MustString("name"),
PassengerDropLat: passenger.Passenger_destination_address.Point().X(),
PassengerDropLng: passenger.Passenger_destination_address.Point().Y(),
PassengerDropAddress: passenger.Passenger_destination_address.Properties.MustString("name"),
PassengerPickupDate: int32(booking.Pickup_date),
PassengerPickupLat: booking.PassengerPickupAddress.Point().X(),
PassengerPickupLng: booking.PassengerPickupAddress.Point().Y(),
PassengerPickupAddress: booking.PassengerPickupAddress.Properties.MustString("name"),
PassengerDropLat: booking.PassengerDropAddress.Point().X(),
PassengerDropLng: booking.PassengerDropAddress.Point().Y(),
PassengerDropAddress: booking.PassengerDropAddress.Properties.MustString("name"),
Status: BookingStatus(booking.Status),
Duration: int32(duration),
Distance: int32(s.handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)),
Car: Car{
Model: driver.Car.Model,
Brand: driver.Car.Brand,
},
Duration: int32(booking.Duration),
Distance: int32(booking.Distance),
Price: Price{
Type: "FREE",
},
}
fmt.Println(response)
return Response(200, response), nil
}
// PatchBookings - Updates status of an existing Booking request.
@@ -130,6 +114,21 @@ func (s *InteractAPIService) PostBookings(ctx context.Context, bookingRequest Bo
booking.Driver_id = bookingRequest.DriverId
booking.Passenger_id = bookingRequest.PassengerId
booking.Operator = bookingRequest.Operator
booking.Departure_address = &geojson.Feature{
Type: "Feature",
Geometry: orb.Geometry(orb.Point{bookingRequest.DepartureAddressLat, bookingRequest.DepartureAddressLong}),
Properties: geojson.Properties{
"name": bookingRequest.DepartureAddress,
},
}
booking.Destination_address = &geojson.Feature{
Type: "Feature",
Geometry: orb.Geometry(orb.Point{bookingRequest.DestinationAddressLat, bookingRequest.DestinationAddressLong}),
Properties: geojson.Properties{
"name": bookingRequest.DestinationAddress,
},
}
booking.Pickup_date = int64(bookingRequest.Pickup_date)
err := s.storage.CreateBooking(booking)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
@@ -145,7 +144,7 @@ func (s *InteractAPIService) PostBookings(ctx context.Context, bookingRequest Bo
if err != nil {
return Response(http.StatusInternalServerError, nil), errors.New("PostBookings internal server error")
}
duration, err := s.handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)
duration, err := s.handler.CalculateDurationBetweenFeatures(booking.Departure_address, booking.Destination_address)
if err != nil {
duration = 0
}
@@ -173,16 +172,16 @@ func (s *InteractAPIService) PostBookings(ctx context.Context, bookingRequest Bo
Gender: passenger.Passenger.Gender,
VerifiedIdentity: passenger.Passenger.VerifiedIdentity,
},
PassengerPickupDate: int32(passenger.Passenger_pickup_date),
PassengerPickupLat: passenger.Passenger_departure_address.Point().X(),
PassengerPickupLng: passenger.Passenger_departure_address.Point().Y(),
PassengerPickupAddress: passenger.Passenger_departure_address.Properties.MustString("name"),
PassengerDropLat: passenger.Passenger_destination_address.Point().X(),
PassengerDropLng: passenger.Passenger_destination_address.Point().Y(),
PassengerDropAddress: passenger.Passenger_destination_address.Properties.MustString("name"),
PassengerPickupDate: int32(booking.Pickup_date),
PassengerPickupLat: booking.Departure_address.Point().X(),
PassengerPickupLng: booking.Departure_address.Point().Y(),
PassengerPickupAddress: booking.Departure_address.Properties.MustString("name"),
PassengerDropLat: booking.Destination_address.Point().X(),
PassengerDropLng: booking.Destination_address.Point().Y(),
PassengerDropAddress: booking.Destination_address.Properties.MustString("name"),
Status: bookingRequest.Status,
Duration: int32(duration),
Distance: int32(s.handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address)),
Distance: int32(s.handler.CalculateDistanceBetweenFeatures(booking.Departure_address, booking.Destination_address)),
Car: Car{
Model: driver.Car.Model,
Brand: driver.Car.Brand,

View File

@@ -12,15 +12,11 @@ package openapi
import (
"context"
"errors"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/spf13/viper"
"net/http"
"solidarity-service/handler"
"solidarity-service/internal"
"solidarity-service/storage"
"solidarity-service/utils"
"strings"
)
// PassengerTripRequestAPIService is a service that implements the logic for the PassengerTripRequestAPIServicer
@@ -44,21 +40,6 @@ func NewPassengerTripRequestAPIService(config *viper.Viper, handler *handler.Sol
// PassengerPost - Create a Passenger Trip Request
func (s *PassengerTripRequestAPIService) PassengerPost(ctx context.Context, passengerTripRequest PassengerTripRequest) (ImplResponse, error) {
passenger := internal.Passenger{}
passenger.Passenger_departure_address = &geojson.Feature{
Type: "Feature",
Geometry: orb.Geometry(orb.Point{passengerTripRequest.DepartureLatitude, passengerTripRequest.DepartureLongitude}),
Properties: geojson.Properties{
"name": passengerTripRequest.DepartureAddress,
},
}
passenger.Passenger_destination_address = &geojson.Feature{
Type: "Feature",
Geometry: orb.Geometry(orb.Point{passengerTripRequest.DestinationLatitude, passengerTripRequest.DestinationLongitude}),
Properties: geojson.Properties{
"name": passengerTripRequest.DestinationAddress,
},
}
passenger.Passenger_pickup_date = int64(passengerTripRequest.DepartureDate)
passenger.Passenger.ID = passengerTripRequest.User.Id
passenger.Passenger.Operator = passengerTripRequest.User.Operator
passenger.Passenger.Alias = passengerTripRequest.User.Alias
@@ -75,9 +56,6 @@ func (s *PassengerTripRequestAPIService) PassengerPost(ctx context.Context, pass
passenger.Preferences.Music = passengerTripRequest.Preferences.Music
err := s.storage.CreatePassenger(passenger)
if err != nil {
if strings.Contains(err.Error(), utils.SQL_DUPLICATE) {
return Response(http.StatusBadRequest, "ID already in use"), nil
}
return Response(http.StatusInternalServerError, nil), errors.New("DriverPunctualAvailabilitiesPost internal server error")
} else {
return Response(201, "Trip request created"), nil

View File

@@ -21,16 +21,31 @@ type BookingRequest struct {
Status BookingStatus `json:"status"`
Operator string `json:"operator"`
DepartureAddressLat float64 `json:"departure_address_lat"`
DepartureAddressLong float64 `json:"departure_address_long"`
DestinationAddressLat float64 `json:"destination_address_lat"`
DestinationAddressLong float64 `json:"destination_address_long"`
DepartureAddress string `json:"departure_address"`
DestinationAddress string `json:"destination_address"`
Pickup_date int32 `json:"pickup_date"`
}
// AssertBookingRequestRequired checks if the required fields are not zero-ed
func AssertBookingRequestRequired(obj BookingRequest) error {
elements := map[string]interface{}{
"id": obj.Id,
"passengerId": obj.PassengerId,
"driverId": obj.DriverId,
"status": obj.Status,
"operator": obj.Operator,
"id": obj.Id,
"passengerId": obj.PassengerId,
"driverId": obj.DriverId,
"status": obj.Status,
"operator": obj.Operator,
"departure_address_lat": obj.DepartureAddressLat,
"departure_address_long": obj.DepartureAddressLong,
"destination_address_lat": obj.DestinationAddressLat,
"destination_address_long": obj.DestinationAddressLong,
"pickup_date": obj.Pickup_date,
"departure_address": obj.DepartureAddress,
"destination_address": obj.DestinationAddress,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {

View File

@@ -10,37 +10,14 @@
package openapi
type PassengerTripRequest struct {
User User `json:"user"`
DestinationAddress string `json:"destination_address"`
DestinationLatitude float64 `json:"destination_latitude"`
DestinationLongitude float64 `json:"destination_longitude"`
DepartureAddress string `json:"departure_address"`
DepartureLatitude float64 `json:"departure_latitude"`
DepartureLongitude float64 `json:"departure_longitude"`
// Departure datetime using a UNIX UTC timestamp in seconds.
DepartureDate int32 `json:"departure_date"`
User User `json:"user"`
Preferences Preferences `json:"preferences,omitempty"`
}
// AssertPassengerTripRequestRequired checks if the required fields are not zero-ed
func AssertPassengerTripRequestRequired(obj PassengerTripRequest) error {
elements := map[string]interface{}{
"user": obj.User,
"destination_address": obj.DestinationAddress,
"destination_latitude": obj.DestinationLatitude,
"destination_longitude": obj.DestinationLongitude,
"departure_address": obj.DepartureAddress,
"departure_latitude": obj.DepartureLatitude,
"departure_longitude": obj.DepartureLongitude,
"departure_date": obj.DepartureDate,
"user": obj.User,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {