From f7479e0c74e5a23f522216e5ecc880c593b05629 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 11 Jun 2021 19:08:24 +0430 Subject: [PATCH] #140 import now is working --- GanjooRazor/Areas/Admin/Pages/Poet.cshtml | 13 + GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs | 32 +++ RMuseum/Controllers/GanjoorController.cs | 8 +- RMuseum/RMuseum.xml | 10 +- RMuseum/Services/IGanjoorService.cs | 7 +- .../GanjoorService-SQLiteImport.cs | 259 ++++++++++-------- .../Services/Implementation/GanjoorService.cs | 5 + 7 files changed, 215 insertions(+), 119 deletions(-) diff --git a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml index bbe613a2..8e4c7218 100644 --- a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml +++ b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml @@ -64,5 +64,18 @@ + + + + + + +
بارگذاری پایگاه داده‌ها: +
+ + +
+
+ diff --git a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs index 4ac5eeec..e5e29bec 100644 --- a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs +++ b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs @@ -49,6 +49,9 @@ namespace GanjooRazor.Areas.Admin.Pages [BindProperty] public IFormFile Image { get; set; } + [BindProperty] + public IFormFile SQLiteDb { get; set; } + private async Task PreparePoet() { @@ -162,5 +165,34 @@ namespace GanjooRazor.Areas.Admin.Pages return Page(); } + + public async Task OnPostUploadDbAsync(IFormFile SQLiteDb) + { + LastResult = ""; + await PreparePoet(); + + + using (HttpClient secureClient = new HttpClient()) + { + await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response); + + MultipartFormDataContent form = new MultipartFormDataContent(); + + using (MemoryStream stream = new MemoryStream()) + { + await SQLiteDb.CopyToAsync(stream); + var fileContent = stream.ToArray(); + form.Add(new ByteArrayContent(fileContent, 0, fileContent.Length), Poet.Nickname, SQLiteDb.FileName); + + HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/ganjoor/sqliteimport/{Request.Query["id"]}", form); + response.EnsureSuccessStatusCode(); + + LastResult = "پایگاه داده‌ها بارگذاری شد."; + + } + } + + return Page(); + } } } diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 3bf67680..e8e932e8 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -1027,7 +1027,7 @@ namespace RMuseum.Controllers } /// - /// imports data from ganjoor SQLite database + /// imports data from ganjoor SQLite database (form file) /// /// /// @@ -1037,10 +1037,12 @@ namespace RMuseum.Controllers [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public IActionResult ImportLocalSQLiteDb(int poetId) + public async Task ImportLocalSQLiteDb(int poetId) { + IFormFile file = Request.Form.Files[0]; + RServiceResult res = - _ganjoorService.ImportFromSqlite(poetId, ""); + await _ganjoorService.ImportFromSqlite(poetId, file); if (res.Result) return Ok(); return BadRequest(res.ExceptionString); diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 208e33f0..7d36b349 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -950,7 +950,7 @@ - imports data from ganjoor SQLite database + imports data from ganjoor SQLite database (form file) @@ -7296,12 +7296,12 @@ - + import from sqlite - + @@ -8334,12 +8334,12 @@ - + import from sqlite - + diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 17c08cad..8ef9e3f0 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -1,4 +1,5 @@ -using RMuseum.Models.Ganjoor; +using Microsoft.AspNetCore.Http; +using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Models.GanjoorAudio.ViewModels; using RSecurityBackend.Models.Generic; @@ -293,9 +294,9 @@ namespace RMuseum.Services /// import from sqlite /// /// - /// + /// /// - RServiceResult ImportFromSqlite(int poetId, string filePath); + Task> ImportFromSqlite(int poetId, IFormFile file); /// /// import GanjoorPage entity data from MySql diff --git a/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs b/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs index 81bab3b1..284bb6a7 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs @@ -1,4 +1,6 @@ using Dapper; +using DNTPersianUtils.Core; +using Microsoft.AspNetCore.Http; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using RMuseum.DbContext; @@ -9,6 +11,7 @@ using RSecurityBackend.Services.Implementation; using System; using System.Collections.Generic; using System.Data; +using System.IO; using System.Linq; using System.Threading.Tasks; @@ -19,16 +22,30 @@ namespace RMuseum.Services.Implementation /// public partial class GanjoorService : IGanjoorService { - + /// /// import from sqlite /// /// - /// + /// /// - public RServiceResult ImportFromSqlite(int poetId, string filePath) + public async Task> ImportFromSqlite(int poetId, IFormFile file) { - _backgroundTaskQueue.QueueBackgroundWorkItem + 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 => { @@ -57,9 +74,7 @@ namespace RMuseum.Services.Implementation await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Importing"); - await _ImportSQLiteCatChildren(context, sqlite, poetId, 0, cat, poet.Nickname); - - await context.SaveChangesAsync(); + await _ImportSQLiteCatChildren(context, sqlite, poetId, await sqlite.QuerySingleAsync($"SELECT id FROM cat WHERE parent_id = 0") , cat, poet.Nickname); await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); } @@ -69,129 +84,157 @@ namespace RMuseum.Services.Implementation 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); } - private async Task _ImportSQLiteCatChildren(RMuseumDbContext context, IDbConnection sqlite, int poetId, int catId, GanjoorCat parentCat, string parentFullTitle) + private async Task _ImportSQLiteCatChildren(RMuseumDbContext context, IDbConnection sqlite, int poetId, int sqliteParentCatId, GanjoorCat parentCat, string parentFullTitle) { - string catHtmlText = ""; - foreach (var cat in await sqlite.QueryAsync($"SELECT * FROM cat WHERE poet_id = {poetId} AND parent_id = {catId} ORDER BY id")) + try { - var poetCatId = 1 + await context.GanjoorCategories.MaxAsync(c => c.Id); - - string url = GPersianTextSync.Farglisize(cat.text); - - GanjoorCat dbCat = new GanjoorCat() + string catHtmlText = ""; + foreach (var cat in await sqlite.QueryAsync($"SELECT * FROM cat WHERE parent_id = {sqliteParentCatId} ORDER BY id")) { - Id = poetCatId, - PoetId = poetId, - Title = cat.text, - UrlSlug = url, - FullUrl = $"{parentCat.FullUrl}/{url}" - }; - context.GanjoorCategories.Add(dbCat); + var poetCatId = 1 + await context.GanjoorCategories.MaxAsync(c => c.Id); - var catPageId = 1 + await context.GanjoorPages.MaxAsync(p => p.Id); + string url = GPersianTextSync.Farglisize(cat.text); - GanjoorPage dbPageCat = new GanjoorPage() - { - Id = catPageId, - GanjoorPageType = GanjoorPageType.CatPage, - Published = false, - PageOrder = -1, - Title = dbCat.Title, - FullTitle = $"{parentFullTitle} » {dbCat.Title}", - UrlSlug = dbCat.UrlSlug, - FullUrl = dbCat.FullUrl, - HtmlText = "", - PoetId = poetId, - CatId = poetCatId, - PostDate = DateTime.Now - }; - - context.GanjoorPages.Add(dbPageCat); - - catHtmlText += $"

{dbCat.Title}

{Environment.NewLine}"; - - await _ImportSQLiteCatChildren(context, sqlite, poetId, (int)cat.id, dbCat, $"{parentFullTitle} » {dbCat.Title}"); - } - - var poemId = 1 + await context.GanjoorPoems.MaxAsync(p => p.Id); - while (await context.GanjoorPages.Where(p => p.Id == poemId).AnyAsync()) - poemId++; - - int poemNumber = 0; - foreach (var poem in await sqlite.QueryAsync($"SELECT * FROM poem WHERE cat_id = {catId} ORDER BY id")) - { - poemNumber++; - GanjoorPoem dbPoem = new GanjoorPoem() - { - Id = poemId, - CatId = parentCat.Id, - Title = poem.title, - UrlSlug = $"sh{poemNumber}", - FullTitle = $"{parentFullTitle} » {poem.title}", - FullUrl = $"{parentCat.FullUrl}/sh{poemNumber}", - }; - - List poemVerses = new List(); - foreach (var verse in await sqlite.QueryAsync($"SELECT * FROM verse WHERE poem_id = {poem.id} ORDER BY vorder")) - { - GanjoorVerse dbVerse = new GanjoorVerse() + GanjoorCat dbCat = new GanjoorCat() { - PoemId = poemId, - VOrder = verse.vorder, - VersePosition = verse.position, - Text = verse.text.Replace("ـ", "").Replace(" ", " ").ApplyCorrectYeKe().Trim() + Id = poetCatId, + PoetId = poetId, + Title = cat.text, + UrlSlug = url, + FullUrl = $"{parentCat.FullUrl}/{url}", + ParentId = parentCat.Id }; - poemVerses.Add(dbVerse); + context.GanjoorCategories.Add(dbCat); + + var catPageId = 1 + await context.GanjoorPages.MaxAsync(p => p.Id); + while (await context.GanjoorPoems.Where(p => p.Id == catPageId).AnyAsync()) + catPageId++; + + GanjoorPage dbPageCat = new GanjoorPage() + { + Id = catPageId, + GanjoorPageType = GanjoorPageType.CatPage, + Published = false, + PageOrder = -1, + Title = dbCat.Title, + FullTitle = $"{parentFullTitle} » {dbCat.Title}", + UrlSlug = dbCat.UrlSlug, + FullUrl = dbCat.FullUrl, + HtmlText = "", + PoetId = poetId, + CatId = poetCatId, + PostDate = DateTime.Now + }; + + context.GanjoorPages.Add(dbPageCat); + + await context.SaveChangesAsync(); + + catHtmlText += $"

{dbCat.Title}

{Environment.NewLine}"; + + await _ImportSQLiteCatChildren(context, sqlite, poetId, (int)cat.id, dbCat, $"{parentFullTitle} » {dbCat.Title}"); } - dbPoem.PlainText = PreparePlainText(poemVerses); - dbPoem.HtmlText = PrepareHtmlText(poemVerses); + var poemId = 1 + await context.GanjoorPoems.MaxAsync(p => p.Id); + while (await context.GanjoorPages.Where(p => p.Id == poemId).AnyAsync()) + poemId++; - context.GanjoorPoems.Add(dbPoem); - - foreach (var dbVerse in poemVerses) + int poemNumber = 0; + foreach (var poem in await sqlite.QueryAsync($"SELECT * FROM poem WHERE cat_id = {sqliteParentCatId} ORDER BY id")) { - context.GanjoorVerses.Add(dbVerse); - await context.SaveChangesAsync();//id set should be in order + poemNumber++; + GanjoorPoem dbPoem = new GanjoorPoem() + { + Id = poemId, + CatId = parentCat.Id, + Title = poem.title, + UrlSlug = $"sh{poemNumber}", + FullTitle = $"{parentFullTitle} » {poem.title}", + FullUrl = $"{parentCat.FullUrl}/sh{poemNumber}", + }; + + List poemVerses = new List(); + foreach (var verse in await sqlite.QueryAsync($"SELECT * FROM verse WHERE poem_id = {poem.id} ORDER BY vorder")) + { + int vOrder = int.Parse(verse.vorder.ToString()); + int position = int.Parse(verse.position.ToString()); + string text = verse.text; + GanjoorVerse dbVerse = new GanjoorVerse() + { + PoemId = poemId, + VOrder = vOrder, + VersePosition = (VersePosition)position, + Text = text.Replace("ـ", "").Replace(" ", " ").ApplyCorrectYeKe().Trim() + }; + poemVerses.Add(dbVerse); + } + + dbPoem.PlainText = PreparePlainText(poemVerses); + dbPoem.HtmlText = PrepareHtmlText(poemVerses); + + context.GanjoorPoems.Add(dbPoem); + await context.SaveChangesAsync(); + + foreach (var dbVerse in poemVerses) + { + context.GanjoorVerses.Add(dbVerse); + await context.SaveChangesAsync();//id set should be in order + } + + GanjoorPage dbPoemPage = new GanjoorPage() + { + Id = poemId, + GanjoorPageType = GanjoorPageType.PoemPage, + Published = false, + PageOrder = -1, + Title = dbPoem.Title, + FullTitle = dbPoem.FullTitle, + UrlSlug = dbPoem.UrlSlug, + FullUrl = dbPoem.FullUrl, + HtmlText = dbPoem.HtmlText, + PoetId = poetId, + CatId = parentCat.Id, + PoemId = poemId, + PostDate = DateTime.Now + }; + + context.GanjoorPages.Add(dbPoemPage); + await context.SaveChangesAsync(); + + catHtmlText += $"

{dbPoemPage.Title}

{Environment.NewLine}"; + + + poemId++; } - GanjoorPage dbPoemPage = new GanjoorPage() + if (!string.IsNullOrEmpty(catHtmlText)) { - Id = poemId, - GanjoorPageType = GanjoorPageType.PoemPage, - Published = false, - PageOrder = -1, - Title = dbPoem.Title, - FullTitle = dbPoem.FullTitle, - UrlSlug = dbPoem.UrlSlug, - FullUrl = dbPoem.FullUrl, - HtmlText = dbPoem.HtmlText, - PoetId = poemId, - CatId = parentCat.Id, - PostDate = DateTime.Now - }; + var parentCatPage = await context.GanjoorPages.Where(p => p.FullUrl == parentCat.FullUrl).SingleAsync(); + parentCatPage.HtmlText += catHtmlText; + context.GanjoorPages.Update(parentCatPage); + } - _context.GanjoorPages.Add(dbPoemPage); - - catHtmlText += $"

{dbPoemPage.Title}

{Environment.NewLine}"; - - - poemId++; + await context.SaveChangesAsync(); } - - if (!string.IsNullOrEmpty(catHtmlText)) + catch(Exception exp) { - var parentCatPage = await context.GanjoorPages.Where(p => p.FullUrl == parentCat.FullUrl && p.GanjoorPageType == GanjoorPageType.CatPage && p.PoetId == poetId).SingleAsync(); - parentCatPage.HtmlText += catHtmlText; - context.GanjoorPages.Update(parentCatPage); + return exp.ToString(); } - - await context.SaveChangesAsync(); - + return ""; } diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 9be382d9..87e63aa0 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -2627,6 +2627,9 @@ namespace RMuseum.Services.Implementation var id = 1 + await _context.GanjoorPoets.MaxAsync(p => p.Id); + if (string.IsNullOrEmpty(poet.Description)) + poet.Description = ""; + GanjoorPoet dbPoet = new GanjoorPoet() { Id = id, @@ -2651,6 +2654,8 @@ namespace RMuseum.Services.Implementation _context.GanjoorCategories.Add(dbCat); var poetPageId = 1 + await _context.GanjoorPages.MaxAsync(p => p.Id); + while (await _context.GanjoorPoems.Where(p => p.Id == poetPageId).AnyAsync()) + poetPageId++; var pageText = ""; foreach(var line in poet.Description.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))