73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
|
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{},
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
|
||
|
result.Vehicle, err = VehicleFromStorageType(&booking.Vehicle)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|