From 848a5f4bd2faa8cd480da7a9b27df603fc380e16 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sun, 1 Sep 2024 12:23:38 +0330 Subject: [PATCH] #445 TajikImportFromSqlite --- .../GanjoorService-Tajik.cs | 2 - .../GanjoorService-TajikSQLiteImport.cs | 109 ++++++++++++++++++ .../PublishProfiles/FolderProfile.pubxml | 17 +++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs create mode 100644 TajikGanjoor/Properties/PublishProfiles/FolderProfile.pubxml diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Tajik.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Tajik.cs index fdd3e789..486b18da 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Tajik.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Tajik.cs @@ -5,8 +5,6 @@ using System.Threading.Tasks; using RSecurityBackend.Models.Generic; using RMuseum.Models.Ganjoor.ViewModels; using System.Linq; -using Org.BouncyCastle.Asn1.Ocsp; -using DNTPersianUtils.Core; using RSecurityBackend.Services.Implementation; using System.Collections.Generic; diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs new file mode 100644 index 00000000..f61f0007 --- /dev/null +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-TajikSQLiteImport.cs @@ -0,0 +1,109 @@ +using Microsoft.EntityFrameworkCore; +using System; +using RMuseum.Models.Ganjoor; +using System.Threading.Tasks; +using RSecurityBackend.Models.Generic; +using RMuseum.Models.Ganjoor.ViewModels; +using System.Linq; +using RSecurityBackend.Services.Implementation; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; +using Microsoft.Data.Sqlite; +using RMuseum.DbContext; +using System.Data; +using System.IO; +using Dapper; + +namespace RMuseum.Services.Implementation +{ + /// + /// IGanjoorService implementation + /// + public partial class GanjoorService : IGanjoorService + { + public async Task> TajikImportFromSqlite(int poetId, IFormFile file) + { + try + { + string dir = Path.Combine($"{Configuration.GetSection("PictureFileService")["StoragePath"]}", "SQLiteImports"); + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + string filePath = Path.Combine(dir, file.FileName); + if (File.Exists(filePath)) + File.Delete(filePath); + using (FileStream fsMain = new FileStream(filePath, FileMode.Create)) + { + await file.CopyToAsync(fsMain); + } + + _backgroundTaskQueue.QueueBackgroundWorkItem + ( + async token => + { + using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so _context might be already been freed/collected by GC + { + LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context); + var job = (await jobProgressServiceEF.NewJob("ImportFromSqlite", "Query data")).Result; + + try + { + SqliteConnectionStringBuilder connectionStringBuilder = new SqliteConnectionStringBuilder(); + connectionStringBuilder.DataSource = filePath; + using (SqliteConnection sqliteConnection = new SqliteConnection(connectionStringBuilder.ToString())) + { + await sqliteConnection.OpenAsync(); + IDbConnection sqlite = sqliteConnection; + var poets = (await sqlite.QueryAsync("SELECT * FROM poet ORDER BY id")).ToList(); + if (poetId != 0 && poets.Count != 1) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, "poets count in sqlite db is not equal to 1"); + } + + foreach (var poet in poets) + { + int poetId = poet.id; + if (await context.TajikPoets.AnyAsync(p => p.Id == poetId)) continue; + var cat = await context.GanjoorCategories.AsNoTracking().Where(c => c.PoetId == poetId && c.ParentId == null).SingleAsync(); + var catPage = await context.GanjoorPages.AsNoTracking().Where(p => p.FullUrl == cat.FullUrl).SingleAsync(); + + await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Importing"); + + var resImport = await _ImportSQLiteCatChildren(context, sqlite, poetId, await sqlite.QuerySingleAsync($"SELECT id FROM cat WHERE parent_id = 0"), cat, poet.Nickname, jobProgressServiceEF, job, catPage.Id); + + if (string.IsNullOrEmpty(resImport)) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + else + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, resImport); + } + } + + + + + + + } + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + } + + File.Delete(filePath); + } + ); + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + return new RServiceResult(true); + } + + } +} \ No newline at end of file diff --git a/TajikGanjoor/Properties/PublishProfiles/FolderProfile.pubxml b/TajikGanjoor/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 00000000..d6c4b49f --- /dev/null +++ b/TajikGanjoor/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,17 @@ + + + + + false + false + true + Release + Any CPU + FileSystem + bin\Release\net8.0-windows\publish\ + FileSystem + <_TargetId>Folder + + \ No newline at end of file