2022-08-12 12:49:16 +00:00
|
|
|
package grpcapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/fleets/storage"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
structpb "google.golang.org/protobuf/types/known/structpb"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (v Booking) ToStorageType() storage.Booking {
|
|
|
|
booking := storage.Booking{
|
|
|
|
ID: v.Id,
|
|
|
|
Vehicleid: v.Vehicleid,
|
|
|
|
Driver: v.Driver,
|
|
|
|
Startdate: v.Startdate.AsTime(),
|
|
|
|
Enddate: v.Enddate.AsTime(),
|
|
|
|
Unavailablefrom: v.Unavailablefrom.AsTime(),
|
|
|
|
Unavailableto: v.Unavailableto.AsTime(),
|
|
|
|
Data: map[string]any{},
|
2023-05-05 13:16:00 +00:00
|
|
|
Deleted: v.Deleted,
|
2022-08-12 12:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, d := range v.Data.GetFields() {
|
|
|
|
jsondata, err := protojson.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var data any
|
|
|
|
json.Unmarshal(jsondata, &data)
|
|
|
|
booking.Data[k] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Vehicle != nil {
|
|
|
|
booking.Vehicle = v.Vehicle.ToStorageType()
|
|
|
|
}
|
|
|
|
|
|
|
|
return booking
|
|
|
|
}
|
|
|
|
|
|
|
|
func BookingFromStorageType(booking *storage.Booking) (*Booking, error) {
|
|
|
|
|
|
|
|
d, err := sanitizeData(booking.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := structpb.NewStruct(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := &Booking{
|
|
|
|
Id: booking.ID,
|
|
|
|
Vehicleid: booking.Vehicleid,
|
|
|
|
Driver: booking.Driver,
|
|
|
|
Startdate: timestamppb.New(booking.Startdate),
|
|
|
|
Enddate: timestamppb.New(booking.Enddate),
|
|
|
|
Unavailablefrom: timestamppb.New(booking.Unavailablefrom),
|
|
|
|
Unavailableto: timestamppb.New(booking.Unavailableto),
|
|
|
|
Data: data,
|
2023-05-05 13:16:00 +00:00
|
|
|
Deleted: booking.Deleted,
|
2022-08-12 12:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.Vehicle, err = VehicleFromStorageType(&booking.Vehicle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|