Skip to content

Commit

Permalink
Refactored TgStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianMorozov committed Apr 29, 2024
1 parent 7b92607 commit b81f7ba
Show file tree
Hide file tree
Showing 78 changed files with 578 additions and 510 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/branch_main_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ jobs:
- name: Tests for Release Any CPU
run: | #
dotnet test Tests/TgAssertCoreTests/TgAssertCoreTests.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
dotnet test Tests/TgDownloaderConsoleTest/TgDownloaderConsoleTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
dotnet test Tests/TgDownloaderTest/TgDownloaderTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
# dotnet test Tests/TgDownloaderConsoleTest/TgDownloaderConsoleTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
# dotnet test Tests/TgDownloaderTest/TgDownloaderTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
# dotnet test Tests/TgEfCoreTests/TgEfCoreTests.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
6 changes: 3 additions & 3 deletions .github/workflows/branch_preview_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
run: nuget restore TgDownloader.sln

- name: Tests for Release Any CPU
run: | #dotnet test TgDownloader.sln --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
run: | # dotnet test TgDownloader.sln --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
dotnet test Tests/TgAssertCoreTests/TgAssertCoreTests.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
dotnet test Tests/TgDownloaderConsoleTest/TgDownloaderConsoleTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
dotnet test Tests/TgDownloaderTest/TgDownloaderTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
# dotnet test Tests/TgDownloaderConsoleTest/TgDownloaderConsoleTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
# dotnet test Tests/TgDownloaderTest/TgDownloaderTest.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
# dotnet test Tests/TgEfCoreTests/TgEfCoreTests.csproj --no-restore --verbosity quiet -property:Configuration=Release -property:Platform="Any CPU"
20 changes: 11 additions & 9 deletions Clients/TgDownloaderBlazor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Add services to the container
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// Radzen.
// Radzen
// https://blazor.radzen.com/get-started
builder.Services.AddRadzenComponents();
builder.Services.AddScoped<TgJsService>();
// Inject.
// Inject
builder.Services.AddHttpClient();

// EF Core.
builder.Services
.AddDbContextFactory<TgEfContext>(opt => opt
.UseSqlite($"{TgLocaleHelper.Instance.SqliteDataSource}={TgAppSettingsHelper.Instance.AppXml.FileStorage}"));
// Register TgEfContext as the DbContext for EF Core
builder.Services.AddDbContextFactory<TgEfContext>(options => options
.UseSqlite(b => b.MigrationsAssembly(nameof(TgDownloaderBlazor))));
//builder.Services.AddDbContextFactory<TgEfContext>(opt => opt.UseSqlite($"{TgLocaleHelper.Instance.SqliteDataSource}={TgAppSettingsHelper.Instance.AppXml.FileStorage}"));
TgEfContext efContext = TgEfContext.Instance;
efContext.Database.Migrate();

var app = builder.Build();
WebApplication app = builder.Build();

// Configure the HTTP request pipeline.
// Configure the HTTP request pipeline
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
Expand Down
10 changes: 10 additions & 0 deletions Clients/TgDownloaderBlazor/TgDownloaderBlazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Radzen.Blazor" Version="4.30.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Core\TgDownloader\TgDownloader.csproj" />
<ProjectReference Include="..\..\Core\TgStorage\TgStorage.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
64 changes: 33 additions & 31 deletions Clients/TgDownloaderBlazor/Utils/TgDatabaseUtility.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
//// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
//// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgDownloaderBlazor.Utils;
//namespace TgDownloaderBlazor.Utils;

