Skip to content

Commit

Permalink
Add default sorting for decks, groups and group members (#264)
Browse files Browse the repository at this point in the history
* feat: implemented default sorting for decks, groups and group members

* added toLower operation & sorted pending members
  • Loading branch information
4KevR authored Feb 26, 2024
1 parent e8e1ec6 commit 8c5cb42
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
15 changes: 15 additions & 0 deletions backend/pkg/comparators/modelComparators.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions backend/pkg/comparators/protoComparators.go
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 6 additions & 4 deletions backend/pkg/model/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"github.com/kioku-project/kioku/pkg/helper"
"gorm.io/gorm"
"time"
)

type GroupType string
Expand All @@ -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;"`
Expand Down
10 changes: 3 additions & 7 deletions backend/services/carddeck/handler/carddeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions backend/services/collaboration/handler/collaboration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions backend/store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c5cb42

Please sign in to comment.