Skip to content

Commit

Permalink
✨feat: Added composite Replace method
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaferlez committed Mar 28, 2024
1 parent 7a0b883 commit 935d44d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
45 changes: 44 additions & 1 deletion src/Simpleverse.Repository.Db/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class Entity<TModel, TFilter, TOptions>
IQueryExistDb<TFilter>,
IQueryGetDb<TModel, TFilter, TOptions>,
IQueryListDb<TModel, TFilter, TOptions>,
IDeleteDb<TModel, TFilter, TOptions>
IDeleteDb<TModel, TFilter, TOptions>,
IReplaceDb<TModel, TFilter>
where TModel : class
where TFilter : class, IQueryFilter, new()
where TOptions : DbQueryOptions, new()
Expand Down Expand Up @@ -177,6 +178,48 @@ protected virtual SqlBuilder.Template SelectTemplate(QueryBuilder<TModel> builde

#endregion

#region Replace

public Task<(int Deleted, int Added)> Replace(
Action<TFilter> filterSetup,
IEnumerable<TModel> models
)
=> Repository.ExecuteAsyncWithTransaction((conn, tran) => Replace(conn, tran, filterSetup, models));

public virtual Task<(int Deleted, int Added)> Replace(
IDbConnection conn,
IDbTransaction tran,
Action<TFilter> filterSetup,
IEnumerable<TModel> models
)
{
return conn.ExecuteAsyncWithTransaction(
async (conn, tran) =>
{
var deleted = await DeleteAsync(
conn,
filterSetup,
transaction: tran
);
if (models == null)
return (deleted, 0);
var added = await AddAsync(
conn,
models,
outputMap: OutputMapper.Map,
transaction: tran
);
return (deleted, added);
},
transaction: tran
);
}

#endregion

#region Delete

public Task<int> DeleteAsync(Action<TFilter> filterSetup = null, Action<TOptions> optionsSetup = null)
Expand Down
20 changes: 20 additions & 0 deletions src/Simpleverse.Repository.Db/Operations/IReplaceDb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Simpleverse.Repository.Operations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace Simpleverse.Repository.Db.Operations
{
public interface IReplaceDb<TModel, TFilter> : IReplace<TModel, TFilter>
where TModel : class
where TFilter : IQueryFilter, new()
{
Task<(int Deleted, int Added)> Replace(
IDbConnection conn,
IDbTransaction tran,
Action<TFilter> filterSetup,
IEnumerable<TModel> models
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>Dapper, Bulk, Merge, Upsert, Delete, Insert, Update, Repository</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>2.0.29</Version>
<Version>2.0.30</Version>
<Description>High performance operation for MS SQL Server built for Dapper ORM. Including bulk operations Insert, Update, Delete, Get as well as Upsert both single and bulk.</Description>
<AssemblyVersion>2.0.29</AssemblyVersion>
<FileVersion>2.0.29</FileVersion>
<AssemblyVersion>2.0.30</AssemblyVersion>
<FileVersion>2.0.30</FileVersion>
<RepositoryUrl>https://github.com/lukaferlez/Simpleverse.Repository</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<EmbedAllSources>true</EmbedAllSources>
Expand Down
16 changes: 16 additions & 0 deletions src/Simpleverse.Repository/Operations/IReplace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Simpleverse.Repository.Operations
{
public interface IReplace<TModel, TFilter>
where TModel : class
where TFilter : IQueryFilter, new()
{
Task<(int Deleted, int Added)> Replace(
Action<TFilter> filterSetup,
IEnumerable<TModel> models
);
}
}
6 changes: 3 additions & 3 deletions src/Simpleverse.Repository/Simpleverse.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>Repository</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
<Description>Base repository elements.</Description>
<AssemblyVersion>1.0.6</AssemblyVersion>
<FileVersion>1.0.6</FileVersion>
<AssemblyVersion>1.0.7</AssemblyVersion>
<FileVersion>1.0.7</FileVersion>
<RepositoryUrl>https://github.com/lukaferlez/Simpleverse.Repository</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
Expand Down

0 comments on commit 935d44d

Please sign in to comment.