diff --git a/src/Marten/Patching/IPatchExpression.cs b/src/Marten/Patching/IPatchExpression.cs index b1096a9103..56f9dd8071 100644 --- a/src/Marten/Patching/IPatchExpression.cs +++ b/src/Marten/Patching/IPatchExpression.cs @@ -81,6 +81,15 @@ public interface IPatchExpression /// IPatchExpression Increment(Expression> expression, float increment = 1); + /// + /// Increment a single field or property by adding the increment value + /// to the persisted value + /// + /// + /// + /// + IPatchExpression Increment(Expression> expression, decimal increment = 1); + /// /// Append an element to the end of a child collection on the persisted /// document diff --git a/src/Marten/Patching/PatchExpression.cs b/src/Marten/Patching/PatchExpression.cs index 49d91298ed..c0fae6e92c 100644 --- a/src/Marten/Patching/PatchExpression.cs +++ b/src/Marten/Patching/PatchExpression.cs @@ -122,6 +122,17 @@ public IPatchExpression Increment(Expression> expression, floa _patchSet.Add(new PatchData(Items: patch, false)); return this; } + + public IPatchExpression Increment(Expression> expression, decimal increment = 1) + { + var patch = new Dictionary(); + patch.Add("type", "increment_float"); + patch.Add("increment", increment); + patch.Add("path", toPath(expression)); + _patchSet.Add(new PatchData(Items: patch, false)); + return this; + } + //TODO NRT - Annotations are currently inaccurate here due to lack of null guards. Replace with guards in .NET 6+ public IPatchExpression Append(Expression>> expression, TElement element) { diff --git a/src/PatchingTests/Patching/PatchExpressionTests.cs b/src/PatchingTests/Patching/PatchExpressionTests.cs index 5bacf6c414..18f4a215f4 100644 --- a/src/PatchingTests/Patching/PatchExpressionTests.cs +++ b/src/PatchingTests/Patching/PatchExpressionTests.cs @@ -224,6 +224,36 @@ public void increment_float_with_explicit_interval() _expression.Patch["increment"].ShouldBe(5); } + [Fact] + public void increment_decimal_with_default() + { + _expression.Increment(x => x.Decimal); + + _expression.Patch["path"].ShouldBe("Decimal"); + _expression.Patch["type"].ShouldBe("increment_float"); + _expression.Patch["increment"].ShouldBe(1); + } + + [Fact] + public void increment_decimal_with_default_deep() + { + _expression.Increment(x => x.Inner.Inner.Decimal); + + _expression.Patch["path"].ShouldBe("Inner.Inner.Decimal"); + _expression.Patch["type"].ShouldBe("increment_float"); + _expression.Patch["increment"].ShouldBe(1); + } + + [Fact] + public void increment_decimal_with_explicit_interval() + { + _expression.Increment(x => x.Decimal, 5); + + _expression.Patch["path"].ShouldBe("Decimal"); + _expression.Patch["type"].ShouldBe("increment_float"); + _expression.Patch["increment"].ShouldBe(5); + } + [Fact] public void append_shallow() { diff --git a/src/PatchingTests/Patching/patching_api.cs b/src/PatchingTests/Patching/patching_api.cs index d295f7a56a..99e6d3f1bd 100644 --- a/src/PatchingTests/Patching/patching_api.cs +++ b/src/PatchingTests/Patching/patching_api.cs @@ -14,13 +14,17 @@ using Weasel.Core; using Weasel.Postgresql.SqlGeneration; using Xunit; +using Xunit.Abstractions; namespace PatchingTests.Patching; public class patching_api: OneOffConfigurationsContext { - public patching_api() + private readonly ITestOutputHelper _testOutputHelper; + + public patching_api(ITestOutputHelper testOutputHelper) { + _testOutputHelper = testOutputHelper; StoreOptions(_ => { _.UseDefaultSerialization(EnumStorage.AsString); @@ -264,6 +268,23 @@ public void increment_for_float() } } + [Fact] + public void increment_for_decimal() + { + var target = Target.Random(); + target.Decimal = 11.2m; + + theSession.Store(target); + theSession.Patch(target.Id).Increment(x => x.Decimal, 2.4m); + theSession.SaveChanges(); + theSession.SaveChanges(); + + using (var query = theStore.QuerySession()) + { + query.Load(target.Id).Decimal.ShouldBe(13.6m); + } + } + [Fact] public void append_to_a_primitive_array() {