Skip to content

Commit

Permalink
Updated TgDownloaderConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianMorozov committed Nov 5, 2024
1 parent 9811339 commit a07ab11
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Clients/TgDownloaderConsole/Helpers/TgMenuHelperAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void RunActionProgress(TgDownloadSettingsViewModel tgDownloadSettings, Ac
.AutoClear(true)
.HideCompleted(true)
.Columns(GetProgressColumns())
.StartAsync(async context =>
.Start(context =>
{
Stopwatch sw = Stopwatch.StartNew();
Stopwatch swFileRefresh = Stopwatch.StartNew();
Expand Down Expand Up @@ -176,8 +176,8 @@ async Task UpdateStateSourceAsync(long sourceId, int messageId, string message)
if (string.IsNullOrEmpty(message))
return;
statusContext.Status(TgLog.GetMarkupString(isScanCount
? $"{GetStatus(tgDownloadSettings.SourceVm.SourceScanCount, messageId)} | {message} | " +
$"Progress {tgDownloadSettings.SourceVm.ProgressPercentString}"
//? $"{GetStatus(tgDownloadSettings.SourceVm.SourceScanCount, messageId)} | {message} | Progress {tgDownloadSettings.SourceVm.ProgressPercentString}"
? $"{GetStatus(tgDownloadSettings.SourceVm.SourceScanCount, messageId)} | {message}"
: GetFileStatus(message)));
statusContext.Refresh();
}
Expand Down
2 changes: 1 addition & 1 deletion Clients/TgDownloaderConsole/Helpers/TgMenuHelperClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private void AskClientConnect(TgDownloadSettingsViewModel tgDownloadSettings)
public void ClientConnect(TgDownloadSettingsViewModel tgDownloadSettings, bool isSilent)
{
ShowTableClient(tgDownloadSettings);
TgClient.ConnectSessionConsoleAsync(ConfigConsole, ProxyRepository.GetCurrentProxy(AppRepository.GetCurrentApp()).Item);
TgClient.ConnectSessionConsole(ConfigConsole, ProxyRepository.GetCurrentProxy(AppRepository.GetCurrentApp()).Item);
if (TgClient.ClientException.IsExist || TgClient.ProxyException.IsExist)
TgLog.MarkupInfo(TgLocale.TgClientSetupCompleteError);
else
Expand Down
13 changes: 5 additions & 8 deletions Clients/TgDownloaderConsole/Helpers/TgMenuHelperStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public void SetupStorage(TgDownloadSettingsViewModel tgDownloadSettings)

private void TgStorageBackupDb()
{
if (AskQuestionReturnNegative(TgLocale.MenuStorageDbBackup))
return;
if (AskQuestionReturnNegative(TgLocale.MenuStorageDbBackup)) return;
TgLog.WriteLine($"{TgLocale.MenuStorageBackupDirectory}: {Path.GetDirectoryName(TgAppSettings.AppXml.XmlEfStorage)}");
(bool IsSuccess, string FileName) backupResult = EfContext.BackupDb();
TgLog.WriteLine($"{TgLocale.MenuStorageBackupFile}: {backupResult.FileName}");
Expand All @@ -83,9 +82,8 @@ private void TgStorageBackupDb()

private void TgStorageCreateNewDb()
{
if (AskQuestionReturnNegative(TgLocale.MenuStorageDbCreateNew))
return;
TgEfUtils.CreateAndUpdateDb();
if (AskQuestionReturnNegative(TgLocale.MenuStorageDbCreateNew)) return;
TgEfUtils.CreateAndUpdateDbAsync().GetAwaiter().GetResult();
}

private void TgStorageDeleteExistsDb()
Expand All @@ -106,10 +104,9 @@ private void TgStorageTablesVersionsView()

private void TgStorageTablesClear()
{
if (AskQuestionReturnNegative(TgLocale.MenuStorageTablesClear))
return;
if (AskQuestionReturnNegative(TgLocale.MenuStorageTablesClear)) return;
EfContext.DeleteTables();
TgEfUtils.CreateAndUpdateDb();
TgEfUtils.CreateAndUpdateDbAsync().GetAwaiter().GetResult();
TgLog.WriteLine(TgLocale.MenuStorageTablesClearFinished);
Console.ReadKey();
}
Expand Down
16 changes: 8 additions & 8 deletions Clients/TgDownloaderConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

// Register TgEfContext as the DbContext for EF Core
tgLog.WriteLine("EF Core init ...");
TgEfUtils.CreateAndUpdateDb();
await TgEfUtils.CreateAndUpdateDbAsync();
tgLog.WriteLine("EF Core init success");

// Transfer data from previous TgDownloader.db into TgStorage.db
DataTransferBetweenStorages();
await DataTransferBetweenStoragesAsync();

TgDownloadSettingsViewModel tgDownloadSettings = new();
TgMenuHelper menu = new();
Expand Down Expand Up @@ -76,14 +76,14 @@
}
} while (menu.Value is not TgEnumMenuMain.Exit);

void DataTransferBetweenStorages()
async Task DataTransferBetweenStoragesAsync()
{
using TgEfContext efContextTo = TgEfUtils.CreateEfContext();
if (TgEfUtils.IsDataExistsInTablesAsync(efContextTo, tgLog.WriteLine).GetAwaiter().GetResult()) return;
await using TgEfContext efContextTo = TgEfUtils.CreateEfContext();
if (await TgEfUtils.IsDataExistsInTablesAsync(efContextTo, tgLog.WriteLine)) return;

if (!File.Exists(TgAppSettingsHelper.Instance.AppXml.XmlFileStorage)) return;
using TgEfContext efContextFrom = TgEfUtils.CreateEfContext(TgAppSettingsHelper.Instance.AppXml.XmlFileStorage);
if (!TgEfUtils.IsDataExistsInTablesAsync(efContextFrom, tgLog.WriteLine).GetAwaiter().GetResult()) return;
await using TgEfContext efContextFrom = TgEfUtils.CreateEfContext(TgAppSettingsHelper.Instance.AppXml.XmlFileStorage);
//if (! await TgEfUtils.IsDataExistsInTablesAsync(efContextFrom, tgLog.WriteLine)) return;

string prompt = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title($"{TgLocaleHelper.Instance.AskDataMigration}")
Expand All @@ -92,7 +92,7 @@ void DataTransferBetweenStorages()
if (prompt.Equals(TgLocaleHelper.Instance.MenuIsFalse)) return;

tgLog.WriteLine("Storage transfer ...");
TgEfUtils.DataTransferBetweenStoragesAsync(efContextFrom, efContextTo, tgLog.WriteLine).GetAwaiter().GetResult();
await TgEfUtils.DataTransferBetweenStoragesAsync(efContextFrom, efContextTo, tgLog.WriteLine);
tgLog.WriteLine("Storage transfer success");
}

Expand Down
43 changes: 21 additions & 22 deletions Core/TgDownloader/Helpers/TgClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ private async Task<int> SetChannelMessageIdFirstCoreAsync(TgDownloadSettingsView
public TgDownloadSmartSource CreateSmartSource(TgDownloadSettingsViewModel tgDownloadSettings, bool isSilent)
{
TgEfStorageResult<TgEfSourceEntity> storageResult = SourceRepository
.Get(new TgEfSourceEntity { Id = tgDownloadSettings.SourceVm.SourceId }, isNoTracking: false);
.Get(new() { Id = tgDownloadSettings.SourceVm.SourceId }, isNoTracking: false);

TgDownloadSmartSource smartSource = CreateSmartSourceCoreAsync(tgDownloadSettings).GetAwaiter().GetResult();
if (smartSource.ChatBase is not null && IsChatBaseAccessAsync(smartSource.ChatBase).GetAwaiter().GetResult())
Expand Down Expand Up @@ -1016,18 +1016,16 @@ public void UpdateSourceDb(TgEfSourceViewModel sourceVm, TgDownloadSettingsViewM

private async Task UpdateSourceTgAsync(TlChannel channel, string about, int count)
{
TgEfSourceEntity itemFind = (await SourceRepository.GetAsync(new TgEfSourceEntity
{ Id = channel.id }, isNoTracking: false)).Item;

itemFind.Id = channel.id;
itemFind.UserName = channel.username;
if (!string.IsNullOrEmpty(channel.title))
itemFind.Title = channel.title;
if (!string.IsNullOrEmpty(about))
itemFind.About = about;
itemFind.Count = count;

await SourceRepository.SaveAsync(itemFind);
//var storageResult = await SourceRepository.GetAsync(new() { Id = channel.id }, isNoTracking: false);
var source = new TgEfSourceEntity
{
Id = channel.id,
UserName = channel.username,
Title = channel.title,
About = about,
Count = count
};
await SourceRepository.SaveAsync(source);
}

private async Task UpdateSourceTgAsync(TlChannel channel, int count) =>
Expand All @@ -1051,7 +1049,7 @@ await TryCatchFuncAsync(async () =>
}
tgDownloadSettings.SourceVm.SourceScanCount = DicChatsAll.Count;
tgDownloadSettings.SourceVm.SourceScanCurrent = 0;
// ListChannels.
// ListChannels
foreach (TlChannel channel in EnumerableChannels)
{
tgDownloadSettings.SourceVm.SourceScanCurrent++;
Expand All @@ -1066,13 +1064,14 @@ await TryCatchFuncAsync(async () =>
if (chatFull?.full_chat is ChannelFull channelFull)
{
await UpdateSourceTgAsync(channel, channelFull.about, messagesCount);
await UpdateStateSourceAsync(tgDownloadSettings.SourceVm.SourceId, 0, $"{channel} | {messagesCount} | {TgDataFormatUtils.TrimStringEnd(channelFull.about)}");
await UpdateStateSourceAsync(tgDownloadSettings.SourceVm.SourceId, tgDownloadSettings.SourceVm.SourceScanCurrent,
$"{channel} | {TgDataFormatUtils.TrimStringEnd(channelFull.about, 40)}");
}
}
else
{
await UpdateSourceTgAsync(channel, messagesCount);
await UpdateStateSourceAsync(tgDownloadSettings.SourceVm.SourceId, 0, $"{channel} | {messagesCount}");
await UpdateStateSourceAsync(tgDownloadSettings.SourceVm.SourceId, tgDownloadSettings.SourceVm.SourceScanCurrent, $"{channel}");
}
}, isLoginConsole: true);
}
Expand Down Expand Up @@ -1209,11 +1208,11 @@ await TryCatchFuncAsync(async () =>
bool isAccessToMessages = await Client.Channels_ReadMessageContents(smartSource.ChatBase as TlChannel);
if (isAccessToMessages)
{
List<Task> downloadTasks = new List<Task>();
List<Task> downloadTasks = new();
while (tgDownloadSettings.SourceVm.SourceFirstId <= tgDownloadSettings.SourceVm.SourceLastId)
{
if (Client is null || (Client is not null && Client.Disconnected) || !tgDownloadSettings.SourceVm.IsDownload)
break;
if (Client is null) break;
if ((Client is not null && Client.Disconnected) || !tgDownloadSettings.SourceVm.IsDownload) break;
await TryCatchFuncAsync(async () =>
{
Messages_MessagesBase messages = smartSource.ChatBase is not null
Expand All @@ -1235,7 +1234,7 @@ await TryCatchFuncAsync(async () =>
{
await Task.WhenAll(downloadTasks);
downloadTasks.Clear();
downloadTasks = new List<Task>();
downloadTasks = new();
}
}, isLoginConsole: true);
}
Expand Down Expand Up @@ -1593,10 +1592,10 @@ private async Task MessageSaveAsync(TgDownloadSettingsViewModel tgDownloadSettin
// });
//}
TgEfStorageResult<TgEfMessageEntity> storageResult = await MessageRepository.GetAsync(
new TgEfMessageEntity { SourceId = tgDownloadSettings.SourceVm.SourceId, Id = tgDownloadSettings.SourceVm.SourceFirstId }, isNoTracking: false);
new() { SourceId = tgDownloadSettings.SourceVm.SourceId, Id = tgDownloadSettings.SourceVm.SourceFirstId }, isNoTracking: false);
if (!storageResult.IsExists || (storageResult.IsExists && tgDownloadSettings.IsRewriteMessages))
{
await MessageRepository.SaveAsync(new TgEfMessageEntity
await MessageRepository.SaveAsync(new()
{
Id = messageId,
SourceId = tgDownloadSettings.SourceVm.SourceId,
Expand Down
8 changes: 6 additions & 2 deletions Docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Help menu
- Russian localization

## [0.5.150] - 2024-04-28
## [0.5.150] - 2024-11-05
### Fixed
- Created EF Core entities
- Fixed test execution
- Fixed workflows actions
## Changed
- Using EF Core instead of DevExpress XPO
- Test execution
## Added projects
- Added TgDownloaderWinUI app

## [0.4.150] - 2024-03-27
## Added projects
Expand Down

0 comments on commit a07ab11

Please sign in to comment.