diff --git a/.gitignore b/.gitignore index 3479fd2..d16774f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ racing/racing *.out .idea .vscode - +*.exe \ No newline at end of file diff --git a/api/proto/racing/racing.pb.go b/api/proto/racing/racing.pb.go index bf8907a..4f6ae3a 100644 --- a/api/proto/racing/racing.pb.go +++ b/api/proto/racing/racing.pb.go @@ -22,6 +22,56 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +// SortDirection lets a caller influence ordering of list results. +type SortDirection int32 + +const ( + SortDirection_SORT_DIRECTION_UNSPECIFIED SortDirection = 0 // Defaults to ascending. + SortDirection_SORT_DIRECTION_ASC SortDirection = 1 // Ascending advertised_start_time. + SortDirection_SORT_DIRECTION_DESC SortDirection = 2 // Descending advertised_start_time. +) + +// Enum value maps for SortDirection. +var ( + SortDirection_name = map[int32]string{ + 0: "SORT_DIRECTION_UNSPECIFIED", + 1: "SORT_DIRECTION_ASC", + 2: "SORT_DIRECTION_DESC", + } + SortDirection_value = map[string]int32{ + "SORT_DIRECTION_UNSPECIFIED": 0, + "SORT_DIRECTION_ASC": 1, + "SORT_DIRECTION_DESC": 2, + } +) + +func (x SortDirection) Enum() *SortDirection { + p := new(SortDirection) + *p = x + return p +} + +func (x SortDirection) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (SortDirection) Descriptor() protoreflect.EnumDescriptor { + return file_racing_racing_proto_enumTypes[0].Descriptor() +} + +func (SortDirection) Type() protoreflect.EnumType { + return &file_racing_racing_proto_enumTypes[0] +} + +func (x SortDirection) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SortDirection.Descriptor instead. +func (SortDirection) EnumDescriptor() ([]byte, []int) { + return file_racing_racing_proto_rawDescGZIP(), []int{0} +} + // Request for ListRaces call. type ListRacesRequest struct { state protoimpl.MessageState @@ -128,6 +178,9 @@ type ListRacesRequestFilter struct { // visible_only, when true, restricts results to only races marked visible. // When omitted or false, all races (visible or not) are returned. VisibleOnly *bool `protobuf:"varint,2,opt,name=visible_only,json=visibleOnly,proto3,oneof" json:"visible_only,omitempty"` + // sort_direction chooses ascending or descending order by advertised_start_time. + // If omitted or UNSPECIFIED the results are returned ascending (earliest first). + SortDirection *SortDirection `protobuf:"varint,3,opt,name=sort_direction,json=sortDirection,proto3,enum=racing.SortDirection,oneof" json:"sort_direction,omitempty"` } func (x *ListRacesRequestFilter) Reset() { @@ -176,6 +229,13 @@ func (x *ListRacesRequestFilter) GetVisibleOnly() bool { return false } +func (x *ListRacesRequestFilter) GetSortDirection() SortDirection { + if x != nil && x.SortDirection != nil { + return *x.SortDirection + } + return SortDirection_SORT_DIRECTION_UNSPECIFIED +} + // A race resource. type Race struct { state protoimpl.MessageState @@ -287,34 +347,46 @@ var file_racing_racing_proto_rawDesc = []byte{ 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x72, 0x61, 0x63, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x52, 0x61, 0x63, 0x65, 0x52, 0x05, 0x72, 0x61, 0x63, 0x65, - 0x73, 0x22, 0x72, 0x0a, 0x16, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x1f, 0x0a, 0x0b, 0x6d, - 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x03, - 0x52, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x73, 0x12, 0x26, 0x0a, 0x0c, - 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x08, 0x48, 0x00, 0x52, 0x0b, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x4f, 0x6e, 0x6c, - 0x79, 0x88, 0x01, 0x01, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, - 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x22, 0xcb, 0x01, 0x0a, 0x04, 0x52, 0x61, 0x63, 0x65, 0x12, 0x0e, - 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1d, - 0x0a, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x03, 0x52, 0x09, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x12, 0x12, 0x0a, - 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, - 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, - 0x03, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x69, 0x73, - 0x69, 0x62, 0x6c, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x76, 0x69, 0x73, 0x69, - 0x62, 0x6c, 0x65, 0x12, 0x4e, 0x0a, 0x15, 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, 0x73, 0x65, - 0x64, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 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, 0x13, - 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, 0x73, 0x65, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x54, - 0x69, 0x6d, 0x65, 0x32, 0x65, 0x0a, 0x06, 0x52, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x12, 0x5b, 0x0a, - 0x09, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x12, 0x18, 0x2e, 0x72, 0x61, 0x63, - 0x69, 0x6e, 0x67, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x4c, 0x69, - 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, - 0x19, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x22, 0x0e, 0x2f, 0x76, 0x31, 0x2f, 0x6c, 0x69, 0x73, - 0x74, 0x2d, 0x72, 0x61, 0x63, 0x65, 0x73, 0x3a, 0x01, 0x2a, 0x42, 0x09, 0x5a, 0x07, 0x2f, 0x72, - 0x61, 0x63, 0x69, 0x6e, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x73, 0x22, 0xc8, 0x01, 0x0a, 0x16, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x1f, 0x0a, 0x0b, + 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, + 0x03, 0x52, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x73, 0x12, 0x26, 0x0a, + 0x0c, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x08, 0x48, 0x00, 0x52, 0x0b, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x4f, 0x6e, + 0x6c, 0x79, 0x88, 0x01, 0x01, 0x12, 0x41, 0x0a, 0x0e, 0x73, 0x6f, 0x72, 0x74, 0x5f, 0x64, 0x69, + 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x15, 0x2e, + 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x44, 0x69, 0x72, 0x65, 0x63, + 0x74, 0x69, 0x6f, 0x6e, 0x48, 0x01, 0x52, 0x0d, 0x73, 0x6f, 0x72, 0x74, 0x44, 0x69, 0x72, 0x65, + 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x88, 0x01, 0x01, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x76, 0x69, 0x73, + 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x73, 0x6f, + 0x72, 0x74, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xcb, 0x01, 0x0a, + 0x04, 0x52, 0x61, 0x63, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, + 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6d, 0x65, 0x65, 0x74, 0x69, + 0x6e, 0x67, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, + 0x65, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, + 0x12, 0x18, 0x0a, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x08, 0x52, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x12, 0x4e, 0x0a, 0x15, 0x61, 0x64, + 0x76, 0x65, 0x72, 0x74, 0x69, 0x73, 0x65, 0x64, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, + 0x69, 0x6d, 0x65, 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, 0x13, 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, 0x73, 0x65, + 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x2a, 0x60, 0x0a, 0x0d, 0x53, 0x6f, + 0x72, 0x74, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1e, 0x0a, 0x1a, 0x53, + 0x4f, 0x52, 0x54, 0x5f, 0x44, 0x49, 0x52, 0x45, 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x55, 0x4e, + 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x16, 0x0a, 0x12, 0x53, + 0x4f, 0x52, 0x54, 0x5f, 0x44, 0x49, 0x52, 0x45, 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x41, 0x53, + 0x43, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x53, 0x4f, 0x52, 0x54, 0x5f, 0x44, 0x49, 0x52, 0x45, + 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x44, 0x45, 0x53, 0x43, 0x10, 0x02, 0x32, 0x65, 0x0a, 0x06, + 0x52, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x12, 0x5b, 0x0a, 0x09, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, + 0x63, 0x65, 0x73, 0x12, 0x18, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x4c, 0x69, 0x73, + 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, + 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x19, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, + 0x22, 0x0e, 0x2f, 0x76, 0x31, 0x2f, 0x6c, 0x69, 0x73, 0x74, 0x2d, 0x72, 0x61, 0x63, 0x65, 0x73, + 0x3a, 0x01, 0x2a, 0x42, 0x09, 0x5a, 0x07, 0x2f, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -329,25 +401,28 @@ func file_racing_racing_proto_rawDescGZIP() []byte { return file_racing_racing_proto_rawDescData } +var file_racing_racing_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_racing_racing_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_racing_racing_proto_goTypes = []interface{}{ - (*ListRacesRequest)(nil), // 0: racing.ListRacesRequest - (*ListRacesResponse)(nil), // 1: racing.ListRacesResponse - (*ListRacesRequestFilter)(nil), // 2: racing.ListRacesRequestFilter - (*Race)(nil), // 3: racing.Race - (*timestamppb.Timestamp)(nil), // 4: google.protobuf.Timestamp + (SortDirection)(0), // 0: racing.SortDirection + (*ListRacesRequest)(nil), // 1: racing.ListRacesRequest + (*ListRacesResponse)(nil), // 2: racing.ListRacesResponse + (*ListRacesRequestFilter)(nil), // 3: racing.ListRacesRequestFilter + (*Race)(nil), // 4: racing.Race + (*timestamppb.Timestamp)(nil), // 5: google.protobuf.Timestamp } var file_racing_racing_proto_depIdxs = []int32{ - 2, // 0: racing.ListRacesRequest.filter:type_name -> racing.ListRacesRequestFilter - 3, // 1: racing.ListRacesResponse.races:type_name -> racing.Race - 4, // 2: racing.Race.advertised_start_time:type_name -> google.protobuf.Timestamp - 0, // 3: racing.Racing.ListRaces:input_type -> racing.ListRacesRequest - 1, // 4: racing.Racing.ListRaces:output_type -> racing.ListRacesResponse - 4, // [4:5] is the sub-list for method output_type - 3, // [3:4] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 3, // 0: racing.ListRacesRequest.filter:type_name -> racing.ListRacesRequestFilter + 4, // 1: racing.ListRacesResponse.races:type_name -> racing.Race + 0, // 2: racing.ListRacesRequestFilter.sort_direction:type_name -> racing.SortDirection + 5, // 3: racing.Race.advertised_start_time:type_name -> google.protobuf.Timestamp + 1, // 4: racing.Racing.ListRaces:input_type -> racing.ListRacesRequest + 2, // 5: racing.Racing.ListRaces:output_type -> racing.ListRacesResponse + 5, // [5:6] is the sub-list for method output_type + 4, // [4:5] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_racing_racing_proto_init() } @@ -411,13 +486,14 @@ func file_racing_racing_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_racing_racing_proto_rawDesc, - NumEnums: 0, + NumEnums: 1, NumMessages: 4, NumExtensions: 0, NumServices: 1, }, GoTypes: file_racing_racing_proto_goTypes, DependencyIndexes: file_racing_racing_proto_depIdxs, + EnumInfos: file_racing_racing_proto_enumTypes, MessageInfos: file_racing_racing_proto_msgTypes, }.Build() File_racing_racing_proto = out.File diff --git a/api/proto/racing/racing.proto b/api/proto/racing/racing.proto index 2e44b91..9e88a27 100644 --- a/api/proto/racing/racing.proto +++ b/api/proto/racing/racing.proto @@ -6,6 +6,13 @@ option go_package = "/racing"; import "google/protobuf/timestamp.proto"; import "google/api/annotations.proto"; +// SortDirection lets a caller influence ordering of list results. +enum SortDirection { + SORT_DIRECTION_UNSPECIFIED = 0; // Defaults to ascending. + SORT_DIRECTION_ASC = 1; // Ascending advertised_start_time. + SORT_DIRECTION_DESC = 2; // Descending advertised_start_time. +} + service Racing { // ListRaces returns a list of all races. rpc ListRaces(ListRacesRequest) returns (ListRacesResponse) { @@ -31,6 +38,9 @@ message ListRacesRequestFilter { // visible_only, when true, restricts results to only races marked visible. // When omitted or false, all races (visible or not) are returned. optional bool visible_only = 2; + // sort_direction chooses ascending or descending order by advertised_start_time. + // If omitted or UNSPECIFIED the results are returned ascending (earliest first). + optional SortDirection sort_direction = 3; } /* Resources */ diff --git a/racing/db/races.go b/racing/db/races.go index 99805f7..7eea074 100644 --- a/racing/db/races.go +++ b/racing/db/races.go @@ -68,27 +68,37 @@ func (r *racesRepo) applyFilter(query string, filter *racing.ListRacesRequestFil args []interface{} ) - if filter == nil { - return query, args - } - - if len(filter.MeetingIds) > 0 { - clauses = append(clauses, "meeting_id IN ("+strings.Repeat("?,", len(filter.MeetingIds)-1)+"?)") + if filter != nil { + if len(filter.MeetingIds) > 0 { + clauses = append(clauses, "meeting_id IN ("+strings.Repeat("?,", len(filter.MeetingIds)-1)+"?)") - for _, meetingID := range filter.MeetingIds { - args = append(args, meetingID) + for _, meetingID := range filter.MeetingIds { + args = append(args, meetingID) + } } - } - // visible_only filter: only return rows where visible = 1 when explicitly true. - if filter.VisibleOnly != nil && *filter.VisibleOnly { - clauses = append(clauses, "visible = 1") + // visible_only filter: only return rows where visible = 1 when explicitly true. + if filter.VisibleOnly != nil && *filter.VisibleOnly { + clauses = append(clauses, "visible = 1") + } } if len(clauses) != 0 { query += " WHERE " + strings.Join(clauses, " AND ") } + // Always order by advertised_start_time (ascending by default). + order := " ORDER BY advertised_start_time ASC" + if filter != nil && filter.SortDirection != nil { + switch *filter.SortDirection { + case racing.SortDirection_SORT_DIRECTION_DESC: + order = " ORDER BY advertised_start_time DESC" + case racing.SortDirection_SORT_DIRECTION_ASC, racing.SortDirection_SORT_DIRECTION_UNSPECIFIED: + order = " ORDER BY advertised_start_time ASC" + } + } + query += order + return query, args } diff --git a/racing/db/races_test.go b/racing/db/races_test.go new file mode 100644 index 0000000..17405f2 --- /dev/null +++ b/racing/db/races_test.go @@ -0,0 +1,101 @@ +package db + +import ( + "regexp" + "testing" + "time" + + "git.neds.sh/matty/entain/racing/proto/racing" + "github.com/DATA-DOG/go-sqlmock" + "google.golang.org/protobuf/types/known/timestamppb" +) + +// helper to build timestamp +func mustTS(t *testing.T, tm time.Time) *racing.Race { + ts := timestamppb.New(tm) + return &racing.Race{AdvertisedStartTime: ts} +} + +func TestList_DefaultAscending(t *testing.T) { + db, mock, err := sqlmock.New() + if err != nil { + t.Fatalf("mock: %v", err) + } + defer db.Close() + + r := NewRacesRepo(db) + + rows := sqlmock.NewRows([]string{"id", "meeting_id", "name", "number", "visible", "advertised_start_time"}). + AddRow(1, 1, "A", 1, 1, time.Now().Add(1*time.Hour)). + AddRow(2, 1, "B", 2, 1, time.Now().Add(2*time.Hour)) + + mock.ExpectQuery(regexp.QuoteMeta("SELECT \n\t\t\tid, \n\t\t\tmeeting_id, \n\t\t\tname, \n\t\t\tnumber, \n\t\t\tvisible, \n\t\t\tadvertised_start_time \n\t\tFROM races ORDER BY advertised_start_time ASC")). + WillReturnRows(rows) + + out, err := r.List(nil) + if err != nil { + t.Fatalf("list: %v", err) + } + if len(out) != 2 { + t.Fatalf("expected 2 rows got %d", len(out)) + } +} + +func TestList_Descending(t *testing.T) { + db, mock, err := sqlmock.New() + if err != nil { + t.Fatalf("mock: %v", err) + } + defer db.Close() + r := NewRacesRepo(db) + + dir := racing.SortDirection_SORT_DIRECTION_DESC + filter := &racing.ListRacesRequestFilter{SortDirection: &dir} + + rows := sqlmock.NewRows([]string{"id", "meeting_id", "name", "number", "visible", "advertised_start_time"}). + AddRow(2, 1, "B", 2, 1, time.Now().Add(2*time.Hour)). + AddRow(1, 1, "A", 1, 1, time.Now().Add(1*time.Hour)) + + mock.ExpectQuery("ORDER BY advertised_start_time DESC"). + WillReturnRows(rows) + + out, err := r.List(filter) + if err != nil { + t.Fatalf("list: %v", err) + } + if len(out) != 2 { + t.Fatalf("expected 2 rows got %d", len(out)) + } + if out[0].Id != 2 { + t.Fatalf("expected first id 2 got %d", out[0].Id) + } +} + +func TestList_VisibleOnly(t *testing.T) { + db, mock, err := sqlmock.New() + if err != nil { + t.Fatalf("mock: %v", err) + } + defer db.Close() + r := NewRacesRepo(db) + + vo := true + filter := &racing.ListRacesRequestFilter{VisibleOnly: &vo} + + rows := sqlmock.NewRows([]string{"id", "meeting_id", "name", "number", "visible", "advertised_start_time"}). + AddRow(1, 1, "A", 1, 1, time.Now().Add(1*time.Hour)) + + mock.ExpectQuery("WHERE visible = 1 ORDER BY advertised_start_time ASC"). + WillReturnRows(rows) + + out, err := r.List(filter) + if err != nil { + t.Fatalf("list: %v", err) + } + if len(out) != 1 { + t.Fatalf("expected 1 row got %d", len(out)) + } + if !out[0].Visible { + t.Fatalf("expected visible race") + } +} diff --git a/racing/go.mod b/racing/go.mod index 9b9181b..eb76820 100644 --- a/racing/go.mod +++ b/racing/go.mod @@ -3,7 +3,8 @@ module git.neds.sh/matty/entain/racing go 1.23 require ( - github.com/golang/protobuf v1.5.4 // indirect legacy proto + github.com/DATA-DOG/go-sqlmock v1.5.2 + github.com/golang/protobuf v1.5.4 github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 github.com/mattn/go-sqlite3 v1.14.22 golang.org/x/net v0.26.0 diff --git a/racing/go.sum b/racing/go.sum index 03fa1b1..284f917 100644 --- a/racing/go.sum +++ b/racing/go.sum @@ -1,3 +1,5 @@ +github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= +github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= @@ -5,6 +7,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is= github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM= +github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= diff --git a/racing/proto/racing/racing.pb.go b/racing/proto/racing/racing.pb.go index 3a00a3c..6ca87d8 100644 --- a/racing/proto/racing/racing.pb.go +++ b/racing/proto/racing/racing.pb.go @@ -21,6 +21,56 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +// SortDirection lets a caller influence ordering of list results. +type SortDirection int32 + +const ( + SortDirection_SORT_DIRECTION_UNSPECIFIED SortDirection = 0 // Defaults to ascending. + SortDirection_SORT_DIRECTION_ASC SortDirection = 1 // Ascending advertised_start_time. + SortDirection_SORT_DIRECTION_DESC SortDirection = 2 // Descending advertised_start_time. +) + +// Enum value maps for SortDirection. +var ( + SortDirection_name = map[int32]string{ + 0: "SORT_DIRECTION_UNSPECIFIED", + 1: "SORT_DIRECTION_ASC", + 2: "SORT_DIRECTION_DESC", + } + SortDirection_value = map[string]int32{ + "SORT_DIRECTION_UNSPECIFIED": 0, + "SORT_DIRECTION_ASC": 1, + "SORT_DIRECTION_DESC": 2, + } +) + +func (x SortDirection) Enum() *SortDirection { + p := new(SortDirection) + *p = x + return p +} + +func (x SortDirection) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (SortDirection) Descriptor() protoreflect.EnumDescriptor { + return file_racing_racing_proto_enumTypes[0].Descriptor() +} + +func (SortDirection) Type() protoreflect.EnumType { + return &file_racing_racing_proto_enumTypes[0] +} + +func (x SortDirection) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SortDirection.Descriptor instead. +func (SortDirection) EnumDescriptor() ([]byte, []int) { + return file_racing_racing_proto_rawDescGZIP(), []int{0} +} + type ListRacesRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -126,6 +176,9 @@ type ListRacesRequestFilter struct { // visible_only, when true, restricts results to only races marked visible. // When omitted or false, all races (visible or not) are returned. VisibleOnly *bool `protobuf:"varint,2,opt,name=visible_only,json=visibleOnly,proto3,oneof" json:"visible_only,omitempty"` + // sort_direction chooses ascending or descending order by advertised_start_time. + // If omitted or UNSPECIFIED the results are returned ascending (earliest first). + SortDirection *SortDirection `protobuf:"varint,3,opt,name=sort_direction,json=sortDirection,proto3,enum=racing.SortDirection,oneof" json:"sort_direction,omitempty"` } func (x *ListRacesRequestFilter) Reset() { @@ -174,6 +227,13 @@ func (x *ListRacesRequestFilter) GetVisibleOnly() bool { return false } +func (x *ListRacesRequestFilter) GetSortDirection() SortDirection { + if x != nil && x.SortDirection != nil { + return *x.SortDirection + } + return SortDirection_SORT_DIRECTION_UNSPECIFIED +} + // A race resource. type Race struct { state protoimpl.MessageState @@ -283,33 +343,44 @@ var file_racing_racing_proto_rawDesc = []byte{ 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x72, 0x61, 0x63, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x52, 0x61, 0x63, 0x65, 0x52, 0x05, 0x72, 0x61, - 0x63, 0x65, 0x73, 0x22, 0x72, 0x0a, 0x16, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x1f, 0x0a, - 0x0b, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, - 0x28, 0x03, 0x52, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x73, 0x12, 0x26, - 0x0a, 0x0c, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x08, 0x48, 0x00, 0x52, 0x0b, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x4f, - 0x6e, 0x6c, 0x79, 0x88, 0x01, 0x01, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x76, 0x69, 0x73, 0x69, 0x62, - 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x22, 0xcb, 0x01, 0x0a, 0x04, 0x52, 0x61, 0x63, 0x65, - 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, - 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x12, - 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x04, 0x20, - 0x01, 0x28, 0x03, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x76, - 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x76, 0x69, - 0x73, 0x69, 0x62, 0x6c, 0x65, 0x12, 0x4e, 0x0a, 0x15, 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, - 0x73, 0x65, 0x64, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 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, 0x13, 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, 0x73, 0x65, 0x64, 0x53, 0x74, 0x61, 0x72, - 0x74, 0x54, 0x69, 0x6d, 0x65, 0x32, 0x4c, 0x0a, 0x06, 0x52, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x12, - 0x42, 0x0a, 0x09, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x12, 0x18, 0x2e, 0x72, - 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, - 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2f, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x63, 0x65, 0x73, 0x22, 0xc8, 0x01, 0x0a, 0x16, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, + 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x1f, + 0x0a, 0x0b, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x03, 0x52, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x73, 0x12, + 0x26, 0x0a, 0x0c, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x08, 0x48, 0x00, 0x52, 0x0b, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, + 0x4f, 0x6e, 0x6c, 0x79, 0x88, 0x01, 0x01, 0x12, 0x41, 0x0a, 0x0e, 0x73, 0x6f, 0x72, 0x74, 0x5f, + 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, + 0x15, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x44, 0x69, 0x72, + 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x48, 0x01, 0x52, 0x0d, 0x73, 0x6f, 0x72, 0x74, 0x44, 0x69, + 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x88, 0x01, 0x01, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x76, + 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x5f, 0x6f, 0x6e, 0x6c, 0x79, 0x42, 0x11, 0x0a, 0x0f, 0x5f, + 0x73, 0x6f, 0x72, 0x74, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xcb, + 0x01, 0x0a, 0x04, 0x52, 0x61, 0x63, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x65, 0x65, 0x74, 0x69, + 0x6e, 0x67, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6d, 0x65, 0x65, + 0x74, 0x69, 0x6e, 0x67, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, + 0x6d, 0x62, 0x65, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, + 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x18, 0x05, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x12, 0x4e, 0x0a, 0x15, + 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, 0x73, 0x65, 0x64, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, + 0x5f, 0x74, 0x69, 0x6d, 0x65, 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, 0x13, 0x61, 0x64, 0x76, 0x65, 0x72, 0x74, 0x69, + 0x73, 0x65, 0x64, 0x53, 0x74, 0x61, 0x72, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x2a, 0x60, 0x0a, 0x0d, + 0x53, 0x6f, 0x72, 0x74, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1e, 0x0a, + 0x1a, 0x53, 0x4f, 0x52, 0x54, 0x5f, 0x44, 0x49, 0x52, 0x45, 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x5f, + 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x16, 0x0a, + 0x12, 0x53, 0x4f, 0x52, 0x54, 0x5f, 0x44, 0x49, 0x52, 0x45, 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x5f, + 0x41, 0x53, 0x43, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x53, 0x4f, 0x52, 0x54, 0x5f, 0x44, 0x49, + 0x52, 0x45, 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x44, 0x45, 0x53, 0x43, 0x10, 0x02, 0x32, 0x4c, + 0x0a, 0x06, 0x52, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x12, 0x42, 0x0a, 0x09, 0x4c, 0x69, 0x73, 0x74, + 0x52, 0x61, 0x63, 0x65, 0x73, 0x12, 0x18, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x4c, + 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x19, 0x2e, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x61, 0x63, + 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, + 0x2f, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -324,25 +395,28 @@ func file_racing_racing_proto_rawDescGZIP() []byte { return file_racing_racing_proto_rawDescData } +var file_racing_racing_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_racing_racing_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_racing_racing_proto_goTypes = []interface{}{ - (*ListRacesRequest)(nil), // 0: racing.ListRacesRequest - (*ListRacesResponse)(nil), // 1: racing.ListRacesResponse - (*ListRacesRequestFilter)(nil), // 2: racing.ListRacesRequestFilter - (*Race)(nil), // 3: racing.Race - (*timestamppb.Timestamp)(nil), // 4: google.protobuf.Timestamp + (SortDirection)(0), // 0: racing.SortDirection + (*ListRacesRequest)(nil), // 1: racing.ListRacesRequest + (*ListRacesResponse)(nil), // 2: racing.ListRacesResponse + (*ListRacesRequestFilter)(nil), // 3: racing.ListRacesRequestFilter + (*Race)(nil), // 4: racing.Race + (*timestamppb.Timestamp)(nil), // 5: google.protobuf.Timestamp } var file_racing_racing_proto_depIdxs = []int32{ - 2, // 0: racing.ListRacesRequest.filter:type_name -> racing.ListRacesRequestFilter - 3, // 1: racing.ListRacesResponse.races:type_name -> racing.Race - 4, // 2: racing.Race.advertised_start_time:type_name -> google.protobuf.Timestamp - 0, // 3: racing.Racing.ListRaces:input_type -> racing.ListRacesRequest - 1, // 4: racing.Racing.ListRaces:output_type -> racing.ListRacesResponse - 4, // [4:5] is the sub-list for method output_type - 3, // [3:4] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 3, // 0: racing.ListRacesRequest.filter:type_name -> racing.ListRacesRequestFilter + 4, // 1: racing.ListRacesResponse.races:type_name -> racing.Race + 0, // 2: racing.ListRacesRequestFilter.sort_direction:type_name -> racing.SortDirection + 5, // 3: racing.Race.advertised_start_time:type_name -> google.protobuf.Timestamp + 1, // 4: racing.Racing.ListRaces:input_type -> racing.ListRacesRequest + 2, // 5: racing.Racing.ListRaces:output_type -> racing.ListRacesResponse + 5, // [5:6] is the sub-list for method output_type + 4, // [4:5] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_racing_racing_proto_init() } @@ -406,13 +480,14 @@ func file_racing_racing_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_racing_racing_proto_rawDesc, - NumEnums: 0, + NumEnums: 1, NumMessages: 4, NumExtensions: 0, NumServices: 1, }, GoTypes: file_racing_racing_proto_goTypes, DependencyIndexes: file_racing_racing_proto_depIdxs, + EnumInfos: file_racing_racing_proto_enumTypes, MessageInfos: file_racing_racing_proto_msgTypes, }.Build() File_racing_racing_proto = out.File diff --git a/racing/proto/racing/racing.proto b/racing/proto/racing/racing.proto index 791bb51..349b948 100644 --- a/racing/proto/racing/racing.proto +++ b/racing/proto/racing/racing.proto @@ -5,6 +5,13 @@ option go_package = "/racing"; import "google/protobuf/timestamp.proto"; +// SortDirection lets a caller influence ordering of list results. +enum SortDirection { + SORT_DIRECTION_UNSPECIFIED = 0; // Defaults to descending (most recent first). + SORT_DIRECTION_ASC = 1; // Ascending advertised_start_time (earliest first). + SORT_DIRECTION_DESC = 2; // Descending advertised_start_time (latest / most imminent first). +} + service Racing { // ListRaces will return a collection of all races. rpc ListRaces(ListRacesRequest) returns (ListRacesResponse) {} @@ -27,6 +34,9 @@ message ListRacesRequestFilter { // visible_only, when true, restricts results to only races marked visible. // When omitted or false, all races (visible or not) are returned. optional bool visible_only = 2; + // sort_direction chooses ascending or descending order by advertised_start_time. + // If omitted or UNSPECIFIED the results are returned descending (latest first). + optional SortDirection sort_direction = 3; } /* Resources */