public static class TgDatabaseUtility
{
// Method to see the database. Should not be used in production: demo purposes only.
// options: The configured options.
// count: The number of contacts to seed.
public static async Task EnsureDbCreatedAndSeedWithCountOfAsync(DbContextOptions<TgEfContext> options, int count)
{
#if DEBUG
Console.WriteLine($"EnsureDbCreatedAndSeedWithCountOfAsync");
#endif
// Empty to avoid logging while inserting (otherwise will flood console).
LoggerFactory factory = new();
DbContextOptionsBuilder<TgEfContext> builder = new DbContextOptionsBuilder<TgEfContext>()
.UseLoggerFactory(factory)
.UseSqlite($"{TgLocaleHelper.Instance.SqliteDataSource}={TgAppSettingsHelper.Instance.AppXml.FileStorage}");
await using var efContext = new TgEfContext(builder.Options);
#if DEBUG
Console.WriteLine(efContext.Database.GetConnectionString());
#endif
//public static class TgDatabaseUtility
//{
// // Method to see the database. Should not be used in production: demo purposes only.
// // options: The configured options.
// // count: The number of contacts to seed.
// public static async Task EnsureDbCreatedAndSeedWithCountOfAsync(DbContextOptions<TgEfContext> options, int count)
// {
//#if DEBUG
// Console.WriteLine($"EnsureDbCreatedAndSeedWithCountOfAsync");
//#endif
// // Empty to avoid logging while inserting (otherwise will flood console).
// LoggerFactory factory = new();
// DbContextOptionsBuilder<TgEfContext> builder = new DbContextOptionsBuilder<TgEfContext>()
// .UseLoggerFactory(factory)
// .UseSqlite(b => b.MigrationsAssembly(nameof(TgDownloaderBlazor)))
// .UseSqlite($"{TgLocaleHelper.Instance.SqliteDataSource}={TgAppSettingsHelper.Instance.AppXml.FileStorage}")
// ;
// await using var efContext = new TgEfContext(builder.Options);
//#if DEBUG
// Console.WriteLine(efContext.Database.GetConnectionString());
//#endif

// Result is true if the database had to be created.
//if (await efContext.Database.EnsureCreatedAsync())
//{
// //var seed = new SeedContacts();
// //await seed.SeedDatabaseWithContactCountOfAsync(context, count);
// List<TgEfProxyEntity> proxies = efContext.Proxies.Select(x => x).Take(1).ToList();
//}
}
}
// // Result is true if the database had to be created.
// //if (await efContext.Database.EnsureCreatedAsync())
// //{
// // //var seed = new SeedContacts();
// // //await seed.SeedDatabaseWithContactCountOfAsync(context, count);
// // List<TgEfProxyEntity> proxies = efContext.Proxies.Select(x => x).Take(1).ToList();
// //}
// }
//}
4 changes: 4 additions & 0 deletions Clients/TgDownloaderConsole/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
global using System.Globalization;
global using System.Reflection;
global using System.Text;
global using Microsoft.EntityFrameworkCore;
global using TgDownloader.Helpers;
global using TgDownloader.Models;
global using TgDownloaderConsole.Helpers;
global using TgLocalization.Helpers;
global using TgStorage;
global using TgStorage.Common;
global using TgStorage.Contracts;
global using TgStorage.Domain.Apps;
global using TgStorage.Domain.Filters;
global using TgStorage.Helpers;
global using TgStorage.Domain.Proxies;
global using TgStorage.Domain.Sources;
global using TgStorage.Domain.Versions;
global using TgStorage.Enums;
global using TgStorage.Models;
global using TgStorage.Utils;
20 changes: 2 additions & 18 deletions Clients/TgDownloaderConsole/Helpers/TgMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,21 @@
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
// ReSharper disable InconsistentNaming

using TgStorage.Contracts;

namespace TgDownloaderConsole.Helpers;

[DebuggerDisplay("{ToDebugString()}")]
internal sealed partial class TgMenuHelper : ITgHelper
internal sealed partial class TgMenuHelper() : ITgHelper
{
#region Design pattern "Lazy Singleton"

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private static TgMenuHelper _instance;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public static TgMenuHelper Instance => LazyInitializer.EnsureInitialized(ref _instance);

#endregion

#region Public and internal fields, properties, constructor

internal TgAppSettingsHelper TgAppSettings => TgAppSettingsHelper.Instance;
internal TgLocaleHelper TgLocale => TgLocaleHelper.Instance;
internal TgLogHelper TgLog => TgLogHelper.Instance;
internal TgClientHelper TgClient => TgClientHelper.Instance;
internal Style StyleMain => new(Color.White, null, Decoration.Bold | Decoration.Conceal | Decoration.Italic);
internal TgEfContext EfContext { get; } = default!;
internal TgEfContext EfContext => TgEfContext.Instance;
internal TgEnumMenuMain Value { get; set; }

public TgMenuHelper()
{
EfContext = TgStorageUtils.GetEfContextProd();
}

#endregion

#region Public and internal methods
Expand Down
8 changes: 4 additions & 4 deletions Clients/TgDownloaderConsole/Helpers/TgMenuHelperAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public bool CheckTgSettingsWithWarning(TgDownloadSettingsModel tgDownloadSetting
};


public void RunActionProgress(TgDownloadSettingsModel tgDownloadSettings, Func<TgDownloadSettingsModel, Task> action,
public void RunActionProgress(TgDownloadSettingsModel tgDownloadSettings, Action<TgDownloadSettingsModel> action,
bool isSkipCheckTgSettings, bool isScanCount)
{
if (!isSkipCheckTgSettings && !CheckTgSettingsWithWarning(tgDownloadSettings))
Expand Down Expand Up @@ -123,7 +123,7 @@ async Task UpdateStateFileAsync(long sourceId, int messageId, string fileName, l
TgClient.SetupUpdateStateSource(UpdateStateSourceAsync);
TgClient.SetupUpdateStateFile(UpdateStateFileAsync);
// Action.
await action(tgDownloadSettings);
action(tgDownloadSettings);
sw.Stop();
progressTaskFile.StopTask();
progressTaskSource.StopTask();
Expand All @@ -144,7 +144,7 @@ async Task UpdateStateFileAsync(long sourceId, int messageId, string fileName, l
}
}

public void RunActionStatus(TgDownloadSettingsModel tgDownloadSettings, Func<TgDownloadSettingsModel, Task> action,
public void RunActionStatus(TgDownloadSettingsModel tgDownloadSettings, Action<TgDownloadSettingsModel> action,
bool isSkipCheckTgSettings, bool isScanCount)
{
if (!isSkipCheckTgSettings && !CheckTgSettingsWithWarning(tgDownloadSettings))
Expand Down Expand Up @@ -227,7 +227,7 @@ async Task UpdateStateFileAsync(long sourceId, int messageId, string fileName, l
TgClient.SetupUpdateStateMessage(UpdateStateMessageAsync);
// Action.
Stopwatch sw = Stopwatch.StartNew();
action(tgDownloadSettings).GetAwaiter().GetResult();
action(tgDownloadSettings);
sw.Stop();
// Update state source.
UpdateStateSourceAsync(0, 0,
Expand Down
33 changes: 17 additions & 16 deletions Clients/TgDownloaderConsole/Helpers/TgMenuHelperAdvanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void SetupAdvanced(TgDownloadSettingsModel tgDownloadSettings)
switch (menu)
{
case TgEnumMenuDownload.AutoDownload:
RunActionStatus(tgDownloadSettings, AutoDownloadAsync, true, false);
RunActionStatus(tgDownloadSettings, AutoDownload, true, false);
break;
case TgEnumMenuDownload.AutoViewEvents:
RunActionStatus(tgDownloadSettings, AutoViewEvents, true, false);
Expand Down Expand Up @@ -89,19 +89,19 @@ private void ScanSources(TgDownloadSettingsModel tgDownloadSettings, TgEnumSourc
switch (sourceType)
{
case TgEnumSourceType.Chat:
RunActionStatus(tgDownloadSettings, ScanSourcesChatsWithSaveAsync, true, true);
RunActionStatus(tgDownloadSettings, ScanSourcesChatsWithSave, true, true);
break;
case TgEnumSourceType.Dialog:
RunActionStatus(tgDownloadSettings, ScanSourcesDialogsWithSaveAsync, true, true);
RunActionStatus(tgDownloadSettings, ScanSourcesDialogsWithSave, true, true);
break;
}
}

private async Task ScanSourcesChatsWithSaveAsync(TgDownloadSettingsModel tgDownloadSettings) =>
await TgClient.ScanSourcesTgConsoleAsync(tgDownloadSettings, TgEnumSourceType.Chat);
private void ScanSourcesChatsWithSave(TgDownloadSettingsModel tgDownloadSettings) =>
TgClient.ScanSourcesTgConsoleAsync(tgDownloadSettings, TgEnumSourceType.Chat).GetAwaiter().GetResult();

private async Task ScanSourcesDialogsWithSaveAsync(TgDownloadSettingsModel tgDownloadSettings) =>
await TgClient.ScanSourcesTgConsoleAsync(tgDownloadSettings, TgEnumSourceType.Dialog);
private void ScanSourcesDialogsWithSave(TgDownloadSettingsModel tgDownloadSettings) =>
TgClient.ScanSourcesTgConsoleAsync(tgDownloadSettings, TgEnumSourceType.Dialog).GetAwaiter().GetResult();

private void ViewSources(TgDownloadSettingsModel tgDownloadSettings)
{
Expand All @@ -125,32 +125,33 @@ private void ViewVersions(TgDownloadSettingsModel tgDownloadSettings)

private void MarkHistoryRead(TgDownloadSettingsModel tgDownloadSettings)
{
RunActionStatus(tgDownloadSettings, MarkHistoryReadAsync, true, false);
RunActionStatus(tgDownloadSettings, MarkHistoryReadCore, true, false);
}

private async Task AutoDownloadAsync(TgDownloadSettingsModel _)
private void AutoDownload(TgDownloadSettingsModel _)
{
IEnumerable<TgEfSourceEntity> sources =
(await EfContext.SourceRepository.GetEnumerableAsync(TgEnumTableTopRecords.All, isNoTracking: true)).Items;
IEnumerable<TgEfSourceEntity> sources = EfContext.SourceRepository.GetEnumerable(TgEnumTableTopRecords.All, isNoTracking: true).Items;
foreach (TgEfSourceEntity source in sources.Where(sourceSetting => sourceSetting.IsAutoUpdate))
{
TgDownloadSettingsModel tgDownloadSettings = SetupDownloadSource(source.Id);
string sourceId = string.IsNullOrEmpty(source.UserName) ? $"{source.Id}" : $"{source.Id} | @{source.UserName}";
// StatusContext.
await TgClient.UpdateStateSourceAsync(source.Id, source.FirstId,
TgClient.UpdateStateSourceAsync(source.Id, source.FirstId,
source.Count <= 0
? $"The source {sourceId} hasn't any messages!"
: $"The source {sourceId} has {source.Count} messages.");
: $"The source {sourceId} has {source.Count} messages.")
.GetAwaiter().GetResult();
// ManualDownload.
if (source.Count > 0)
await ManualDownloadAsync(tgDownloadSettings);
ManualDownload(tgDownloadSettings);
}
}

private async Task AutoViewEvents(TgDownloadSettingsModel tgDownloadSettings)
private void AutoViewEvents(TgDownloadSettingsModel tgDownloadSettings)
{
TgClient.IsUpdateStatus = true;
await TgClient.UpdateStateSourceAsync(tgDownloadSettings.SourceVm.SourceId, tgDownloadSettings.SourceVm.SourceFirstId, "Auto view updates is started");
TgClient.UpdateStateSourceAsync(tgDownloadSettings.SourceVm.SourceId, tgDownloadSettings.SourceVm.SourceFirstId,
"Auto view updates is started").GetAwaiter().GetResult();
TgLog.MarkupLine(TgLocale.TypeAnyKeyForReturn);
Console.ReadKey();
TgClient.IsUpdateStatus = false;
Expand Down
Loading

0 comments on commit b81f7ba

Please sign in to comment.