1 Commits

Author SHA1 Message Date
896c1f2f1b edit proto files to add Deleted option 2023-05-05 15:16:00 +02:00
14 changed files with 1001 additions and 2476 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,3 @@
config.yaml
.vscode
.idea
__debug_bin

2
go.mod
View File

@@ -3,6 +3,7 @@ module git.coopgo.io/coopgo-platform/fleets
go 1.18
require (
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.3.0
github.com/spf13/viper v1.12.0
go.mongodb.org/mongo-driver v1.10.1
@@ -12,7 +13,6 @@ require (
require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect

View File

@@ -20,6 +20,7 @@ func (v Booking) ToStorageType() storage.Booking {
Unavailablefrom: v.Unavailablefrom.AsTime(),
Unavailableto: v.Unavailableto.AsTime(),
Data: map[string]any{},
Deleted: v.Deleted,
}
for k, d := range v.Data.GetFields() {
@@ -62,6 +63,7 @@ func BookingFromStorageType(booking *storage.Booking) (*Booking, error) {
Unavailablefrom: timestamppb.New(booking.Unavailablefrom),
Unavailableto: timestamppb.New(booking.Unavailableto),
Data: data,
Deleted: booking.Deleted,
}
result.Vehicle, err = VehicleFromStorageType(&booking.Vehicle)

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc v3.19.4
// - protoc v3.21.12
// source: fleets.proto
package grpcapi
@@ -27,14 +27,14 @@ type FleetsClient interface {
GetVehicle(ctx context.Context, in *GetVehicleRequest, opts ...grpc.CallOption) (*GetVehicleResponse, error)
GetVehicles(ctx context.Context, in *GetVehiclesRequest, opts ...grpc.CallOption) (*GetVehiclesResponse, error)
UpdateVehicle(ctx context.Context, in *UpdateVehicleRequest, opts ...grpc.CallOption) (*UpdateVehicleResponse, error)
//Bookings
// Bookings
CreateBooking(ctx context.Context, in *CreateBookingRequest, opts ...grpc.CallOption) (*CreateBookingResponse, error)
GetBooking(ctx context.Context, in *GetBookingRequest, opts ...grpc.CallOption) (*GetBookingResponse, error)
GetBookings(ctx context.Context, in *GetBookingsRequest, opts ...grpc.CallOption) (*GetBookingsResponse, error)
GetDriverBookings(ctx context.Context, in *GetDriverBookingsRequest, opts ...grpc.CallOption) (*GetDriverBookingsResponse, error)
UpdateBooking(ctx context.Context, in *UpdateBookingRequest, opts ...grpc.CallOption) (*UpdateBookingResponse, error)
DeleteBooking(ctx context.Context, in *DeleteBookingRequest, opts ...grpc.CallOption) (*DeleteBookingResponse, error)
//Search / Availabilities
// Search / Availabilities
FindVehicle(ctx context.Context, in *FindVehicleRequest, opts ...grpc.CallOption) (*FindVehicleResponse, error)
}
@@ -154,14 +154,14 @@ type FleetsServer interface {
GetVehicle(context.Context, *GetVehicleRequest) (*GetVehicleResponse, error)
GetVehicles(context.Context, *GetVehiclesRequest) (*GetVehiclesResponse, error)
UpdateVehicle(context.Context, *UpdateVehicleRequest) (*UpdateVehicleResponse, error)
//Bookings
// Bookings
CreateBooking(context.Context, *CreateBookingRequest) (*CreateBookingResponse, error)
GetBooking(context.Context, *GetBookingRequest) (*GetBookingResponse, error)
GetBookings(context.Context, *GetBookingsRequest) (*GetBookingsResponse, error)
GetDriverBookings(context.Context, *GetDriverBookingsRequest) (*GetDriverBookingsResponse, error)
UpdateBooking(context.Context, *UpdateBookingRequest) (*UpdateBookingResponse, error)
DeleteBooking(context.Context, *DeleteBookingRequest) (*DeleteBookingResponse, error)
//Search / Availabilities
// Search / Availabilities
FindVehicle(context.Context, *FindVehicleRequest) (*FindVehicleResponse, error)
mustEmbedUnimplementedFleetsServer()
}

View File

@@ -55,8 +55,7 @@ func (s FleetsServerImpl) GetVehicle(ctx context.Context, req *GetVehicleRequest
func (s FleetsServerImpl) GetVehicles(ctx context.Context, req *GetVehiclesRequest) (*GetVehiclesResponse, error) {
filter := storage.VehicleFilters{
Types: req.Types,
Administrators: req.Administrators,
Types: req.Types,
}
if req.AvailabilityFrom.IsValid() {
filter.AvailableFrom = req.AvailabilityFrom.AsTime()

View File

@@ -1,359 +1,250 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v3.19.4
// source: vehicles.proto
package grpcapi
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
fmt "fmt"
proto "github.com/golang/protobuf/proto"
structpb "google.golang.org/protobuf/types/known/structpb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
reflect "reflect"
sync "sync"
math "math"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type Vehicle struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Namespace string `protobuf:"bytes,2,opt,name=namespace,proto3" json:"namespace,omitempty"`
Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
Administrators []string `protobuf:"bytes,4,rep,name=administrators,proto3" json:"administrators,omitempty"`
Data *structpb.Struct `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
Bookings []*Booking `protobuf:"bytes,6,rep,name=bookings,proto3" json:"bookings,omitempty"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Namespace string `protobuf:"bytes,2,opt,name=namespace,proto3" json:"namespace,omitempty"`
Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
Administrators []string `protobuf:"bytes,4,rep,name=administrators,proto3" json:"administrators,omitempty"`
Data *structpb.Struct `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
Bookings []*Booking `protobuf:"bytes,6,rep,name=bookings,proto3" json:"bookings,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (x *Vehicle) Reset() {
*x = Vehicle{}
if protoimpl.UnsafeEnabled {
mi := &file_vehicles_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Vehicle) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Vehicle) ProtoMessage() {}
func (x *Vehicle) ProtoReflect() protoreflect.Message {
mi := &file_vehicles_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Vehicle.ProtoReflect.Descriptor instead.
func (m *Vehicle) Reset() { *m = Vehicle{} }
func (m *Vehicle) String() string { return proto.CompactTextString(m) }
func (*Vehicle) ProtoMessage() {}
func (*Vehicle) Descriptor() ([]byte, []int) {
return file_vehicles_proto_rawDescGZIP(), []int{0}
return fileDescriptor_b84b5fa4f4d86273, []int{0}
}
func (x *Vehicle) GetId() string {
if x != nil {
return x.Id
func (m *Vehicle) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Vehicle.Unmarshal(m, b)
}
func (m *Vehicle) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Vehicle.Marshal(b, m, deterministic)
}
func (m *Vehicle) XXX_Merge(src proto.Message) {
xxx_messageInfo_Vehicle.Merge(m, src)
}
func (m *Vehicle) XXX_Size() int {
return xxx_messageInfo_Vehicle.Size(m)
}
func (m *Vehicle) XXX_DiscardUnknown() {
xxx_messageInfo_Vehicle.DiscardUnknown(m)
}
var xxx_messageInfo_Vehicle proto.InternalMessageInfo
func (m *Vehicle) GetId() string {
if m != nil {
return m.Id
}
return ""
}
func (x *Vehicle) GetNamespace() string {
if x != nil {
return x.Namespace
func (m *Vehicle) GetNamespace() string {
if m != nil {
return m.Namespace
}
return ""
}
func (x *Vehicle) GetType() string {
if x != nil {
return x.Type
func (m *Vehicle) GetType() string {
if m != nil {
return m.Type
}
return ""
}
func (x *Vehicle) GetAdministrators() []string {
if x != nil {
return x.Administrators
func (m *Vehicle) GetAdministrators() []string {
if m != nil {
return m.Administrators
}
return nil
}
func (x *Vehicle) GetData() *structpb.Struct {
if x != nil {
return x.Data
func (m *Vehicle) GetData() *structpb.Struct {
if m != nil {
return m.Data
}
return nil
}
func (x *Vehicle) GetBookings() []*Booking {
if x != nil {
return x.Bookings
func (m *Vehicle) GetBookings() []*Booking {
if m != nil {
return m.Bookings
}
return nil
}
type Booking struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Vehicleid string `protobuf:"bytes,2,opt,name=vehicleid,proto3" json:"vehicleid,omitempty"`
Driver string `protobuf:"bytes,3,opt,name=driver,proto3" json:"driver,omitempty"`
Startdate *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=startdate,proto3" json:"startdate,omitempty"`
Enddate *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=enddate,proto3" json:"enddate,omitempty"`
Unavailablefrom *timestamppb.Timestamp `protobuf:"bytes,6,opt,name=unavailablefrom,proto3" json:"unavailablefrom,omitempty"`
Unavailableto *timestamppb.Timestamp `protobuf:"bytes,7,opt,name=unavailableto,proto3" json:"unavailableto,omitempty"`
Data *structpb.Struct `protobuf:"bytes,8,opt,name=data,proto3" json:"data,omitempty"`
Vehicle *Vehicle `protobuf:"bytes,9,opt,name=vehicle,proto3" json:"vehicle,omitempty"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Vehicleid string `protobuf:"bytes,2,opt,name=vehicleid,proto3" json:"vehicleid,omitempty"`
Driver string `protobuf:"bytes,3,opt,name=driver,proto3" json:"driver,omitempty"`
Startdate *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=startdate,proto3" json:"startdate,omitempty"`
Enddate *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=enddate,proto3" json:"enddate,omitempty"`
Unavailablefrom *timestamppb.Timestamp `protobuf:"bytes,6,opt,name=unavailablefrom,proto3" json:"unavailablefrom,omitempty"`
Unavailableto *timestamppb.Timestamp `protobuf:"bytes,7,opt,name=unavailableto,proto3" json:"unavailableto,omitempty"`
Data *structpb.Struct `protobuf:"bytes,8,opt,name=data,proto3" json:"data,omitempty"`
Deleted bool `protobuf:"varint,10,opt,name=deleted,proto3" json:"deleted,omitempty"`
Vehicle *Vehicle `protobuf:"bytes,9,opt,name=vehicle,proto3" json:"vehicle,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (x *Booking) Reset() {
*x = Booking{}
if protoimpl.UnsafeEnabled {
mi := &file_vehicles_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Booking) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Booking) ProtoMessage() {}
func (x *Booking) ProtoReflect() protoreflect.Message {
mi := &file_vehicles_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Booking.ProtoReflect.Descriptor instead.
func (m *Booking) Reset() { *m = Booking{} }
func (m *Booking) String() string { return proto.CompactTextString(m) }
func (*Booking) ProtoMessage() {}
func (*Booking) Descriptor() ([]byte, []int) {
return file_vehicles_proto_rawDescGZIP(), []int{1}
return fileDescriptor_b84b5fa4f4d86273, []int{1}
}
func (x *Booking) GetId() string {
if x != nil {
return x.Id
func (m *Booking) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Booking.Unmarshal(m, b)
}
func (m *Booking) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Booking.Marshal(b, m, deterministic)
}
func (m *Booking) XXX_Merge(src proto.Message) {
xxx_messageInfo_Booking.Merge(m, src)
}
func (m *Booking) XXX_Size() int {
return xxx_messageInfo_Booking.Size(m)
}
func (m *Booking) XXX_DiscardUnknown() {
xxx_messageInfo_Booking.DiscardUnknown(m)
}
var xxx_messageInfo_Booking proto.InternalMessageInfo
func (m *Booking) GetId() string {
if m != nil {
return m.Id
}
return ""
}
func (x *Booking) GetVehicleid() string {
if x != nil {
return x.Vehicleid
func (m *Booking) GetVehicleid() string {
if m != nil {
return m.Vehicleid
}
return ""
}
func (x *Booking) GetDriver() string {
if x != nil {
return x.Driver
func (m *Booking) GetDriver() string {
if m != nil {
return m.Driver
}
return ""
}
func (x *Booking) GetStartdate() *timestamppb.Timestamp {
if x != nil {
return x.Startdate
func (m *Booking) GetStartdate() *timestamppb.Timestamp {
if m != nil {
return m.Startdate
}
return nil
}
func (x *Booking) GetEnddate() *timestamppb.Timestamp {
if x != nil {
return x.Enddate
func (m *Booking) GetEnddate() *timestamppb.Timestamp {
if m != nil {
return m.Enddate
}
return nil
}
func (x *Booking) GetUnavailablefrom() *timestamppb.Timestamp {
if x != nil {
return x.Unavailablefrom
func (m *Booking) GetUnavailablefrom() *timestamppb.Timestamp {
if m != nil {
return m.Unavailablefrom
}
return nil
}
func (x *Booking) GetUnavailableto() *timestamppb.Timestamp {
if x != nil {
return x.Unavailableto
func (m *Booking) GetUnavailableto() *timestamppb.Timestamp {
if m != nil {
return m.Unavailableto
}
return nil
}
func (x *Booking) GetData() *structpb.Struct {
if x != nil {
return x.Data
func (m *Booking) GetData() *structpb.Struct {
if m != nil {
return m.Data
}
return nil
}
func (x *Booking) GetVehicle() *Vehicle {
if x != nil {
return x.Vehicle
func (m *Booking) GetDeleted() bool {
if m != nil {
return m.Deleted
}
return false
}
func (m *Booking) GetVehicle() *Vehicle {
if m != nil {
return m.Vehicle
}
return nil
}
var File_vehicles_proto protoreflect.FileDescriptor
var file_vehicles_proto_rawDesc = []byte{
0x0a, 0x0e, 0x76, 0x65, 0x68, 0x69, 0x63, 0x6c, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,
0x66, 0x2f, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1f,
0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f,
0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22,
0xc6, 0x01, 0x0a, 0x07, 0x56, 0x65, 0x68, 0x69, 0x63, 0x6c, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69,
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x6e,
0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09,
0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70,
0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x26, 0x0a,
0x0e, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x69, 0x73, 0x74, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x73, 0x18,
0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0e, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x69, 0x73, 0x74, 0x72,
0x61, 0x74, 0x6f, 0x72, 0x73, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x05, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, 0x04, 0x64, 0x61,
0x74, 0x61, 0x12, 0x24, 0x0a, 0x08, 0x62, 0x6f, 0x6f, 0x6b, 0x69, 0x6e, 0x67, 0x73, 0x18, 0x06,
0x20, 0x03, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x6f, 0x6f, 0x6b, 0x69, 0x6e, 0x67, 0x52, 0x08,
0x62, 0x6f, 0x6f, 0x6b, 0x69, 0x6e, 0x67, 0x73, 0x22, 0x98, 0x03, 0x0a, 0x07, 0x42, 0x6f, 0x6f,
0x6b, 0x69, 0x6e, 0x67, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x02, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x76, 0x65, 0x68, 0x69, 0x63, 0x6c, 0x65, 0x69,
0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x76, 0x65, 0x68, 0x69, 0x63, 0x6c, 0x65,
0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x72, 0x69, 0x76, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01,
0x28, 0x09, 0x52, 0x06, 0x64, 0x72, 0x69, 0x76, 0x65, 0x72, 0x12, 0x38, 0x0a, 0x09, 0x73, 0x74,
0x61, 0x72, 0x74, 0x64, 0x61, 0x74, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e,
0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e,
0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, 0x73, 0x74, 0x61, 0x72, 0x74,
0x64, 0x61, 0x74, 0x65, 0x12, 0x34, 0x0a, 0x07, 0x65, 0x6e, 0x64, 0x64, 0x61, 0x74, 0x65, 0x18,
0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d,
0x70, 0x52, 0x07, 0x65, 0x6e, 0x64, 0x64, 0x61, 0x74, 0x65, 0x12, 0x44, 0x0a, 0x0f, 0x75, 0x6e,
0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x06, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52,
0x0f, 0x75, 0x6e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x66, 0x72, 0x6f, 0x6d,
0x12, 0x40, 0x0a, 0x0d, 0x75, 0x6e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x74,
0x6f, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74,
0x61, 0x6d, 0x70, 0x52, 0x0d, 0x75, 0x6e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65,
0x74, 0x6f, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12,
0x22, 0x0a, 0x07, 0x76, 0x65, 0x68, 0x69, 0x63, 0x6c, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x08, 0x2e, 0x56, 0x65, 0x68, 0x69, 0x63, 0x6c, 0x65, 0x52, 0x07, 0x76, 0x65, 0x68, 0x69,
0x63, 0x6c, 0x65, 0x42, 0x2e, 0x5a, 0x2c, 0x67, 0x69, 0x74, 0x2e, 0x63, 0x6f, 0x6f, 0x70, 0x67,
0x6f, 0x2e, 0x69, 0x6f, 0x2f, 0x63, 0x6f, 0x6f, 0x70, 0x67, 0x6f, 0x2d, 0x70, 0x6c, 0x61, 0x74,
0x66, 0x6f, 0x72, 0x6d, 0x2f, 0x66, 0x6c, 0x65, 0x65, 0x74, 0x73, 0x2f, 0x67, 0x72, 0x70, 0x63,
0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
func init() {
proto.RegisterType((*Vehicle)(nil), "Vehicle")
proto.RegisterType((*Booking)(nil), "Booking")
}
var (
file_vehicles_proto_rawDescOnce sync.Once
file_vehicles_proto_rawDescData = file_vehicles_proto_rawDesc
)
func file_vehicles_proto_rawDescGZIP() []byte {
file_vehicles_proto_rawDescOnce.Do(func() {
file_vehicles_proto_rawDescData = protoimpl.X.CompressGZIP(file_vehicles_proto_rawDescData)
})
return file_vehicles_proto_rawDescData
func init() {
proto.RegisterFile("vehicles.proto", fileDescriptor_b84b5fa4f4d86273)
}
var file_vehicles_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_vehicles_proto_goTypes = []interface{}{
(*Vehicle)(nil), // 0: Vehicle
(*Booking)(nil), // 1: Booking
(*structpb.Struct)(nil), // 2: google.protobuf.Struct
(*timestamppb.Timestamp)(nil), // 3: google.protobuf.Timestamp
}
var file_vehicles_proto_depIdxs = []int32{
2, // 0: Vehicle.data:type_name -> google.protobuf.Struct
1, // 1: Vehicle.bookings:type_name -> Booking
3, // 2: Booking.startdate:type_name -> google.protobuf.Timestamp
3, // 3: Booking.enddate:type_name -> google.protobuf.Timestamp
3, // 4: Booking.unavailablefrom:type_name -> google.protobuf.Timestamp
3, // 5: Booking.unavailableto:type_name -> google.protobuf.Timestamp
2, // 6: Booking.data:type_name -> google.protobuf.Struct
0, // 7: Booking.vehicle:type_name -> Vehicle
8, // [8:8] is the sub-list for method output_type
8, // [8:8] is the sub-list for method input_type
8, // [8:8] is the sub-list for extension type_name
8, // [8:8] is the sub-list for extension extendee
0, // [0:8] is the sub-list for field type_name
}
func init() { file_vehicles_proto_init() }
func file_vehicles_proto_init() {
if File_vehicles_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_vehicles_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Vehicle); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_vehicles_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Booking); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_vehicles_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_vehicles_proto_goTypes,
DependencyIndexes: file_vehicles_proto_depIdxs,
MessageInfos: file_vehicles_proto_msgTypes,
}.Build()
File_vehicles_proto = out.File
file_vehicles_proto_rawDesc = nil
file_vehicles_proto_goTypes = nil
file_vehicles_proto_depIdxs = nil
var fileDescriptor_b84b5fa4f4d86273 = []byte{
// 384 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0x5d, 0xab, 0xd4, 0x30,
0x10, 0xa5, 0xbb, 0xbd, 0xfd, 0x98, 0x8b, 0x2b, 0xe4, 0x41, 0xc3, 0xe5, 0x82, 0x65, 0x11, 0x29,
0xa8, 0x29, 0x5c, 0x7d, 0xf0, 0x51, 0x2e, 0xfe, 0x82, 0x2a, 0x3e, 0xf8, 0x96, 0x36, 0xd3, 0x1a,
0x6c, 0x9b, 0x90, 0xcc, 0x2e, 0xf8, 0xb7, 0xfc, 0x11, 0xfe, 0x2e, 0x21, 0x6d, 0xf7, 0xea, 0x2a,
0xac, 0x6f, 0x99, 0x73, 0xe6, 0x0c, 0xe7, 0xcc, 0x04, 0x76, 0x47, 0xfc, 0xaa, 0xdb, 0x01, 0xbd,
0xb0, 0xce, 0x90, 0xb9, 0xb9, 0xed, 0x8d, 0xe9, 0x07, 0xac, 0x42, 0xd5, 0x1c, 0xba, 0xca, 0x93,
0x3b, 0xb4, 0xb4, 0xb0, 0xcf, 0xce, 0x59, 0xd2, 0x23, 0x7a, 0x92, 0xa3, 0x9d, 0x1b, 0xf6, 0x3f,
0x23, 0x48, 0x3f, 0xcf, 0x13, 0xd9, 0x0e, 0x36, 0x5a, 0xf1, 0xa8, 0x88, 0xca, 0xbc, 0xde, 0x68,
0xc5, 0x6e, 0x21, 0x9f, 0xe4, 0x88, 0xde, 0xca, 0x16, 0xf9, 0x26, 0xc0, 0x0f, 0x00, 0x63, 0x10,
0xd3, 0x77, 0x8b, 0x7c, 0x1b, 0x88, 0xf0, 0x66, 0x2f, 0x60, 0x27, 0xd5, 0xa8, 0x27, 0xed, 0xc9,
0x49, 0x32, 0xce, 0xf3, 0xb8, 0xd8, 0x96, 0x79, 0x7d, 0x86, 0xb2, 0x97, 0x10, 0x2b, 0x49, 0x92,
0x5f, 0x15, 0x51, 0x79, 0x7d, 0xf7, 0x54, 0xcc, 0x2e, 0xc5, 0xea, 0x52, 0x7c, 0x0c, 0x19, 0xea,
0xd0, 0xc4, 0x9e, 0x43, 0xd6, 0x18, 0xf3, 0x4d, 0x4f, 0xbd, 0xe7, 0x49, 0xb1, 0x2d, 0xaf, 0xef,
0x32, 0x71, 0x3f, 0x03, 0xf5, 0x89, 0xd9, 0xff, 0xd8, 0x42, 0xba, 0xa0, 0xff, 0x0a, 0xb2, 0x6c,
0x4d, 0xab, 0x35, 0xc8, 0x09, 0x60, 0x4f, 0x20, 0x51, 0x4e, 0x1f, 0xd1, 0x2d, 0x51, 0x96, 0x8a,
0xbd, 0x83, 0xdc, 0x93, 0x74, 0xa4, 0x24, 0x21, 0x8f, 0x83, 0xd3, 0x9b, 0xbf, 0x9c, 0x7e, 0x5a,
0xf7, 0x59, 0x3f, 0x34, 0xb3, 0xb7, 0x90, 0xe2, 0xa4, 0x82, 0xee, 0xea, 0xa2, 0x6e, 0x6d, 0x65,
0x1f, 0xe0, 0xf1, 0x61, 0x92, 0x47, 0xa9, 0x07, 0xd9, 0x0c, 0xd8, 0x39, 0x33, 0xf2, 0xe4, 0xa2,
0xfa, 0x5c, 0xc2, 0xde, 0xc3, 0xa3, 0xdf, 0x20, 0x32, 0x3c, 0xbd, 0x38, 0xe3, 0x4f, 0xc1, 0xe9,
0x38, 0xd9, 0xff, 0x1c, 0x87, 0x43, 0xaa, 0x70, 0x40, 0x42, 0xc5, 0xa1, 0x88, 0xca, 0xac, 0x5e,
0x4b, 0xb6, 0x87, 0x74, 0xd9, 0x31, 0xcf, 0xc3, 0xa4, 0x4c, 0x2c, 0x1f, 0xad, 0x5e, 0x89, 0x7b,
0xf1, 0xe5, 0x55, 0xaf, 0x49, 0xb4, 0xc6, 0xd8, 0xde, 0x08, 0x6d, 0xaa, 0xf9, 0xf5, 0xda, 0x0e,
0x92, 0x3a, 0xe3, 0xc6, 0xaa, 0x1b, 0x10, 0xc9, 0x57, 0xbd, 0xb3, 0xad, 0xb4, 0xba, 0x49, 0x82,
0x89, 0x37, 0xbf, 0x02, 0x00, 0x00, 0xff, 0xff, 0xe0, 0xd8, 0xa7, 0x63, 0x05, 0x03, 0x00, 0x00,
}

View File

@@ -23,6 +23,6 @@ message Booking {
google.protobuf.Timestamp unavailablefrom = 6;
google.protobuf.Timestamp unavailableto = 7;
google.protobuf.Struct data = 8;
bool deleted = 10;
Vehicle vehicle = 9;
}

View File

@@ -18,7 +18,6 @@ func main() {
var (
service_name = cfg.GetString("name")
grpc_enable = cfg.GetBool("services.grpc.enable")
dev_env = cfg.GetBool("dev_env")
)
storage, err := storage.NewStorage(cfg)
@@ -29,9 +28,6 @@ func main() {
handler := handlers.NewHandler(cfg, storage)
fmt.Println("Running", service_name, ":")
if dev_env {
fmt.Printf("\033]0;%s\007", service_name)
}
failed := make(chan error)

View File

@@ -17,8 +17,7 @@ type Booking struct {
Unavailablefrom time.Time `json:"unavailablefrom"`
Unavailableto time.Time `json:"unavailableto"`
Data map[string]any `json:"data"`
Deleted bool
Deleted bool `json:"deleted"`
Vehicle Vehicle `json:"vehicle" bson:"-"`
}

View File

@@ -1,577 +0,0 @@
package storage
import (
"ariga.io/atlas/sql/postgres"
"ariga.io/atlas/sql/schema"
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/lib/pq"
_ "github.com/lib/pq"
"github.com/spf13/viper"
"os"
"strconv"
)
type PostgresqlStorage struct {
DbConnection *sql.DB
Schema string
Tables map[string]string
}
func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
var (
host = cfg.GetString("storage.db.psql.host")
port = cfg.GetString("storage.db.psql.port")
user = cfg.GetString("storage.db.psql.user")
password = cfg.GetString("storage.db.psql.password")
dbname = cfg.GetString("storage.db.psql.dbname")
sslmode = cfg.GetString("storage.db.psql.sslmode")
pg_schema = cfg.GetString("storage.db.psql.schema")
pgtables_vehicle = cfg.GetString("storage.db.psql.tables.vehicle")
pgtables_booking = cfg.GetString("storage.db.psql.tables.booking")
timezone = "Europe/Paris"
)
portInt, _ := strconv.Atoi(port)
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s TimeZone=%s", host, portInt,
user, password, dbname, sslmode, timezone)
db, err := sql.Open("postgres", psqlconn)
if err != nil {
fmt.Println("error", err)
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql failed")
}
err = db.Ping()
if err != nil {
fmt.Println(err)
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql database failed")
}
return PostgresqlStorage{
DbConnection: db,
Schema: pg_schema,
Tables: map[string]string{
"vehicle": fmt.Sprintf("%s.%s", pg_schema, pgtables_vehicle),
"booking": fmt.Sprintf("%s.%s", pg_schema, pgtables_booking),
},
}, nil
}
func (psql PostgresqlStorage) CreateVehicle(vehicle Vehicle) error {
tx, err := psql.DbConnection.Begin()
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
vehicleQuery := fmt.Sprintf(`
INSERT INTO %s (id, type, namespace, administrators, data, metadata)
VALUES ($1, $2, $3, $4, $5, $6)
`, psql.Tables["vehicle"])
data, err := json.Marshal(vehicle.Data)
if err != nil {
return err
}
metadata, err := json.Marshal(vehicle.Metadata)
if err != nil {
return err
}
_, err = tx.Exec(vehicleQuery, vehicle.ID, vehicle.Type, vehicle.Namespace, pq.Array(vehicle.Administrators),
data, metadata)
if err != nil {
tx.Rollback()
return fmt.Errorf("failed to insert vehicle: %w", err)
}
bookingQuery := fmt.Sprintf(`
INSERT INTO %s (id, vehicleid, driver, startdate, enddate, unavailablefrom, unavailableto, data, deleted)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
`, psql.Tables["booking"])
for _, booking := range vehicle.Bookings {
dataBooking, err := json.Marshal(booking.Data)
if err != nil {
return err
}
_, err = tx.Exec(bookingQuery, booking.ID, booking.Vehicleid, booking.Driver,
booking.Startdate, booking.Enddate, booking.Unavailablefrom, booking.Unavailableto,
dataBooking, booking.Deleted)
if err != nil {
tx.Rollback()
return fmt.Errorf("failed to insert booking: %w", err)
}
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}
func (psql PostgresqlStorage) GetVehicle(id string) (*Vehicle, error) {
vehicle := &Vehicle{}
var vehicleData []byte
var vehicleMetadata []byte
query := fmt.Sprintf(`
SELECT id, type, namespace, administrators, data, metadata
FROM %s
WHERE id = $1
`, psql.Tables["vehicle"])
err := psql.DbConnection.QueryRow(query, id).Scan(
&vehicle.ID,
&vehicle.Type,
&vehicle.Namespace,
pq.Array(&vehicle.Administrators),
&vehicleData,
&vehicleMetadata,
)
err = json.Unmarshal(vehicleData, &vehicle.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle data")
}
err = json.Unmarshal(vehicleMetadata, &vehicle.Metadata)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle metadata")
}
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("vehicle not found")
}
return nil, fmt.Errorf("failed to query database: %w", err)
}
return vehicle, nil
}
func (psql PostgresqlStorage) GetVehicles(namespaces []string) ([]Vehicle, error) {
var vehicles []Vehicle
if len(namespaces) == 0 {
return vehicles, nil
}
query := fmt.Sprintf(`
SELECT id, type, namespace, administrators, data, metadata
FROM %s
WHERE namespace = ANY($1)
`, psql.Tables["vehicle"])
rows, err := psql.DbConnection.Query(query, pq.Array(namespaces))
if err != nil {
return nil, fmt.Errorf("failed to query database: %w", err)
}
defer rows.Close()
for rows.Next() {
var vehicle Vehicle
var administrators []string
var vehicleData []byte
var vehicleMetadata []byte
scanArgs := []interface{}{
&vehicle.ID,
&vehicle.Type,
&vehicle.Namespace,
pq.Array(&administrators),
&vehicleData,
&vehicleMetadata,
}
err := rows.Scan(scanArgs...)
if err != nil {
return nil, fmt.Errorf("failed to scan row: %w", err)
}
vehicle.Administrators = administrators
err = json.Unmarshal(vehicleData, &vehicle.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(vehicleMetadata, &vehicle.Metadata)
if err != nil {
return nil, err
}
vehicles = append(vehicles, vehicle)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("unexpected error after iterating rows: %w", err)
}
return vehicles, nil
}
func (psql PostgresqlStorage) CreateBooking(booking Booking) error {
query := fmt.Sprintf(`
INSERT INTO %s (id, vehicleid, driver, startdate, enddate, unavailablefrom, unavailableto, data, deleted)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
`, psql.Tables["booking"])
bookingData, err := json.Marshal(booking.Data)
if err != nil {
return fmt.Errorf("failed to marshal booking data: %w", err)
}
_, err = psql.DbConnection.Exec(query, booking.ID, booking.Vehicleid, booking.Driver, booking.Startdate,
booking.Enddate, booking.Unavailablefrom, booking.Unavailableto, bookingData, booking.Deleted)
if err != nil {
return fmt.Errorf("failed to insert booking record: %w", err)
}
return nil
}
func (psql PostgresqlStorage) UpdateBooking(bookingToUpdate Booking) error {
query := fmt.Sprintf(`
UPDATE %s SET
driver = $1,
startdate = $2,
enddate = $3,
unavailablefrom = $4,
unavailableto = $5,
data = $6,
deleted = $7
WHERE id = $8
`, psql.Tables["booking"])
bookingData, err := json.Marshal(bookingToUpdate.Data)
if err != nil {
return fmt.Errorf("failed to marshal booking data: %w", err)
}
_, err = psql.DbConnection.Exec(
query,
bookingToUpdate.Driver,
bookingToUpdate.Startdate,
bookingToUpdate.Enddate,
bookingToUpdate.Unavailablefrom,
bookingToUpdate.Unavailableto,
bookingData,
bookingToUpdate.Deleted,
bookingToUpdate.ID,
)
if err != nil {
return fmt.Errorf("failed to update booking: %w", err)
}
return nil
}
func (psql PostgresqlStorage) GetBooking(id string) (*Booking, error) {
booking := &Booking{}
query := fmt.Sprintf(`
SELECT b.id, b.vehicleid, b.driver, b.startdate, b.enddate,
b.unavailablefrom, b.unavailableto, b.data, b.deleted,
v.id, v.type, v.namespace, v.administrators, v.data, v.metadata
FROM %s b
LEFT JOIN %s v ON b.vehicleid = v.id
WHERE b.id = $1
`, psql.Tables["booking"], psql.Tables["vehicle"])
row := psql.DbConnection.QueryRow(query, id)
var administrators []string
var bookingData []byte
var vehicleData []byte
var vehicleMetadata []byte
err := row.Scan(
&booking.ID,
&booking.Vehicleid,
&booking.Driver,
&booking.Startdate,
&booking.Enddate,
&booking.Unavailablefrom,
&booking.Unavailableto,
&bookingData,
&booking.Deleted,
&booking.Vehicle.ID,
&booking.Vehicle.Type,
&booking.Vehicle.Namespace,
pq.Array(&administrators),
&vehicleData,
&vehicleMetadata,
)
if err != nil {
return nil, fmt.Errorf("error receiving booking: %w", err)
}
err = json.Unmarshal(bookingData, &booking.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal booking data: %w", err)
}
err = json.Unmarshal(vehicleData, &booking.Vehicle.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(vehicleMetadata, &booking.Vehicle.Metadata)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle metadata: %w", err)
}
booking.Vehicle.Administrators = administrators
return booking, nil
}
func (psql PostgresqlStorage) GetBookings() ([]Booking, error) {
var bookings []Booking
query := fmt.Sprintf(`
SELECT b.id, b.vehicleid, b.driver, b.startdate, b.enddate,
b.unavailablefrom, b.unavailableto, b.data, b.deleted,
v.id, v.type, v.namespace, v.administrators, v.data, v.metadata
FROM %s b
LEFT JOIN %s v ON b.vehicleid = v.id
`, psql.Tables["booking"], psql.Tables["vehicle"])
rows, err := psql.DbConnection.Query(query)
if err != nil {
return nil, fmt.Errorf("failed to retrieve bookings: %w", err)
}
defer rows.Close()
for rows.Next() {
var booking Booking
var administrators []string
var bookingData []byte
var vehicleData []byte
var vehicleMetadata []byte
err = rows.Scan(
&booking.ID,
&booking.Vehicleid,
&booking.Driver,
&booking.Startdate,
&booking.Enddate,
&booking.Unavailablefrom,
&booking.Unavailableto,
&bookingData,
&booking.Deleted,
&booking.Vehicle.ID,
&booking.Vehicle.Type,
&booking.Vehicle.Namespace,
pq.Array(&administrators),
&vehicleData,
&vehicleMetadata,
)
if err != nil {
return nil, fmt.Errorf("error receiving booking: %w", err)
}
err = json.Unmarshal(bookingData, &booking.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal booking data: %w", err)
}
err = json.Unmarshal(vehicleData, &booking.Vehicle.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(vehicleMetadata, &booking.Vehicle.Metadata)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle metadata: %w", err)
}
booking.Vehicle.Administrators = administrators
bookings = append(bookings, booking)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("failed to retrieve bookings: %w", err)
}
return bookings, nil
}
func (psql PostgresqlStorage) GetBookingsForVehicle(vehicleid string) ([]Booking, error) {
var bookings []Booking
query := fmt.Sprintf(`
SELECT b.id, b.vehicleid, b.driver, b.startdate, b.enddate,
b.unavailablefrom, b.unavailableto, b.data, b.deleted,
v.id, v.type, v.namespace, v.administrators, v.data, v.metadata
FROM %s b
LEFT JOIN %s v ON b.vehicleid = v.id
WHERE b.vehicleid = $1
`, psql.Tables["booking"], psql.Tables["vehicle"])
rows, err := psql.DbConnection.Query(query, vehicleid)
if err != nil {
return nil, fmt.Errorf("error retrieving bookings for vehicle: %w", err)
}
for rows.Next() {
var administrators []string
var bookingData []byte
var vehicleData []byte
var vehicleMetadata []byte
var booking Booking
err := rows.Scan(
&booking.ID,
&booking.Vehicleid,
&booking.Driver,
&booking.Startdate,
&booking.Enddate,
&booking.Unavailablefrom,
&booking.Unavailableto,
&bookingData,
&booking.Deleted,
&booking.Vehicle.ID,
&booking.Vehicle.Type,
&booking.Vehicle.Namespace,
pq.Array(&administrators),
&vehicleData,
&vehicleMetadata,
)
if err != nil {
return nil, fmt.Errorf("error scanning booking row: %w", err)
}
err = json.Unmarshal(bookingData, &booking.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal booking data: %w", err)
}
err = json.Unmarshal(vehicleData, &booking.Vehicle.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle data: %w", err)
}
err = json.Unmarshal(vehicleMetadata, &booking.Vehicle.Metadata)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle metadata: %w", err)
}
booking.Vehicle.Administrators = administrators
bookings = append(bookings, booking)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over booking rows: %w", err)
}
return bookings, nil
}
func (psql PostgresqlStorage) GetBookingsForDriver(driver string) ([]Booking, error) {
bookings := []Booking{}
query := fmt.Sprintf(`
SELECT b.id, b.vehicleid, b.driver, b.startdate, b.enddate,
b.unavailablefrom, b.unavailableto, b.data, b.deleted,
v.id, v.type, v.namespace, v.administrators, v.data, v.metadata
FROM %s b
LEFT JOIN %s v ON b.vehicleid = v.id
WHERE b.driver = $1
`, psql.Tables["booking"], psql.Tables["vehicle"])
rows, err := psql.DbConnection.Query(query, driver)
if err != nil {
return nil, fmt.Errorf("error querying bookings: %w", err)
}
defer rows.Close()
for rows.Next() {
booking := Booking{}
var administrators []string
var bookingData []byte
var vehicleData []byte
var vehicleMetadata []byte
err = rows.Scan(
&booking.ID,
&booking.Vehicleid,
&booking.Driver,
&booking.Startdate,
&booking.Enddate,
&booking.Unavailablefrom,
&booking.Unavailableto,
&bookingData,
&booking.Deleted,
&booking.Vehicle.ID,
&booking.Vehicle.Type,
&booking.Vehicle.Namespace,
pq.Array(&administrators),
&vehicleData,
&vehicleMetadata,
)
if err != nil {
return nil, fmt.Errorf("error scanning bookings: %w", err)
}
err = json.Unmarshal(bookingData, &booking.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal booking data: %w", err)
}
err = json.Unmarshal(vehicleData, &booking.Vehicle.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle data: %w", err)
}
err = json.Unmarshal(vehicleMetadata, &booking.Vehicle.Metadata)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vehicle metadata: %w", err)
}
booking.Vehicle.Administrators = administrators
bookings = append(bookings, booking)
}
return bookings, nil
}
func (psql PostgresqlStorage) DeleteBooking(id string) error {
query := fmt.Sprintf(`
DELETE FROM %s
WHERE id = $1
`, psql.Tables["booking"])
_, err := psql.DbConnection.Exec(query, id)
if err != nil {
return fmt.Errorf("error deleting booking: %v", err)
}
return nil
}
func (psql PostgresqlStorage) Migrate() error {
ctx := context.Background()
driver, err := postgres.Open(psql.DbConnection)
if err != nil {
return err
}
existing, err := driver.InspectRealm(ctx, &schema.InspectRealmOption{Schemas: []string{psql.Schema}})
if err != nil {
return err
}
var desired schema.Realm
hcl, err := os.ReadFile("postgresql/schema.hcl")
if err != nil {
return err
}
err = postgres.EvalHCLBytes(hcl, &desired, nil)
if err != nil {
return err
}
diff, err := driver.RealmDiff(existing, &desired)
if err != nil {
return err
}
err = driver.ApplyChanges(ctx, diff)
if err != nil {
return err
}
return nil
}

View File

@@ -1,88 +0,0 @@
table "booking" {
schema = schema.fleets
column "id" {
null = false
type = uuid
}
column "vehicleid" {
null = true
type = uuid
}
column "driver" {
null = true
type = text
}
column "startdate" {
null = true
type = timestamptz
}
column "enddate" {
null = true
type = timestamptz
}
column "unavailablefrom" {
null = true
type = timestamptz
}
column "unavailableto" {
null = true
type = timestamptz
}
column "data" {
null = true
type = jsonb
}
column "deleted" {
null = true
type = boolean
}
primary_key {
columns = [column.id]
}
foreign_key "booking_vehicleid_fkey" {
columns = [column.vehicleid]
ref_columns = [table.vehicle.column.id]
on_update = NO_ACTION
on_delete = NO_ACTION
}
foreign_key "vehicle_booking_fk" {
columns = [column.vehicleid]
ref_columns = [table.vehicle.column.id]
on_update = NO_ACTION
on_delete = NO_ACTION
}
}
table "vehicle" {
schema = schema.fleets
column "id" {
null = false
type = uuid
}
column "type" {
null = true
type = text
}
column "namespace" {
null = true
type = text
}
column "administrators" {
null = true
type = sql("text[]")
}
column "data" {
null = true
type = jsonb
}
column "metadata" {
null = true
type = jsonb
}
primary_key {
columns = [column.id]
}
}
schema "fleets" {
}

View File

@@ -31,9 +31,6 @@ func NewStorage(cfg *viper.Viper) (Storage, error) {
case "mongodb":
s, err := NewMongoDBStorage(cfg)
return s, err
case "psql":
s, err := NewPostgresqlStorage(cfg)
return s, err
default:
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
}

View File

@@ -17,14 +17,15 @@ type Vehicle struct {
}
type VehicleFilters struct {
Types []string
Administrators []string
AvailableFrom time.Time
AvailableTo time.Time
Types []string
AvailableFrom time.Time
AvailableTo time.Time
}
func (v Vehicle) Free(start time.Time, end time.Time) bool {
for _, b := range v.Bookings {
fmt.Println("Bookings for", v)
fmt.Println(b)
if ((start.Before(b.Unavailablefrom) || start.Equal(b.Unavailablefrom)) && (end.After(b.Unavailablefrom.Add(24*time.Hour)) || end.Equal(b.Unavailablefrom.Add(24*time.Hour)))) ||
((start.Before(b.Unavailableto) || start.Equal(b.Unavailableto)) && (end.After(b.Unavailableto.Add(24*time.Hour)) || end.Equal(b.Unavailableto.Add(24*time.Hour)))) ||
((start.After(b.Unavailablefrom) || start.Equal(b.Unavailablefrom)) && (end.Before(b.Unavailableto.Add(24*time.Hour)) || end.Equal(b.Unavailableto.Add(24*time.Hour)))) {
@@ -35,8 +36,6 @@ func (v Vehicle) Free(start time.Time, end time.Time) bool {
}
func (v Vehicle) MatchesFilters(filters VehicleFilters) bool {
fmt.Println("Filters : ", filters)
if len(filters.Types) > 0 {
found := false
for _, t := range filters.Types {
@@ -50,24 +49,11 @@ func (v Vehicle) MatchesFilters(filters VehicleFilters) bool {
}
}
if len(filters.Administrators) > 0 {
found := false
for _, a := range filters.Administrators {
for _, va := range v.Administrators {
if a == va {
found = true
break
}
}
}
if !found {
return false
}
}
if !v.Free(filters.AvailableFrom, filters.AvailableTo) {
return false
}
fmt.Println(filters)
return true
}