From 580ea1ef349e6c9a82e20ac9a487323e3153fc16 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 20 Mar 2025 10:14:04 +0330 Subject: [PATCH] improving tagging with digital sources --- RMuseum/Controllers/GanjoorController.cs | 9 ++-- RMuseum/RMuseum.xml | 12 ++--- RMuseum/Services/IGanjoorService.cs | 4 +- .../GanjoorService-DigitalSources.cs | 48 ++++++++++++++++--- 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 63475481..329a9e22 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -4248,16 +4248,15 @@ namespace RMuseum.Controllers /// tag category with source /// /// - /// + /// /// - [HttpPut("source/{catId}/{sourceUrlSlug}")] + [HttpPut("source/{catId}")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public IActionResult TagCategoryWithSource(int catId, string sourceUrlSlug) + public IActionResult TagCategoryWithSource(int catId, [FromBody]DigitalSource source) { - - _ganjoorService.TagCategoryWithSource(catId, sourceUrlSlug); + _ganjoorService.TagCategoryWithSource(catId, source); return Ok(); } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index c95dc06e..f9f0d7c1 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2295,12 +2295,12 @@ - + tag category with source - + @@ -17447,12 +17447,12 @@ - + tag with sources - + @@ -19413,12 +19413,12 @@ - + tag with sources - + diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 7ea86138..835ec368 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -1459,8 +1459,8 @@ namespace RMuseum.Services /// tag with sources /// /// - /// - void TagCategoryWithSource(int catId, string sourceUrlSlug); + /// + void TagCategoryWithSource(int catId, DigitalSource source); /// /// update digital sources stats diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-DigitalSources.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-DigitalSources.cs index 0595c56d..d7680679 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-DigitalSources.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-DigitalSources.cs @@ -10,6 +10,7 @@ using DNTPersianUtils.Core; using System.Globalization; using System.Threading.Tasks; using RSecurityBackend.Models.Generic; +using System.Security.Cryptography.X509Certificates; namespace RMuseum.Services.Implementation { @@ -38,8 +39,8 @@ namespace RMuseum.Services.Implementation /// tag with sources /// /// - /// - public void TagCategoryWithSource(int catId, string sourceUrlSlug) + /// + public void TagCategoryWithSource(int catId, DigitalSource source) { _backgroundTaskQueue.QueueBackgroundWorkItem ( @@ -48,7 +49,7 @@ namespace RMuseum.Services.Implementation 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($"TagCategoryWithSource(catId: {catId} - source:{sourceUrlSlug})", "Query data")).Result; + var job = (await jobProgressServiceEF.NewJob($"TagCategoryWithSource(catId: {catId} - source:{source.UrlSlug})", "Query data")).Result; try { var pages = await context.GanjoorPages.AsNoTracking().Where(p => p.FullUrl.StartsWith("/sources/")).ToListAsync(); @@ -87,10 +88,45 @@ namespace RMuseum.Services.Implementation } } - var digitalSource = await context.DigitalSources.Where(p => p.UrlSlug == sourceUrlSlug).SingleOrDefaultAsync(); + var digitalSource = await context.DigitalSources.Where(p => p.UrlSlug == source.UrlSlug).SingleOrDefaultAsync(); if (digitalSource == null) { - await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, $"Digital source not found: {sourceUrlSlug}"); + context.DigitalSources.Add + ( + new DigitalSource() + { + UrlSlug = source.UrlSlug, + ShortName = source.ShortName, + FullName = source.FullName, + SourceType = source.SourceType, + CoupletsCount = 0, + } + ); + + var parentPage = await context.GanjoorPages.AsNoTracking().Where(p => p.UrlSlug == "sources").SingleAsync(); + var newPageId = 1 + await context.GanjoorPages.MaxAsync(p => p.Id); + while (await context.GanjoorPoems.Where(p => p.Id == newPageId).AnyAsync()) + newPageId++; + context.GanjoorPages.Add + ( + new GanjoorPage() + { + Id = newPageId, + GanjoorPageType = GanjoorPageType.None, + Published = true, + PageOrder = -1, + Title = source.FullName, + FullTitle = $"{parentPage.FullTitle} ยป {source.FullName}", + FullUrl = $"{parentPage.FullUrl}/{source.UrlSlug}", + UrlSlug = source.UrlSlug, + HtmlText = $"

{source.FullName}

", + PostDate = DateTime.Now, + ParentId = parentPage.Id, + } + ); + + await context.SaveChangesAsync(); + digitalSource = await context.DigitalSources.Where(p => p.UrlSlug == source.UrlSlug).SingleAsync(); } List catIdList = new List @@ -107,7 +143,7 @@ namespace RMuseum.Services.Implementation foreach (var poem in poems) { poem.SourceName = digitalSource.ShortName; - poem.SourceUrlSlug = sourceUrlSlug; + poem.SourceUrlSlug = source.UrlSlug; context.Update(poem); int coupletCount = await context.GanjoorVerses.AsNoTracking()