diff --git a/backend/pkg/comparators/modelComparators.go b/backend/pkg/comparators/modelComparators.go new file mode 100644 index 00000000..77f407b9 --- /dev/null +++ b/backend/pkg/comparators/modelComparators.go @@ -0,0 +1,15 @@ +package comparators + +import "github.com/kioku-project/kioku/pkg/model" + +func CardModelDateComparator(a, b model.Card) int { + return a.CreatedAt.Compare(b.CreatedAt) +} + +func DeckModelDateComparator(a, b model.Deck) int { + return a.CreatedAt.Compare(b.CreatedAt) +} + +func GroupModelDateComparator(a, b model.Group) int { + return a.CreatedAt.Compare(b.CreatedAt) +} diff --git a/backend/pkg/comparators/protoComparators.go b/backend/pkg/comparators/protoComparators.go new file mode 100644 index 00000000..2dc3c04d --- /dev/null +++ b/backend/pkg/comparators/protoComparators.go @@ -0,0 +1,17 @@ +package comparators + +import ( + pbCommon "github.com/kioku-project/kioku/pkg/proto" + "strings" +) + +func GroupUserProtoRoleComparator(a, b *pbCommon.User) int { + if val := int(b.GroupRole.Number()) - int(a.GroupRole.Number()); val != 0 { + return val + } + return strings.Compare(strings.ToLower(a.UserName), strings.ToLower(b.UserName)) +} + +func UserProtoNameComparator(a, b *pbCommon.User) int { + return strings.Compare(strings.ToLower(a.UserName), strings.ToLower(b.UserName)) +} diff --git a/backend/pkg/model/group.go b/backend/pkg/model/group.go index 207ca32a..8f889262 100644 --- a/backend/pkg/model/group.go +++ b/backend/pkg/model/group.go @@ -3,6 +3,7 @@ package model import ( "github.com/kioku-project/kioku/pkg/helper" "gorm.io/gorm" + "time" ) type GroupType string @@ -14,10 +15,11 @@ const ( ) type Group struct { - ID string `gorm:"primaryKey"` - Name string `gorm:"not null"` - Description string `gorm:"not null"` - IsDefault bool `gorm:"not null"` + ID string `gorm:"primaryKey"` + Name string `gorm:"not null"` + Description string `gorm:"not null"` + IsDefault bool `gorm:"not null"` + CreatedAt time.Time GroupType GroupType `gorm:"not null"` Decks []Deck `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` GroupUserRoles []GroupUserRole `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` diff --git a/backend/services/carddeck/handler/carddeck.go b/backend/services/carddeck/handler/carddeck.go index 6066be04..fa9fadc1 100644 --- a/backend/services/carddeck/handler/carddeck.go +++ b/backend/services/carddeck/handler/carddeck.go @@ -3,7 +3,7 @@ package handler import ( "context" "errors" - + "github.com/kioku-project/kioku/pkg/comparators" pbCommon "github.com/kioku-project/kioku/pkg/proto" pbCardDeck "github.com/kioku-project/kioku/services/carddeck/proto" "gorm.io/gorm" @@ -173,10 +173,6 @@ func (e *CardDeck) updateCardReferences(ctx context.Context, cardSideToDelete *m return isLastCardSide, nil } -func cardModelDateComparator(a, b model.Card) int { - return a.CreatedAt.Compare(b.CreatedAt) -} - func (e *CardDeck) copyCards(ctx context.Context, cards []*model.Card, deckID string) error { for _, card := range cards { newCard := &model.Card{ @@ -219,7 +215,7 @@ func (e *CardDeck) GetGroupDecks(ctx context.Context, req *pbCommon.GroupRequest return err } } - + slices.SortFunc(decks, comparators.DeckModelDateComparator) rsp.Decks = converter.ConvertToTypeArray(decks, converter.StoreDeckToProtoDeckConverter) for _, deck := range rsp.Decks { deckRole, err := e.collaborationService.GetGroupUserRole(ctx, &pbCommon.GroupRequest{ @@ -393,7 +389,7 @@ func (e *CardDeck) GetDeckCards(ctx context.Context, req *pbCommon.DeckRequest, if err := e.checkUserDeckAccess(ctx, req.UserID, deck.ID); err != nil { return err } - slices.SortFunc(deck.Cards, cardModelDateComparator) + slices.SortFunc(deck.Cards, comparators.CardModelDateComparator) rsp.Cards = make([]*pbCommon.Card, len(deck.Cards)) for i, card := range deck.Cards { cardSides, err := e.store.FindCardSidesByCardID(ctx, card.ID) diff --git a/backend/services/collaboration/handler/collaboration.go b/backend/services/collaboration/handler/collaboration.go index c1ddf1db..83e89b7f 100644 --- a/backend/services/collaboration/handler/collaboration.go +++ b/backend/services/collaboration/handler/collaboration.go @@ -3,6 +3,8 @@ package handler import ( "context" "errors" + "github.com/kioku-project/kioku/pkg/comparators" + "golang.org/x/exp/slices" "github.com/kioku-project/kioku/pkg/converter" "github.com/kioku-project/kioku/pkg/helper" @@ -89,6 +91,7 @@ func (e *Collaboration) generateGroupMemberAdmissionResponse( UserEmail: user.UserEmail, } } + slices.SortFunc(memberAdmissions, comparators.UserProtoNameComparator) logger.Infof("Successfully received user information from %d users and added it to request information", len(users.Users)) return @@ -153,6 +156,7 @@ func (e *Collaboration) GetUserGroups(ctx context.Context, req *pbCommon.User, r if err != nil { return err } + slices.SortFunc(groups, comparators.GroupModelDateComparator) protoGroups := converter.ConvertToTypeArray(groups, converter.StoreGroupToProtoGroupConverter) protoRoles := make([]pbCommon.GroupRole, len(protoGroups)) for index, group := range protoGroups { @@ -307,14 +311,16 @@ func (e *Collaboration) GetGroupMembers(ctx context.Context, req *pbCommon.Group if err != nil { return err } - rsp.Users = make([]*pbCommon.User, len(users.Users)) + groupUsers := make([]*pbCommon.User, len(users.Users)) for i, user := range users.Users { - rsp.Users[i] = &pbCommon.User{ + groupUsers[i] = &pbCommon.User{ UserID: user.UserID, UserName: user.UserName, GroupRole: converter.MigrateModelRoleToProtoRole(groupMembers[i].RoleType), } } + slices.SortFunc(groupUsers, comparators.GroupUserProtoRoleComparator) + rsp.Users = groupUsers logger.Infof("Found %d users in group with id %s", len(rsp.Users), req.Group.GroupID) return nil } diff --git a/backend/store/postgres.go b/backend/store/postgres.go index f5f2012d..15cc2661 100644 --- a/backend/store/postgres.go +++ b/backend/store/postgres.go @@ -456,6 +456,7 @@ func (s *CollaborationStoreImpl) ModifyGroup(ctx context.Context, group *model.G Name: group.Name, Description: group.Description, IsDefault: group.IsDefault, + CreatedAt: group.CreatedAt, GroupType: group.GroupType, }).Error return