mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
Fix sorting on multiple ticket fields (#412)
This commit is contained in:
@@ -571,7 +571,7 @@ func (db *Database) TicketCount(ctx context.Context, typequery, filterquery stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sortQuery(paramsSort []string, paramsDesc []bool, bindVars map[string]any) string {
|
func sortQuery(paramsSort []string, paramsDesc []bool, bindVars map[string]any) string {
|
||||||
sort := ""
|
sortQuery := ""
|
||||||
if len(paramsSort) > 0 {
|
if len(paramsSort) > 0 {
|
||||||
var sorts []string
|
var sorts []string
|
||||||
for i, column := range paramsSort {
|
for i, column := range paramsSort {
|
||||||
@@ -582,10 +582,10 @@ func sortQuery(paramsSort []string, paramsDesc []bool, bindVars map[string]any)
|
|||||||
sorts = append(sorts, colsort)
|
sorts = append(sorts, colsort)
|
||||||
bindVars[fmt.Sprintf("column%d", i)] = column
|
bindVars[fmt.Sprintf("column%d", i)] = column
|
||||||
}
|
}
|
||||||
sort = "SORT " + strings.Join(sorts, ", ")
|
sortQuery = "SORT " + strings.Join(sorts, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
return sort
|
return sortQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeMaps(a map[string]any, b map[string]any) map[string]any {
|
func mergeMaps(a map[string]any, b map[string]any) map[string]any {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
@@ -58,30 +59,12 @@ func parseQueryBool(r *http.Request, s string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseQueryStringArray(r *http.Request, key string) ([]string, error) {
|
func parseQueryStringArray(r *http.Request, key string) ([]string, error) {
|
||||||
stringArray, ok := r.URL.Query()[key]
|
return parseQueryArray(r, key), nil
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return removeEmpty(stringArray), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeEmpty(l []string) []string {
|
|
||||||
var stringArray []string
|
|
||||||
for _, s := range l {
|
|
||||||
if s == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
stringArray = append(stringArray, s)
|
|
||||||
}
|
|
||||||
|
|
||||||
return stringArray
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseQueryBoolArray(r *http.Request, key string) ([]bool, error) {
|
func parseQueryBoolArray(r *http.Request, key string) ([]bool, error) {
|
||||||
stringArray, ok := r.URL.Query()[key]
|
stringArray := parseQueryArray(r, key)
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
var boolArray []bool
|
var boolArray []bool
|
||||||
for _, s := range stringArray {
|
for _, s := range stringArray {
|
||||||
if s == "" {
|
if s == "" {
|
||||||
@@ -97,6 +80,33 @@ func parseQueryBoolArray(r *http.Request, key string) ([]bool, error) {
|
|||||||
return boolArray, nil
|
return boolArray, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseQueryArray(r *http.Request, key string) []string {
|
||||||
|
stringArray, ok := r.URL.Query()[key]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(stringArray) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
stringArray = strings.Split(stringArray[0], ",")
|
||||||
|
|
||||||
|
return removeEmpty(stringArray)
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeEmpty(l []string) []string {
|
||||||
|
var stringArray []string
|
||||||
|
for _, s := range l {
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
stringArray = append(stringArray, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringArray
|
||||||
|
}
|
||||||
|
|
||||||
func parseQueryOptionalInt(r *http.Request, key string) (*int, error) {
|
func parseQueryOptionalInt(r *http.Request, key string) (*int, error) {
|
||||||
s := r.URL.Query().Get(key)
|
s := r.URL.Query().Get(key)
|
||||||
if s == "" {
|
if s == "" {
|
||||||
|
|||||||
86
generated/api/api_test.go
Normal file
86
generated/api/api_test.go
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_parseQueryOptionalBoolArray(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
r *http.Request
|
||||||
|
key string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []bool
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "bool array",
|
||||||
|
args: args{
|
||||||
|
r: httptest.NewRequest(
|
||||||
|
http.MethodGet,
|
||||||
|
"https://try.catalyst-soar.com/api/tickets?type=alert&offset=0&count=10&sort=status%2Cowner%2Ccreated&desc=true%2Cfalse%2Cfalse&query=status+%3D%3D+%27open%27+AND+%28owner+%3D%3D+%27eve%27+OR+%21owner%29",
|
||||||
|
nil,
|
||||||
|
),
|
||||||
|
key: "desc",
|
||||||
|
},
|
||||||
|
want: []bool{true, false, false},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := parseQueryOptionalBoolArray(tt.args.r, tt.args.key)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("parseQueryOptionalBoolArray() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("parseQueryOptionalBoolArray() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_parseQueryOptionalStringArray(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
r *http.Request
|
||||||
|
key string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "string array",
|
||||||
|
args: args{
|
||||||
|
r: httptest.NewRequest(
|
||||||
|
http.MethodGet,
|
||||||
|
"https://try.catalyst-soar.com/api/tickets?type=alert&offset=0&count=10&sort=status%2Cowner%2Ccreated&desc=true%2Cfalse%2Cfalse&query=status+%3D%3D+%27open%27+AND+%28owner+%3D%3D+%27eve%27+OR+%21owner%29",
|
||||||
|
nil,
|
||||||
|
),
|
||||||
|
key: "sort",
|
||||||
|
},
|
||||||
|
want: []string{"status", "owner", "created"},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := parseQueryOptionalStringArray(tt.args.r, tt.args.key)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("parseQueryOptionalStringArray() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("parseQueryOptionalStringArray() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,27 +38,27 @@ func ticketIDs(ticketResponses []*model.TicketResponse) []driver.DocumentID {
|
|||||||
return ids
|
return ids
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) ListTickets(ctx context.Context, s3 *string, i *int, i2 *int, strings []string, bools []bool, s2 *string) (*model.TicketList, error) {
|
func (s *Service) ListTickets(ctx context.Context, ticketType *string, offsetP, countP *int, sort []string, descending []bool, queryP *string) (*model.TicketList, error) {
|
||||||
q := ""
|
q := ""
|
||||||
if s2 != nil && *s2 != "" {
|
if queryP != nil && *queryP != "" {
|
||||||
q = *s2
|
q = *queryP
|
||||||
}
|
}
|
||||||
t := ""
|
t := ""
|
||||||
if s3 != nil && *s3 != "" {
|
if ticketType != nil && *ticketType != "" {
|
||||||
t = *s3
|
t = *ticketType
|
||||||
}
|
}
|
||||||
|
|
||||||
offset := int64(0)
|
offset := int64(0)
|
||||||
if i != nil {
|
if offsetP != nil {
|
||||||
offset = int64(*i)
|
offset = int64(*offsetP)
|
||||||
}
|
}
|
||||||
|
|
||||||
count := int64(25)
|
count := int64(25)
|
||||||
if i2 != nil {
|
if countP != nil {
|
||||||
count = int64(*i2)
|
count = int64(*countP)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.database.TicketList(ctx, t, q, strings, bools, offset, count)
|
return s.database.TicketList(ctx, t, q, sort, descending, offset, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) CreateTicket(ctx context.Context, form *model.TicketForm) (doc *model.TicketResponse, err error) {
|
func (s *Service) CreateTicket(ctx context.Context, form *model.TicketForm) (doc *model.TicketResponse, err error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user