Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

join user's cards and sort users, by card's count. #935

Open
bahramee opened this issue Jan 16, 2021 · 4 comments
Open

join user's cards and sort users, by card's count. #935

bahramee opened this issue Jan 16, 2021 · 4 comments

Comments

@bahramee
Copy link

hello
i have a usecase with this details:
a query on "User"s wich every users have many cards wich used by user.
question is here:
how can i sort user by his card's count.

and this is my current query...

final cardQuery = Query<Company>(context)
      ..where((x) => x.id).equalTo(companyId)
      ..join(set: (u) => u.user)
          .join(set: (c) => c.card);
@bahramee bahramee changed the title sort data & join object join user's cards and sort users, by card's count. Jan 16, 2021
@Reductions
Copy link
Contributor

You can not do that using the ORM at the moment and it would be a big undertaking for it to be implemented.
Also your query will be better described like that:

final cardQuery = Query<User>(context)
      ..where((u) => u.company).identifiedBy(companyId)
      //..join(object: (u) => u.company) uncomment this row only if you want the company to be returned by the query
      ..join(set: (u) => u.card);

@bahramee
Copy link
Author

thanks for fast answer.
i ask you for another help...
this is my current sorting progress:

final groupScores =
        groupBy(companyScores.user, (obj) => obj['card'].length);

    final List<UserScore> users = [];
    UserRank userRank;
    int companyRank = 1;
    groupScores.entries.map((entry) {
      entry.value.map((e) async {
        bool isOwner;
        if (userId == null)
          isOwner = false;
        else {
          isOwner = true;
        }
        users.add(UserScore(
            userId: e.id,
            username: e.username,
            score: e.card.length,
            productId: productId,
            isOwner: isOwner,
            companyRank: companyRank,));
        companyRank++;
      }).toList();
    }).toList();

    return users;

can you help me please for optimize that ?

@Reductions
Copy link
Contributor

Reductions commented Jan 16, 2021

This is if you don't want to include the company rank:

bool isOwner = userId != null;
companyScores.user.sort((u1, u2) => u1.card.length - u2.card.length);
return companyScores.user.map((user) => UserScore(
    userId: user.id,
    username: user.username,
    score: user.card.length,
    productId: productId,
    isOwner: isOwner,
    //company rank will be the the index in the newly create array
)).toList();

@Reductions
Copy link
Contributor

This is if you want score:

bool isOwner = userId != null;
companyScores.user.sort((u1, u2) => u1.card.length - u2.card.length);
final userRank = <UserScore>[];
for(var i = 0; i < companyScores.user.length; ++i) {
  userRank.add(UserScore(
      userId: user.id,
      username: user.username,
      score: user.card.length,
      productId: productId,
      isOwner: isOwner,
      companyRank: i,
  ))
}
return userRank;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants