Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
micbar committed Feb 21, 2022
1 parent 8d36fc1 commit 08b22b1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 113 deletions.
85 changes: 18 additions & 67 deletions graph/pkg/service/v0/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func (g Graph) GetDrives(w http.ResponseWriter, r *http.Request) {
return
}

spaces = g.sortSpaces(odataReq, spaces)
spaces, err = sortSpaces(odataReq, spaces)
if err != nil {
g.logger.Error().Err(err).Msg("error sorting the spaces list")
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, err.Error())
return
}

render.Status(r, http.StatusOK)
render.JSON(w, r, &listResponse{Value: spaces})
Expand Down Expand Up @@ -714,77 +719,23 @@ func (g Graph) DeleteDrive(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}

func (g Graph) sortSpaces(req *godata.GoDataRequest, spaces []*libregraph.Drive) []*libregraph.Drive {
if req.Query.OrderBy == nil {
return spaces
func sortSpaces(req *godata.GoDataRequest, spaces []*libregraph.Drive) ([]*libregraph.Drive, error) {
var sorter sort.Interface
if req.Query.OrderBy == nil || len(req.Query.OrderBy.OrderByItems) != 1 {
return spaces, nil
}
switch req.Query.OrderBy.OrderByItems[0].Field.Value {
case "name":
spaces = sortByName(req.Query.OrderBy.OrderByItems[0].Order, spaces)
sorter = spacesByName{spaces}
case "lastModifiedDateTime":
spaces = sortByLastModifiedDateTime(req.Query.OrderBy.OrderByItems[0].Order, spaces)
}
return spaces
}

func sortByName(order string, spaces []*libregraph.Drive) []*libregraph.Drive {
if order == "desc" {
sort.Slice(spaces,
func(p, q int) bool {
return *spaces[p].Name > *spaces[q].Name
},
)
}
if order == "asc" {
sort.Slice(spaces,
func(p, q int) bool {
return *spaces[p].Name < *spaces[q].Name
},
)
sorter = spacesByLastModifiedDateTime{spaces}
default:
return nil, fmt.Errorf("we do not support %s as a order parameter", req.Query.OrderBy.OrderByItems[0].Field.Value)
}
return spaces
}

func sortByLastModifiedDateTime(order string, spaces []*libregraph.Drive) []*libregraph.Drive {
if order == "desc" {
sort.Slice(spaces,
func(p, q int) bool {
// compare the items when both dates are set
if spaces[p].LastModifiedDateTime != nil && spaces[q].LastModifiedDateTime != nil {
return spaces[p].LastModifiedDateTime.After(*spaces[q].LastModifiedDateTime)
}
// move left item down if it has no value
if spaces[p].LastModifiedDateTime == nil && spaces[q].LastModifiedDateTime != nil {
return false
}
// move right item down if it has no value
if spaces[p].LastModifiedDateTime != nil && spaces[q].LastModifiedDateTime == nil {
return true
}
// fallback to name if no dateTime is set on both items
return *spaces[p].Name > *spaces[q].Name
},
)
}
if order == "asc" {
sort.Slice(spaces,
func(p, q int) bool {
// compare the items when both dates are set
if spaces[p].LastModifiedDateTime != nil && spaces[q].LastModifiedDateTime != nil {
return spaces[p].LastModifiedDateTime.Before(*spaces[q].LastModifiedDateTime)
}
// move left item up if it has no value
if spaces[p].LastModifiedDateTime == nil && spaces[q].LastModifiedDateTime != nil {
return true
}
// move right item up if it has no value
if spaces[p].LastModifiedDateTime != nil && spaces[q].LastModifiedDateTime == nil {
return false
}
// fallback to name if no dateTime is set on both items
return *spaces[p].Name < *spaces[q].Name
},
)
if req.Query.OrderBy.OrderByItems[0].Order == "asc" {
sorter = sort.Reverse(sorter)
}
return spaces
sort.Sort(sorter)
return spaces, nil
}
89 changes: 43 additions & 46 deletions graph/pkg/service/v0/drives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ type sortTest struct {
DrivesSorted []*libregraph.Drive
}

var Time1 = time.Date(2022, 02, 02, 15, 00, 00, 00, time.UTC)
var Time2 = time.Date(2022, 02, 03, 15, 00, 00, 00, time.UTC)
var Time3, Time5, Time6 *time.Time
var Time4 = time.Date(2022, 02, 05, 15, 00, 00, 00, time.UTC)
var Drives = []*libregraph.Drive{
{Id: libregraph.PtrString("3"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Admin"), LastModifiedDateTime: Time3},
{Id: libregraph.PtrString("1"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Einstein"), LastModifiedDateTime: &Time1},
{Id: libregraph.PtrString("2"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Marie"), LastModifiedDateTime: &Time2},
{Id: libregraph.PtrString("4"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Richard"), LastModifiedDateTime: &Time4},
var time1 = time.Date(2022, 02, 02, 15, 00, 00, 00, time.UTC)
var time2 = time.Date(2022, 02, 03, 15, 00, 00, 00, time.UTC)
var time3, time5, time6 *time.Time
var time4 = time.Date(2022, 02, 05, 15, 00, 00, 00, time.UTC)
var drives = []*libregraph.Drive{
drive("3", "project", "Admin", time3),
drive("1", "project", "Einstein", &time1),
drive("2", "project", "Marie", &time2),
drive("4", "project", "Richard", &time4),
}
var DrivesLong = append(Drives, []*libregraph.Drive{
{Id: libregraph.PtrString("5"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Bob"), LastModifiedDateTime: Time5},
{Id: libregraph.PtrString("6"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Alice"), LastModifiedDateTime: Time6},
var drivesLong = append(drives, []*libregraph.Drive{
drive("5", "project", "Bob", time5),
drive("6", "project", "Alice", time6),
}...)

var sortTests = []sortTest{
{
Drives: Drives,
Drives: drives,
Query: godata.GoDataRequest{
Query: &godata.GoDataQuery{
OrderBy: &godata.GoDataOrderByQuery{
Expand All @@ -43,14 +43,14 @@ var sortTests = []sortTest{
},
},
DrivesSorted: []*libregraph.Drive{
{Id: libregraph.PtrString("3"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Admin"), LastModifiedDateTime: Time3},
{Id: libregraph.PtrString("1"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Einstein"), LastModifiedDateTime: &Time1},
{Id: libregraph.PtrString("2"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Marie"), LastModifiedDateTime: &Time2},
{Id: libregraph.PtrString("4"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Richard"), LastModifiedDateTime: &Time4},
drive("3", "project", "Admin", time3),
drive("1", "project", "Einstein", &time1),
drive("2", "project", "Marie", &time2),
drive("4", "project", "Richard", &time4),
},
},
{
Drives: Drives,
Drives: drives,
Query: godata.GoDataRequest{
Query: &godata.GoDataQuery{
OrderBy: &godata.GoDataOrderByQuery{
Expand All @@ -61,14 +61,14 @@ var sortTests = []sortTest{
},
},
DrivesSorted: []*libregraph.Drive{
{Id: libregraph.PtrString("4"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Richard"), LastModifiedDateTime: &Time4},
{Id: libregraph.PtrString("2"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Marie"), LastModifiedDateTime: &Time2},
{Id: libregraph.PtrString("1"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Einstein"), LastModifiedDateTime: &Time1},
{Id: libregraph.PtrString("3"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Admin"), LastModifiedDateTime: Time3},
drive("4", "project", "Richard", &time4),
drive("2", "project", "Marie", &time2),
drive("1", "project", "Einstein", &time1),
drive("3", "project", "Admin", time3),
},
},
{
Drives: DrivesLong,
Drives: drivesLong,
Query: godata.GoDataRequest{
Query: &godata.GoDataQuery{
OrderBy: &godata.GoDataOrderByQuery{
Expand All @@ -79,16 +79,16 @@ var sortTests = []sortTest{
},
},
DrivesSorted: []*libregraph.Drive{
{Id: libregraph.PtrString("3"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Admin"), LastModifiedDateTime: Time3},
{Id: libregraph.PtrString("6"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Alice"), LastModifiedDateTime: Time6},
{Id: libregraph.PtrString("5"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Bob"), LastModifiedDateTime: Time5},
{Id: libregraph.PtrString("1"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Einstein"), LastModifiedDateTime: &Time1},
{Id: libregraph.PtrString("2"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Marie"), LastModifiedDateTime: &Time2},
{Id: libregraph.PtrString("4"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Richard"), LastModifiedDateTime: &Time4},
drive("3", "project", "Admin", time3),
drive("6", "project", "Alice", time6),
drive("5", "project", "Bob", time5),
drive("1", "project", "Einstein", &time1),
drive("2", "project", "Marie", &time2),
drive("4", "project", "Richard", &time4),
},
},
{
Drives: DrivesLong,
Drives: drivesLong,
Query: godata.GoDataRequest{
Query: &godata.GoDataQuery{
OrderBy: &godata.GoDataOrderByQuery{
Expand All @@ -99,28 +99,25 @@ var sortTests = []sortTest{
},
},
DrivesSorted: []*libregraph.Drive{
{Id: libregraph.PtrString("4"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Richard"), LastModifiedDateTime: &Time4},
{Id: libregraph.PtrString("2"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Marie"), LastModifiedDateTime: &Time2},
{Id: libregraph.PtrString("1"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Einstein"), LastModifiedDateTime: &Time1},
{Id: libregraph.PtrString("5"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Bob"), LastModifiedDateTime: Time5},
{Id: libregraph.PtrString("6"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Alice"), LastModifiedDateTime: Time6},
{Id: libregraph.PtrString("3"), DriveType: libregraph.PtrString("project"), Name: libregraph.PtrString("Admin"), LastModifiedDateTime: Time3},
drive("4", "project", "Richard", &time4),
drive("2", "project", "Marie", &time2),
drive("1", "project", "Einstein", &time1),
drive("5", "project", "Bob", time5),
drive("6", "project", "Alice", time6),
drive("3", "project", "Admin", time3),
},
},
}

func drive(ID string, dType string, name string, lastModified *time.Time) *libregraph.Drive {
return &libregraph.Drive{Id: libregraph.PtrString(ID), DriveType: libregraph.PtrString(dType), Name: libregraph.PtrString(name), LastModifiedDateTime: lastModified}
}

// TestSort tests the available orderby queries
func TestSort(t *testing.T) {
graph := Graph{
config: nil,
mux: nil,
logger: nil,
identityBackend: nil,
gatewayClient: nil,
httpClient: nil,
spacePropertiesCache: nil,
}
for _, test := range sortTests {
sorted := graph.sortSpaces(&test.Query, test.Drives)
sorted, err := sortSpaces(&test.Query, test.Drives)
assert.NoError(t, err)
assert.Equal(t, test.DrivesSorted, sorted)
}
}
45 changes: 45 additions & 0 deletions graph/pkg/service/v0/ordering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package svc

import (
libregraph "github.com/owncloud/libre-graph-api-go"
)

type spacesSlice []*libregraph.Drive

// Len is the number of elements in the collection.
func (d spacesSlice) Len() int { return len(d) }

// Swap swaps the elements with indexes i and j.
func (d spacesSlice) Swap(i, j int) { d[i], d[j] = d[j], d[i] }

type spacesByName struct {
spacesSlice
}
type spacesByLastModifiedDateTime struct {
spacesSlice
}

// Less reports whether the element with index i
// must sort before the element with index j.
func (s spacesByName) Less(i, j int) bool {
return *s.spacesSlice[i].Name > *s.spacesSlice[j].Name
}

// Less reports whether the element with index i
// must sort before the element with index j.
func (s spacesByLastModifiedDateTime) Less(i, j int) bool {
// compare the items when both dates are set
if s.spacesSlice[i].LastModifiedDateTime != nil && s.spacesSlice[j].LastModifiedDateTime != nil {
return s.spacesSlice[i].LastModifiedDateTime.After(*s.spacesSlice[j].LastModifiedDateTime)
}
// move left item down if it has no value
if s.spacesSlice[i].LastModifiedDateTime == nil && s.spacesSlice[j].LastModifiedDateTime != nil {
return false
}
// move right item down if it has no value
if s.spacesSlice[i].LastModifiedDateTime != nil && s.spacesSlice[j].LastModifiedDateTime == nil {
return true
}
// fallback to name if no dateTime is set on both items
return *s.spacesSlice[i].Name > *s.spacesSlice[j].Name
}

0 comments on commit 08b22b1

Please sign in to comment.