From 975aae85e6c79b585dc342545d6051b00560efce Mon Sep 17 00:00:00 2001
From: Hamid Reza Mohammadi
Date: Fri, 22 Dec 2023 17:30:39 +0330
Subject: [PATCH] #418 regenerate related pages
---
RMuseum/Controllers/GanjoorController.cs | 9 +-
RMuseum/RMuseum.xml | 18 ++--
RMuseum/Services/IGanjoorService.cs | 9 +-
.../GanjoorService-QuotedPoems.cs | 83 +++++++++++++++++--
4 files changed, 102 insertions(+), 17 deletions(-)
diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs
index e2043af0..186fb592 100644
--- a/RMuseum/Controllers/GanjoorController.cs
+++ b/RMuseum/Controllers/GanjoorController.cs
@@ -3749,8 +3749,9 @@ namespace RMuseum.Controllers
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task InsertGanjoorQuotedPoemAsync([FromBody] GanjoorQuotedPoem quoted)
{
+ var loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
RServiceResult res =
- await _ganjoorService.InsertGanjoorQuotedPoemAsync(quoted);
+ await _ganjoorService.InsertGanjoorQuotedPoemAsync(quoted, loggedOnUserId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
@@ -3768,8 +3769,9 @@ namespace RMuseum.Controllers
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task UpdateGanjoorQuotedPoemsAsync([FromBody] GanjoorQuotedPoem quoted)
{
+ var loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
RServiceResult res =
- await _ganjoorService.UpdateGanjoorQuotedPoemsAsync(quoted);
+ await _ganjoorService.UpdateGanjoorQuotedPoemsAsync(quoted, loggedOnUserId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok();
@@ -3787,8 +3789,9 @@ namespace RMuseum.Controllers
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task DeleteGanjoorQuotedPoemByIdAsync(Guid id)
{
+ var loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
RServiceResult res =
- await _ganjoorService.DeleteGanjoorQuotedPoemByIdAsync(id);
+ await _ganjoorService.DeleteGanjoorQuotedPoemByIdAsync(id, loggedOnUserId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok();
diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml
index fe08a106..f8a77efa 100644
--- a/RMuseum/RMuseum.xml
+++ b/RMuseum/RMuseum.xml
@@ -15559,25 +15559,28 @@
-
+
insert quoted poem
+
-
+
update quoted poem
+
-
+
delete quoted by id
+
@@ -17573,25 +17576,28 @@
-
+
insert quoted poem
+
-
+
update quoted poem
+
-
+
delete quoted by id
+
diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs
index 4c5bdbcd..7332e0f8 100644
--- a/RMuseum/Services/IGanjoorService.cs
+++ b/RMuseum/Services/IGanjoorService.cs
@@ -1259,21 +1259,24 @@ namespace RMuseum.Services
/// insert quoted poem
///
///
+ ///
///
- Task> InsertGanjoorQuotedPoemAsync(GanjoorQuotedPoem quoted);
+ Task> InsertGanjoorQuotedPoemAsync(GanjoorQuotedPoem quoted, Guid editingUserId);
///
/// update quoted poem
///
///
+ ///
///
- Task> UpdateGanjoorQuotedPoemsAsync(GanjoorQuotedPoem quoted);
+ Task> UpdateGanjoorQuotedPoemsAsync(GanjoorQuotedPoem quoted, Guid editingUserId);
///
/// delete quoted by id
///
///
+ ///
///
- Task> DeleteGanjoorQuotedPoemByIdAsync(Guid id);
+ Task> DeleteGanjoorQuotedPoemByIdAsync(Guid id, Guid editingUserId);
}
}
diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-QuotedPoems.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-QuotedPoems.cs
index 2acad1ca..95a63ab8 100644
--- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-QuotedPoems.cs
+++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-QuotedPoems.cs
@@ -7,7 +7,6 @@ using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services.Implementation;
using System;
using System.Collections.Generic;
-using System.Drawing.Drawing2D;
using System.Linq;
using System.Threading.Tasks;
@@ -18,6 +17,40 @@ namespace RMuseum.Services.Implementation
///
public partial class GanjoorService : IGanjoorService
{
+ public RServiceResult StartRegeneratingRelatedPoemsPageAsync(Guid editingUserId, int poetId, int relatedPoetId)
+ {
+ 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($"StartRegeneratingRelatedPoemsPageAsync({poetId}, {relatedPoetId})", "Query data")).Result;
+
+ try
+ {
+ await _RegenerateRelatedPoemsPageAsync(editingUserId, context, poetId, relatedPoetId);
+ 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());
+ }
+ }
+
///
/// parse related pages
///
@@ -92,6 +125,11 @@ namespace RMuseum.Services.Implementation
}
html += $"
{Environment.NewLine}";
+ if(!string.IsNullOrEmpty(quotedPoem.Note))
+ {
+ html += $"{quotedPoem.Note}
{Environment.NewLine}";
+ }
+
html += $"{Environment.NewLine}";
}
html += $"{Environment.NewLine}";
@@ -154,6 +192,11 @@ namespace RMuseum.Services.Implementation
}
html += $"{Environment.NewLine}";
+ if (!string.IsNullOrEmpty(quotedPoem.Note))
+ {
+ html += $"{quotedPoem.Note}
{Environment.NewLine}";
+ }
+
html += $"{Environment.NewLine}";
}
html += $"{Environment.NewLine}";
@@ -264,8 +307,9 @@ namespace RMuseum.Services.Implementation
/// insert quoted poem
///
///
+ ///
///
- public async Task> InsertGanjoorQuotedPoemAsync(GanjoorQuotedPoem quoted)
+ public async Task> InsertGanjoorQuotedPoemAsync(GanjoorQuotedPoem quoted, Guid editingUserId)
{
try
{
@@ -291,7 +335,14 @@ namespace RMuseum.Services.Implementation
await _context.SaveChangesAsync();
}
-
+ if (quoted.RelatedPoetId != null)
+ {
+ var page = await _context.GanjoorPages.AsNoTracking().Where(p => p.PoetId == quoted.PoetId && p.SecondPoetId == quoted.RelatedPoetId).SingleOrDefaultAsync();
+ if(page != null)
+ {
+ StartRegeneratingRelatedPoemsPageAsync(editingUserId, quoted.PoetId, (int)quoted.RelatedPoetId);
+ }
+ }
return new RServiceResult(quoted);
@@ -306,8 +357,9 @@ namespace RMuseum.Services.Implementation
/// update quoted poem
///
///
+ ///
///
- public async Task> UpdateGanjoorQuotedPoemsAsync(GanjoorQuotedPoem quoted)
+ public async Task> UpdateGanjoorQuotedPoemsAsync(GanjoorQuotedPoem quoted, Guid editingUserId)
{
try
{
@@ -357,6 +409,15 @@ namespace RMuseum.Services.Implementation
await _context.SaveChangesAsync();
+ if (quoted.RelatedPoetId != null)
+ {
+ var page = await _context.GanjoorPages.AsNoTracking().Where(p => p.PoetId == quoted.PoetId && p.SecondPoetId == quoted.RelatedPoetId).SingleOrDefaultAsync();
+ if (page != null)
+ {
+ StartRegeneratingRelatedPoemsPageAsync(editingUserId, quoted.PoetId, (int)quoted.RelatedPoetId);
+ }
+ }
+
return new RServiceResult(true);
}
@@ -370,13 +431,16 @@ namespace RMuseum.Services.Implementation
/// delete quoted by id
///
///
+ ///
///
- public async Task> DeleteGanjoorQuotedPoemByIdAsync(Guid id)
+ public async Task> DeleteGanjoorQuotedPoemByIdAsync(Guid id, Guid editingUserId)
{
try
{
var q = await _context.GanjoorQuotedPoems.Where(q => q.Id == id).SingleAsync();
var poemId = q.PoemId;
+ var poetId = q.PoetId;
+ var relatedPoetId = q.RelatedPoetId;
var relatedPoemId = q.RelatedPoemId;
_context.Remove(q);
await _context.SaveChangesAsync();
@@ -398,6 +462,15 @@ namespace RMuseum.Services.Implementation
await _context.SaveChangesAsync();
}
+ if (relatedPoetId != null)
+ {
+ var page = await _context.GanjoorPages.AsNoTracking().Where(p => p.PoetId == poetId && p.SecondPoetId == relatedPoetId).SingleOrDefaultAsync();
+ if (page != null)
+ {
+ StartRegeneratingRelatedPoemsPageAsync(editingUserId, poetId, (int)relatedPoetId);
+ }
+ }
+
return new RServiceResult(true);