I am trying to implement multiGet operation in Spring on Redis, it throws me an error - redis

I am trying to execute multiGet function in Spring on Redis. It throws me an error. I have implemented get function successfully but while implementing multiGet it asks me for a Collection as second parameter. I am not sure what to enter? Can someone please guide me here.
Here is my code for multiGet()
Method definition:
#Override
public User findById_MultiGet(String id) {
return (User)hashOperations.multiGet("USER", id);
}
Code In Controller :
#GetMapping("Map/MultiGet/{id}")
public User allMultiGet(#PathVariable("id") final String id) {
// MultiGet function
return userRepository.findById_MultiGet(id);
}
Error for above multiget method is multiget(Object, Collection) type not (String,String) type
Below code for Get function is working.
public User findById(String id) {
return (User)hashOperations.get("USER", id);
}
Code In Controller for Get function :
#GetMapping("Map/Get/{id}")
public User allGet(#PathVariable("id") final String id) {
// Get function
return userRepository.findById(id);
}

For multiGet the second parameter should be a Collection like a List (in case you want the values of the list returned as result on the same positions as their belonging keys in the input list) or a Set.
In your example this would be something like this:
List<Object> values = hashOperations.multiGet("USER", Arrays.asList("id", "name"));
Object id = values.get(0);
Object name = values.get(1);

Related

Why documentt.data.getValue() gives empty string? [duplicate]

A custom object that takes a parameter of (DocumentSnapShot documentsnapShot). also is an inner object from Firebase that retrieves a snapshot and set the values to my custom model also have its argument (DocumentSnapShot documentsnapShot). However, I wish to get the data from Firebase and pass it to my custom argument because mine takes multiple data not only Firebase. And it's not possible to iterate Firestore without an override.
Here's the code:
public UserSettings getUserSettings(DocumentSnapshot documentSnapshot){
Log.d(TAG, "getUserSettings: retrieving user account settings from firestore");
DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
settings.setDisplay_name(documentSnapshot.getString("display_name"));
settings.setUsername(documentSnapshot.getString("username"));
settings.setWebsite(documentSnapshot.getString("website"));
settings.setProfile_photo(documentSnapshot.getString("profile_photo"));
settings.setPosts(documentSnapshot.getLong("posts"));
settings.setFollowers(documentSnapshot.getLong("followers"));
settings.setFollowing(documentSnapshot.getLong("following"));
}
});
}
You cannot return something now that hasn't been loaded yet. Firestore loads data asynchronously, since it may take some time for this. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before that data is available. If you want to pass settings object to another method, just call that method inside onSuccess() method and pass that object as an argument. So a quick fix would be this:
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
yourMethod(settings);
}
One more thing to mention is that you don't need to set the those values to object that already have them. You are already getting the data from the database as an object.
So remember, onSuccess() method has an asynchronous behaviour, which means that is called even before you are getting the data from your database. If you want to use the settings object outside that method, you need to create your own callback. To achieve this, first you need to create an interface like this:
public interface MyCallback {
void onCallback(UserAccountSettings settings);
}
Then you need to create a method that is actually getting the data from the database. This method should look like this:
public void readData(MyCallback myCallback) {
DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
myCallback.onCallback(settings);
}
});
}
In the end just simply call readData() method and pass an instance of the MyCallback interface as an argument wherever you need it like this:
readData(new MyCallback() {
#Override
public void onCallback(UserAccountSettings settings) {
Log.d("TAG", settings.getDisplay_name());
}
});
This is the only way in which you can use that object of UserAccountSettings class outside onSuccess() method. For more informations, you can take also a look at this video.
Use LiveData as return type and observe the changes of it's value to execute desired operation.
private MutableLiveData<UserAccountSettings> userSettingsMutableLiveData = new MutableLiveData<>();
public MutableLiveData<UserAccountSettings> getUserSettings(DocumentSnapshot documentSnapshot){
DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
settings.setDisplay_name(documentSnapshot.getString("display_name"));
settings.setUsername(documentSnapshot.getString("username"));
settings.setWebsite(documentSnapshot.getString("website"));
settings.setProfile_photo(documentSnapshot.getString("profile_photo"));
settings.setPosts(documentSnapshot.getLong("posts"));
settings.setFollowers(documentSnapshot.getLong("followers"));
settings.setFollowing(documentSnapshot.getLong("following"));
userSettingsMutableLiveData.setValue(settings);
}
});
return userSettingsMutableLiveData;
}
Then from your Activity/Fragment observe the LiveData and inside onChanged do your desired operation.
getUserSettings().observe(this, new Observer<UserAccountSettings>() {
#Override
public void onChanged(UserAccountSettings userAccountSettings) {
//here, do whatever you want on `userAccountSettings`
}
});

Can I change my response data in OutputFormatter in ASP.NET Core 3.1

I'm trying to create a simple feature to make the first action act like the second one.
public IActionResult GetMessage()
{
return "message";
}
public IActionResult GetMessageDataModel()
{
return new MessageDataModel("message");
}
First idea came to my mind was to extend SystemTextJsonOutputFormater, and wrap context.Object with my data model in WriteResponseBodyAsync, but the action is marked sealed.
Then I tried to override WriteAsync but context.Object doesn't have protected setter, either.
Is there anyway I can achieve this by manipulating OutputFormatter?
Or I have another option instead of a custom OutputFormatter?
for some reason they prefer every response in a same format like {"return":"some message I write.","code":1}, hence I want this feature to achieve this instead of creating MessageDataModel every time.
Based on your description and requirement, it seems that you'd like to generate unified-format data globally instead of achieving it in each action's code logic. To achieve it, you can try to implement it in action filter, like below.
public class MyCustomFilter : Attribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
// implement code logic here
// based on your actual scenario
// get original message
// generate new instance of MessageDataModel
//example:
var mes = context.Result as JsonResult;
var model = new MessageDataModel
{
Code = 1,
Return = mes.Value.ToString()
};
context.Result = new JsonResult(model);
}
Apply it on specific action(s)
[MyCustomFilter]
public IActionResult GetMessage()
{
return Json("message");
}

How to pass parameter to previous page using INavigationAware Back button

I am having problem using INavigationAware codes. I have 3 pages. For example I named it pageA, pageB and pageC. PageA is a list view and I will pass the parameter to pageB using OnItemSelected
PageA View Model
public void OnItemSelected(Complaint item)
{
if (item != null)
{
var param = new NavigationParameters();
param.Add("id", item.Id);
mNavigationService.NavigateAsync("pageB", param);
}
}
In pageB, i will get the parameter using OnNavigatedTo.
PageB View Model
public async void OnNavigatedTo(NavigationParameters parameters)
{
var id = parameters["id"];
Title = string.Format("{0}: {1}", Strings.ComplaintDetail_Title, id);
await getComplaintDetail(Convert.ToInt32(id));
}
From pageB, I will send the parameter to pageC using the same way. But right now, I am having problem with passing the parameter back to pageB. Since I am using INavigation Back button on the left top, I don't know how to pass the parameter back to pageB. The issue is I need to pass the parameter (primary key) to all pages for select and update purposes. Please help me. I'm not sure how to pass the parameter using OnNavigatedFrom.
PageC View Model
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
Thank you in advance.
I hate to state the obvious, but have you tried adding to the parameters collection in the OnNavigatedFrom method?
public void OnNavigatedFrom(NavigationParameters parameters)
{
parameters.Add("test", "testValue");
}
I Use Xamarin.Essentials SecureStorage to achieve this result, Just insert your value into SecureStorage with a key then get it again when needed :
Save your parameter when you navigate to PageB :
SecureStorage.SetAsync("current_id", YourParameterValue);
When back to BageB in your OnAppearing event (override the event) get value from SecureStorage :
protected override void OnAppearing()
{
base.OnAppearing();
var CurrentId = long.Parse( SecureStorage.GetAsync("current_id").Result);
this.BindingContext = new PageBViewModel(CurrentId) ;
}
Assuming that your Id is a long.
Note : i am not sure this is the clean way to do it but it works.

TurnBasedMatc get a match via an Id

I'm wondering if its possible to get a TurnBasedMatch if you pass a the TurnBasedMatch Id to a method. I can't find any solutions in the api.
Define this class.
class LoadTurnBasedMatchCallback implements ResultCallback<LoadMatchResult> {
#Override
public void onResult(LoadMatchResult res) {
TurnBasedMatch myMatch = res.getMatch();
}
}
Now load your match as follows, and when the match is loaded you will receive a call to onResult above in which you can obtain your TurnBasedMatch from the ID you provided.
Games.TurnBasedMultiplayer.loadMatch(yourApiClient, yourTurnBasedMatchId).setResultCallback(new LoadTurnBasedMatchCallback());

Nunit Assertion for an empty intersection between collection

I've looked all around, and can't quite figure this one out, and my multitude of trial and error attempts have all been useless.
I have a list of user names (we'll call 'original list') one object is returning
I have a list of user names (we'll call 'filtration list') another object is returning
I am testing a method that returns all of the items from the original list not in the filtration list.
Ideally what I want is something like
Assert.That(returnedList, Has.No.Members.In(filtrationList))
So far the only thing I can do is iterate over the filtrationList and do
Assert.That(returnedList, Has.None.EqualTo(filteredUser))
With nunit you can create any custom constraint.
If you want to verify two collections for intersection, you can create something like this:
public class Intersects : CollectionConstraint
{
private IEnumerable _collection2;
public Intersects(IEnumerable collection2)
: base(collection2)
{
_collection2 = collection2;
}
public static Intersects With(IEnumerable arg)
{
return new Intersects(arg);
}
protected override bool doMatch(IEnumerable collection)
{
foreach (object value in collection)
{
foreach (object value2 in _collection2)
if (value.Equals(value2))
return true;
}
return false;
}
public override void WriteDescriptionTo(MessageWriter writer)
{
//You can put here something more meaningful like items which should not be in verified collection.
writer.Write("intersecting collections");
}
}
usage is pretty simple:
string[] returnedList = new string[] { "Martin", "Kent", "Jack"};
List<string> filteredUsers = new List<string>();
filteredUsers.Add("Jack");
filteredUsers.Add("Bob");
Assert.That(returnedList, Intersects.With(filteredUsers));