Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Argument for Get Method #116

Closed
SubutaDan opened this issue Jan 3, 2021 · 7 comments
Closed

Query Argument for Get Method #116

SubutaDan opened this issue Jan 3, 2021 · 7 comments

Comments

@SubutaDan
Copy link

Hello,
I want to pass an entity to a Repository.get method so I can use a field from the entity as a query parameter. From the example program on Flutter by Example it seems that I should be able to do that, but my get method doesn't accept an instance variable:

//I can't do this:
Repository().get<MyModelType>(myModelInstance);
because I get:

Too many positional arguments: 0 expected, but 1 found.

Is passing instance variables to Repository.get methods supported? How can I pass a value to be used as a query parameter for a get?
Thanks.
-Dan

@tshedor
Copy link
Collaborator

tshedor commented Jan 3, 2021

@SubutaDan no, geting an instance is not supported because, well, you already have that instance.

If you're trying to fetch the latest data of something, you can do a query based on Brick's internal primary key:

import 'package:brick_sqlite_abstract/db.dart';

final repositoryInstances = Repository().get<MyModelType>(query: Query.where(InsertTable.PRIMARY_KEY_FIELD, myModelInstance.primaryKey, limit1: true);
final hydratedInstance = (repositoryInstances?.isNotEmpty ?? false) ? repositoryInstances.first : null;

@SubutaDan
Copy link
Author

Thanks, @tshedor , but my question is slightly different. Sorry I wasn't more clear.

Is there a way to pass a URL query string argument to a get method:

https://myapiserver.com/cats?name=${i_want-to_pass_in_this_value}

If the method is a PUT or a POST I can use an instance field in the query string but I don''t know how to pass a value for a GET as there is no instance.

-Dan

@tshedor
Copy link
Collaborator

tshedor commented Jan 4, 2021

@SubutaDan

Ah, I got you. Yes. endpoint: uses an instance and query as its parameters.

@ConnectOfflineFirstWithRest(
  restConfig: RestSerializable(
    endpoint: r'''{
      if (query?.action == QueryAction.get && query?.where != null) {
        final name = Where.firstByField('name', query.where)?.value;
        if (name != null) return "/cats?name=${name}";
      }

      return "/cats";
    }''';
  )
)
class Cat extends OfflineFirstWithRestModel {}

And then you'd invoke this:

await Repository().get<Cat>(query: Query.where('name').isExactly('Old Deuteronomy'));

Of course, it's up to you on how to handle the query operator, so isExactly, contains, notEqual will all be the same with the above implementation of endpoint:.

@SubutaDan
Copy link
Author

SubutaDan commented Jan 4, 2021 via email

@tshedor
Copy link
Collaborator

tshedor commented Jan 18, 2021

Hi @SubutaDan could you please give this mixin a try?

@SubutaDan
Copy link
Author

SubutaDan commented Jan 19, 2021 via email

@tshedor
Copy link
Collaborator

tshedor commented Jan 30, 2021

Closing this issue; confirmed working

@tshedor tshedor closed this as completed Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants