Skip to content

Commit 74a7094

Browse files
committed
fix: resolve PR issues
1 parent 2270203 commit 74a7094

8 files changed

Lines changed: 80 additions & 45 deletions

File tree

src/Secure.SecurityDatabaseSync.BLL/Tasks/BulkSyncTask.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,14 @@ IEnumerable<Common> GetModelsToUpdate()
121121

122122
var isUpdated = false;
123123

124-
if (targetModel.Name != sourceModel.Name)
124+
if (sourceModel is not null)
125125
{
126-
isUpdated = true;
127-
targetModel.Name = sourceModel.Name;
128-
targetModel.Updated = DateTime.Now;
126+
if (targetModel.Name != sourceModel.Name)
127+
{
128+
isUpdated = true;
129+
targetModel.Name = sourceModel.Name;
130+
targetModel.Updated = DateTime.Now;
131+
}
129132
}
130133

131134
if (isUpdated)

src/Secure.SecurityDatabaseSync.BLL/Tasks/DefaultSyncTask.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,11 @@ private async Task UpdateAsync()
132132
foreach (var model in models)
133133
{
134134
var common = _targetModels
135-
.FirstOrDefault(targetModel =>
135+
.First(targetModel =>
136136
targetModel.InternalNumber == model.InternalNumber);
137137

138-
if (common is not null)
139-
{
140-
common.Name = model.Name;
141-
common.Updated = DateTime.Now;
142-
}
138+
common.Name = model.Name;
139+
common.Updated = DateTime.Now;
143140

144141
_targetContext.Commons.Update(common);
145142
}

src/Secure.SecurityDatabaseSync.BLL/Tasks/HardBulkSyncTask.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Secure.SecurityDatabaseSync.DAL.Contexts;
55
using Secure.SecurityDatabaseSync.DAL.Models;
66
using System;
7-
using System.Collections.Generic;
87
using System.Linq;
98
using System.Threading.Tasks;
109

@@ -49,21 +48,16 @@ public async Task RunAsync()
4948

5049
await _secondAppContext.BulkDeleteAsync(secondAppContextModels);
5150

52-
IEnumerable<Common> GetModelsToAdd()
53-
{
54-
foreach (var firstModel in firstAppContextModels)
51+
var modelsToAdd = firstAppContextModels
52+
.Select(firstModel => new Common
5553
{
56-
yield return new Common
57-
{
58-
InternalNumber = firstModel.InternalNumber,
59-
Code = _code,
60-
Name = firstModel.Name,
61-
Updated = DateTime.Now,
62-
};
63-
}
64-
}
54+
InternalNumber = firstModel.InternalNumber,
55+
Code = _code,
56+
Name = firstModel.Name,
57+
Updated = DateTime.Now,
58+
})
59+
.ToList();
6560

66-
List<Common> modelsToAdd = GetModelsToAdd().ToList();
6761
await _secondAppContext.BulkInsertAsync(modelsToAdd);
6862
}
6963
}

src/Secure.SecurityDatabaseSync.DAL/Contexts/ApplicationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Secure.SecurityDatabaseSync.DAL.Contexts
1111
public class ApplicationContext : DbContext
1212
{
1313
/// <summary>
14-
/// Contructor with params.
14+
/// Constructor with params.
1515
/// </summary>
1616
/// <param name="options">Database context options.</param>
1717
public ApplicationContext(DbContextOptions<ApplicationContext> options)

src/Secure.SecurityDatabaseSync.DAL/Extensions/ApplicationContextExtension.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.Extensions.Configuration;
33
using Secure.SecurityDatabaseSync.DAL.Contexts;
4+
using System;
45

56
namespace Secure.SecurityDatabaseSync.DAL.Extensions
67
{
@@ -19,15 +20,32 @@ public static class ApplicationContextExtension
1920
public static ApplicationContext GetApplicationContext(
2021
this string databaseName,
2122
string appSettingJson,
22-
string section) =>
23-
new ApplicationContext(
24-
new DbContextOptionsBuilder<ApplicationContext>()
25-
.UseSqlServer(new ConfigurationBuilder()
26-
.AddJsonFile(appSettingJson, optional: true, reloadOnChange: true)
27-
.Build()
28-
.GetSection(section)
29-
.Value
30-
.Replace("%databaseName%", databaseName))
31-
.Options);
23+
string section)
24+
{
25+
if (string.IsNullOrEmpty(databaseName))
26+
{
27+
throw new ArgumentException($"'{nameof(databaseName)}' cannot be null or empty.", nameof(databaseName));
28+
}
29+
30+
if (string.IsNullOrEmpty(appSettingJson))
31+
{
32+
throw new ArgumentException($"'{nameof(appSettingJson)}' cannot be null or empty.", nameof(appSettingJson));
33+
}
34+
35+
if (string.IsNullOrEmpty(section))
36+
{
37+
throw new ArgumentException($"'{nameof(section)}' cannot be null or empty.", nameof(section));
38+
}
39+
40+
return new ApplicationContext(
41+
new DbContextOptionsBuilder<ApplicationContext>()
42+
.UseSqlServer(new ConfigurationBuilder()
43+
.AddJsonFile(appSettingJson, optional: true, reloadOnChange: true)
44+
.Build()
45+
.GetSection(section)
46+
.Value
47+
.Replace("%databaseName%", databaseName))
48+
.Options);
49+
}
3250
}
3351
}

src/Secure.SecurityDatabaseSync.UI/Program.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ static string UserInput(
2828
return input;
2929
}
3030

31+
async Task RunSyncServiceAsync(ISyncTask syncService)
32+
{
33+
await syncService.RunAsync();
34+
Console.WriteLine(MessageResource.CommandCompleted);
35+
}
36+
37+
ISyncTask syncService;
38+
3139
Console.WriteLine(MessageResource.CommandAvailableList);
3240
while (true)
3341
{
@@ -59,35 +67,34 @@ static string UserInput(
5967
{
6068
case "--default":
6169
{
62-
ISyncTask defaultSyncService = new DefaultSyncTask(
70+
syncService = new DefaultSyncTask(
6371
GetContext(sourceDb),
6472
GetContext(targetDb),
6573
code);
6674

67-
await defaultSyncService.RunAsync();
68-
Console.WriteLine(MessageResource.CommandCompleted);
75+
await RunSyncServiceAsync(syncService);
6976
}
7077
break;
7178

7279
case "--bulk":
7380
{
74-
ISyncTask bulkSyncService = new BulkSyncTask(
81+
syncService = new BulkSyncTask(
7582
GetContext(sourceDb),
7683
GetContext(targetDb),
7784
code);
78-
await bulkSyncService.RunAsync();
79-
Console.WriteLine(MessageResource.CommandCompleted);
85+
86+
await RunSyncServiceAsync(syncService);
8087
}
8188
break;
8289

8390
case "--hard-bulk":
8491
{
85-
ISyncTask hardBulkSyncService = new HardBulkSyncTask(
92+
syncService = new HardBulkSyncTask(
8693
GetContext(sourceDb),
8794
GetContext(targetDb),
8895
code);
89-
await hardBulkSyncService.RunAsync();
90-
Console.WriteLine(MessageResource.CommandCompleted);
96+
97+
await RunSyncServiceAsync(syncService);
9198
}
9299
break;
93100

@@ -103,9 +110,13 @@ static string UserInput(
103110
break;
104111
}
105112
}
106-
catch (Exception ex)
113+
catch (ArgumentException ex)
107114
{
108115
Console.WriteLine(MessageResource.CommandFailed);
116+
}
117+
catch (Exception ex)
118+
{
119+
Console.WriteLine(MessageResource.CommandError);
109120
Console.WriteLine(ex.StackTrace);
110121
}
111122
}

src/Secure.SecurityDatabaseSync.UI/Resources/MessageResource.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Secure.SecurityDatabaseSync.UI/Resources/MessageResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
<data name="CommandCompleted" xml:space="preserve">
124124
<value>&gt;&gt; Command completed successfully!</value>
125125
</data>
126+
<data name="CommandError" xml:space="preserve">
127+
<value>An error occurred while processing command.</value>
128+
</data>
126129
<data name="CommandFailed" xml:space="preserve">
127130
<value>&gt;&gt; Command failed!</value>
128131
</data>

0 commit comments

Comments
 (0)