diff --git a/RMuseum/Controllers/ArtifactController.cs b/RMuseum/Controllers/ArtifactController.cs index 853b6f2c..1aaf80d7 100644 --- a/RMuseum/Controllers/ArtifactController.cs +++ b/RMuseum/Controllers/ArtifactController.cs @@ -2048,6 +2048,33 @@ namespace RMuseum.Controllers return Ok(pagedResult.Result.Items); } + /// + /// start setting an artifact items as a category poems text original source + /// + /// + /// + /// + + [HttpPut("ganjoorlink/settextorigin/{ganjoorCatId}/{artifactId}")] + [Authorize(Policy = RMuseumSecurableItem.ArtifactEntityShortName + ":" + RMuseumSecurableItem.ReviewGanjoorLinksOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Unauthorized)] + public IActionResult StartSettingArtifactAsTextOriginalSource(int ganjoorCatId, Guid artifactId) + { + try + { + var res = _artifactService.StartSettingArtifactAsTextOriginalSource(ganjoorCatId, artifactId); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(); + } + catch (Exception exp) + { + return BadRequest(exp.ToString()); + } + } + /// /// readonly mode /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 1ac9d4c9..27215f6b 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -618,6 +618,14 @@ + + + start setting an artifact items as a category poems text original source + + + + + readonly mode @@ -10346,6 +10354,14 @@ + + + start setting an artifact items as text original source + + + + + donation service @@ -11633,6 +11649,9 @@ IArtifactService implementation + + IArtifactService implementation + @@ -11740,6 +11759,14 @@ golestan-walters-01 + + + start setting an artifact items as text original source + + + + + start removing original images diff --git a/RMuseum/Services/IArtifactService.cs b/RMuseum/Services/IArtifactService.cs index 2335b27e..302bcab1 100644 --- a/RMuseum/Services/IArtifactService.cs +++ b/RMuseum/Services/IArtifactService.cs @@ -579,5 +579,13 @@ namespace RMuseum.Services /// Task> SearchArtifactItems(PagingParameterModel paging, string term); + /// + /// start setting an artifact items as text original source + /// + /// + /// + /// + RServiceResult StartSettingArtifactAsTextOriginalSource(int ganjoorCatId, Guid artifactId); + } } diff --git a/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-IsTextOriginalSource.cs b/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-IsTextOriginalSource.cs new file mode 100644 index 00000000..4f8aa5f3 --- /dev/null +++ b/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-IsTextOriginalSource.cs @@ -0,0 +1,93 @@ +using Microsoft.EntityFrameworkCore; +using RMuseum.DbContext; +using RSecurityBackend.Models.Generic; +using RSecurityBackend.Services.Implementation; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace RMuseum.Services.Implementation +{ + /// + /// IArtifactService implementation + /// + public partial class ArtifactService : IArtifactService + { + /// + /// start setting an artifact items as text original source + /// + /// + /// + /// + public RServiceResult StartSettingArtifactAsTextOriginalSource(int ganjoorCatId, Guid artifactId) + { + try + { + _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($"SettingArtifactAsTextOriginalSource({ganjoorCatId}, {artifactId})", "Query Poems Data")).Result; + + try + { + List poemIdSet = new List(); + await _EnumerateGanjoorCatPoemIdSet(context, poemIdSet, ganjoorCatId); + await jobProgressServiceEF.UpdateJob(job.Id, 0, "Query Links Data"); + var links = await context.GanjoorLinks.Where(l => l.ArtifactId == artifactId).ToListAsync(); + await jobProgressServiceEF.UpdateJob(job.Id, 0, "Removing Old Originals"); + foreach (var poemId in poemIdSet) + { + var poemLinks = await context.GanjoorLinks.Where(l => l.GanjoorPostId == poemId && l.IsTextOriginalSource == true).ToListAsync(); + foreach (var poemLink in poemLinks) + { + poemLink.IsTextOriginalSource = false; + context.Update(poemLink); + } + } + await jobProgressServiceEF.UpdateJob(job.Id, 0, "Updating"); + foreach (var link in links) + { + if(poemIdSet.Where(p => p == link.GanjoorPostId).Any()) + { + link.IsTextOriginalSource = true; + context.Update(link); + } + } + + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + } + } + ); + + + return new RServiceResult(true); + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + } + + private async Task _EnumerateGanjoorCatPoemIdSet(RMuseumDbContext context, List poemIdSet, int catId) + { + var catPoemIdSets = await context.GanjoorPoems.AsNoTracking().Where(p => p.CatId == catId).Select(p => p.Id).ToListAsync(); + if (catPoemIdSets.Count > 0) + poemIdSet.AddRange(catPoemIdSets); + var subCatIdSet = await context.GanjoorCategories.AsNoTracking().Where(c => c.ParentId == catId).Select(c => c.Id).ToListAsync(); + foreach (var subCatId in subCatIdSet) + { + await _EnumerateGanjoorCatPoemIdSet(context, poemIdSet, subCatId); + } + } + } +}