Skip to content

Commit

Permalink
Added database synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
carvalholuigi25 committed Feb 13, 2024
1 parent 00de84b commit 8c07883
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
72 changes: 66 additions & 6 deletions Server/Controllers/Admin/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using Dotmim.Sync.Sqlite;
using Dotmim.Sync.SqlServer;
using Dotmim.Sync.PostgreSql;
using Dotmim.Sync;
using Dotmim.Sync.MySql;
using LCPCollection.Shared.Classes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Data.SqlClient;
using NuGet.Protocol;
using Microsoft.Data.Sqlite;
using MySqlConnector;
using NuGet.Protocol;
using Npgsql;
using System.Collections;
using Newtonsoft.Json;
using Microsoft.SqlServer.Server;
using System.Diagnostics;

namespace LCPCollection.Server.Controllers
Expand Down Expand Up @@ -59,6 +60,65 @@ public IActionResult DoQryRun([FromBody] QryRunner qryrun)
}
}

/// <summary>
/// This endpoint synchronizes the specific database to the another database
/// </summary>
[HttpPost("dbsync")]
public async Task<IActionResult> DoDBSync(DBSyncRunner dbsrunner)
{
Stopwatch stopWatch = new Stopwatch();

try
{
stopWatch.Start();

StringComparison cmp = StringComparison.InvariantCultureIgnoreCase;
SqlSyncProvider serverProvider = new SqlSyncProvider(_configuration[$"ConnectionStrings:SQLServer"]);
dynamic clientProvider;

if (dbsrunner.DBMode.Contains("SQLite", cmp))
{
clientProvider = new SqliteSyncProvider(_configuration[$"ConnectionStrings:SQLite"]);
}
else if (dbsrunner.DBMode.Contains("MySQL", cmp))
{
clientProvider = new MySqlSyncProvider(_configuration[$"ConnectionStrings:MySQL"]);
}
else if (dbsrunner.DBMode.Contains("PostgreSQL", cmp))
{
clientProvider = new NpgsqlSyncProvider(_configuration[$"ConnectionStrings:PostgreSQL"]);
}
else
{
clientProvider = new SqlSyncProvider(_configuration[$"ConnectionStrings:SQLServer"]);
}

var setup = new SyncSetup("Games", "Movies", "Books", "TVSeries", "Animes", "FilesData", "Softwares", "Websites");

SyncAgent agent = new SyncAgent(clientProvider, serverProvider);

var result = await agent.SynchronizeAsync(setup);
stopWatch.Stop();

return Ok(new
{
Data = result,
DatabaseMode = dbsrunner.DBMode,
DateTimeExecuted = DateTime.UtcNow,
DateMeasureMsg = string.Format("Elapsed time {0} ms", stopWatch.ElapsedMilliseconds)
});
}
catch(Exception e)
{
return Ok(new {
Data = e.Message,
DatabaseMode = dbsrunner.DBMode,
DateTimeExecuted = DateTime.UtcNow,
DateMeasureMsg = string.Format("Elapsed time {0} ms", stopWatch.ElapsedMilliseconds)
});
}
}

private IActionResult FetchDataSQLServer(QryRunner qryrun, StringComparison cmp) {
Stopwatch stopWatch = Stopwatch.StartNew();
var res = new Dictionary<object, object?>();
Expand Down
Binary file added Server/Databases/SQLite/lcpcollection.db-shm
Binary file not shown.
Binary file added Server/Databases/SQLite/lcpcollection.db-wal
Binary file not shown.
7 changes: 7 additions & 0 deletions Server/LCPCollection.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dotmim.Sync.Core" Version="0.9.8" />
<PackageReference Include="Dotmim.Sync.MariaDB" Version="0.9.8" />
<PackageReference Include="Dotmim.Sync.MySql" Version="0.9.8" />
<PackageReference Include="Dotmim.Sync.PostgreSql" Version="0.9.8" />
<PackageReference Include="Dotmim.Sync.Sqlite" Version="0.9.8" />
<PackageReference Include="Dotmim.Sync.SqlServer" Version="0.9.8" />
<PackageReference Include="Dotmim.Sync.SqlServer.ChangeTracking" Version="0.9.8" />
<PackageReference Include="DynamicExpressions.NET" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.1" />
Expand Down
5 changes: 4 additions & 1 deletion Server/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
}
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information"
},
Expand Down
5 changes: 5 additions & 0 deletions Shared/Classes/QryRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public class QryRunner {
[Required] public string QryStr { get; set; } = null!;
}

public class DBSyncRunner
{
[Required] public string DBMode { get; set; } = QryDBModeEnum.SQLServer.ToString();
}

public enum QryDBModeEnum
{
SQLServer,
Expand Down

0 comments on commit 8c07883

Please sign in to comment.