Skip to content
This repository was 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

Description

@bahramee

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);

Activity

  1. changed the title [-]sort data & join object[/-] [+]join user's cards and sort users, by card's count.[/+] on Jan 16, 2021
  2. Reductions commented on Jan 16, 2021

    @Reductions
    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);
  3. bahramee commented on Jan 16, 2021

    @bahramee
    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 ?

  4. Reductions commented on Jan 16, 2021

    @Reductions
    Contributor

    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();
  5. Reductions commented on Jan 16, 2021

    @Reductions
    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions