step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
668 stars 168 forks source link

How can we increase or decrease an integer value by 1. #270

Closed VikashSRathore closed 1 year ago

VikashSRathore commented 2 years ago

Suppose I have a firebase node named "Likes" that contains integer values. How can I increase or decrease the value of this key by one without fetching the value of the key.

Fetching the key may create problem like :

=> user1 fetches value = 10 => user2 fetches value = 10 => user1 increases value by 1 = 11 => user2 increases value by 1 = 11

Now database will have 11 as final value but if 2 users have incremented value after 10 then it should be 12.

I hope the problem is clear.

I am using Realtime firebase database.

bezysoftware commented 2 years ago

That isn't possible with this library

bomosura commented 2 years ago

Hi, I got good news for you, it is possible with this library! Here is my sample of it

Based on their documentation: https://firebase.google.com/docs/reference/rest/database#section-server-values

public async Task Run()
{
    var firebaseClient = new FirebaseClient(
            "<Your URL here>",
            new FirebaseOptions
            {
                AuthTokenAsyncFactory = () => GetAccessToken(),
                AsAccessToken = true
            });

    var dataTest = new CountTest()
    {
        Counts = FieldValue.Increment(1)
    };

    await firebaseClient
        .Child("TestCount")
        .PatchAsync(dataTest);
}

private async Task<string> GetAccessToken() //Source: https://github.com/step-up-labs/firebase-database-dotnet/issues/221#issuecomment-704583145
{
    var jsonString = await File.ReadAllTextAsync("Creds\\firebaseCredential.json");

    var credential = GoogleCredential.FromJson(jsonString).CreateScoped(new string[] {
        "https://www.googleapis.com/auth/firebase.database",
        "https://www.googleapis.com/auth/userinfo.email"
    });

    ITokenAccess c = credential;
    return await c.GetAccessTokenForRequestAsync();
}

public class CountTest
{

    public ServerValue Counts { get; set; }

}

public class FieldValue
{
    public static ServerValue Increment(long value)
    {
        return new ServerValue(new FieldValueIncrement(value));
    }

    public class FieldValueIncrement
    {
        public FieldValueIncrement(long value)
        {
            increment = value;
        }
        public long increment { get; private set; }
    } 
}

public class ServerValue
{
    public ServerValue(object value)
    {
        Value = value;
    }

    [JsonProperty(".sv")]
    public object Value { get; set; }
}

If you want to "Decrease" the integer by 1, just use FieldValue.Increment(-1)

Let me know if this works for you

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 1 year ago

Closing the issue due to inactivity. Feel free to re-open