docs: #306 adding GanjoorCachedRelatedSection

This commit is contained in:
Hamid Reza Mohammadi 2022-04-14 11:36:57 +04:30
parent 7259ebab99
commit c949373eca
6 changed files with 289 additions and 8 deletions

View File

@ -462,5 +462,10 @@ namespace RMuseum.DbContext
/// </summary>
public DbSet<GanjoorPoemSection> GanjoorPoemSections { get; set; }
/// <summary>
/// related sections to each section (having same rhyme letters and prosody metre)
/// </summary>
public DbSet<GanjoorCachedRelatedSection> GanjoorCachedRelatedSections { get; set; }
}
}

View File

@ -0,0 +1,65 @@
namespace RMuseum.Models.Ganjoor
{
public class GanjoorCachedRelatedSection
{
/// <summary>
/// id
/// </summary>
public int Id { get; set; }
/// <summary>
/// poem id
/// </summary>
public int PoemId { get; set; }
/// <summary>
/// poem
/// </summary>
public GanjoorPoem Poem { get; set; }
/// <summary>
/// section index
/// </summary>
public int SectionIndex { get; set; }
/// <summary>
/// poet id
/// </summary>
public int PoetId { get; set; }
/// <summary>
/// order
/// </summary>
public int RelationOrder { get; set; }
/// <summary>
/// poet name
/// </summary>
public string PoetName { get; set; }
/// <summary>
/// poet image url
/// </summary>
public string PoetImageUrl { get; set; }
/// <summary>
/// poem full url
/// </summary>
public string FullUrl { get; set; }
/// <summary>
/// poem full title
/// </summary>
public string FullTitle { get; set; }
/// <summary>
/// excerpt
/// </summary>
public string HtmlExcerpt { get; set; }
/// <summary>
/// other poems
/// </summary>
public int PoetMorePoemsLikeThisCount { get; set; }
}
}

View File

@ -2871,6 +2871,11 @@
poem sections
</summary>
</member>
<member name="P:RMuseum.DbContext.RMuseumDbContext.GanjoorCachedRelatedSections">
<summary>
related sections to each section (having same rhyme letters and prosody metre)
</summary>
</member>
<member name="T:RMuseum.Models.Accounting.DonationExpenditure">
<summary>
donation expenditures
@ -5750,6 +5755,66 @@
other poems
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.Id">
<summary>
id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.PoemId">
<summary>
poem id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.Poem">
<summary>
poem
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.SectionIndex">
<summary>
section index
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.PoetId">
<summary>
poet id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.RelationOrder">
<summary>
order
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.PoetName">
<summary>
poet name
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.PoetImageUrl">
<summary>
poet image url
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.FullUrl">
<summary>
poem full url
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.FullTitle">
<summary>
poem full title
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.HtmlExcerpt">
<summary>
excerpt
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedSection.PoetMorePoemsLikeThisCount">
<summary>
other poems
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.GanjoorCat">
<summary>
Ganjoor Category
@ -10904,7 +10969,7 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemSections(System.Int32)">
<member name="M:RMuseum.Services.IGanjoorService.GetPoemWholeSections(System.Int32)">
<summary>
get poem sections
</summary>
@ -13049,6 +13114,9 @@
<summary>
IGanjoorService implementation
</summary>
<summary>
IGanjoorService implementation
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.SwitchCoupletBookmark(System.Guid,System.Int32,System.Int32)">
<summary>
@ -13330,6 +13398,25 @@
<param name="regenerate"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetRelatedSections(System.Int32,System.Int32,System.Int32,System.Int32)">
<summary>
get a section related sections
</summary>
<param name="poemId">poem id</param>
<param name="sectionIndex">section index</param>
<param name="skip"></param>
<param name="itemsCount">if sent 0 or less returns all items</param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService._UpdateRelatedSections(RMuseum.DbContext.RMuseumDbContext,System.Int32,System.String)">
<summary>
update related sections info (after metreId or rhyme for one of these sections changes)
</summary>
<param name="context"></param>
<param name="metreId"></param>
<param name="rhyme"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartBuildingSitemap">
<summary>
build sitemap
@ -13580,9 +13667,9 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemSections(System.Int32)">
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemWholeSections(System.Int32)">
<summary>
get poem sections
get poem whole sections
</summary>
<param name="id"></param>
<returns></returns>

View File

@ -142,7 +142,7 @@ namespace RMuseum.Services
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorPoemSection[]>> GetPoemSections(int id);
Task<RServiceResult<GanjoorPoemSection[]>> GetPoemWholeSections(int id);
/// <summary>
/// get user up votes for the recitations of a poem

View File

@ -0,0 +1,124 @@
using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Ganjoor;
using RSecurityBackend.Models.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace RMuseum.Services.Implementation
{
/// <summary>
/// IGanjoorService implementation
/// </summary>
public partial class GanjoorService : IGanjoorService
{
/// <summary>
/// get a section related sections
/// </summary>
/// <param name="poemId">poem id</param>
/// <param name="sectionIndex">section index</param>
/// <param name="skip"></param>
/// <param name="itemsCount">if sent 0 or less returns all items</param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorCachedRelatedSection[]>> GetRelatedSections(int poemId, int sectionIndex, int skip, int itemsCount)
{
var source =
_context.GanjoorCachedRelatedSections
.Where(r => r.PoemId == poemId && r.SectionIndex == sectionIndex)
.OrderBy(r => r.RelationOrder);
if (itemsCount <= 0)
return new RServiceResult<GanjoorCachedRelatedSection[]>(await source.ToArrayAsync());
return new RServiceResult<GanjoorCachedRelatedSection[]>
(
await source.Skip(skip).Take(itemsCount).ToArrayAsync()
);
}
/// <summary>
/// update related sections info (after metreId or rhyme for one of these sections changes)
/// </summary>
/// <param name="context"></param>
/// <param name="metreId"></param>
/// <param name="rhyme"></param>
/// <returns></returns>
private async Task<RServiceResult<bool>> _UpdateRelatedSections(RMuseumDbContext context, int metreId, string rhyme)
{
try
{
var sectionIds = await context.GanjoorPoemSections.AsNoTracking().Where(section => section.GanjoorMetreId == metreId && section.RhymeLetters == rhyme).Select(p => p.Id).ToListAsync();
foreach (var id in sectionIds)
{
await _UpdateSectionRelatedSectionsInfoNoSaveChanges(context, id);
}
await context.SaveChangesAsync();
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
private async Task _UpdateSectionRelatedSectionsInfoNoSaveChanges(RMuseumDbContext context, int sectionId)
{
var section = await context.GanjoorPoemSections.AsNoTracking().Where(p => p.Id == sectionId).SingleAsync();
var oldRelations = await context.GanjoorCachedRelatedSections.Where(r => r.PoemId == section.PoemId && r.SectionIndex == section.Index).ToListAsync();
context.GanjoorCachedRelatedSections.RemoveRange(oldRelations);
int metreId = (int)section.GanjoorMetreId;
string rhyme = section.RhymeLetters;
var relatedSections = await context.GanjoorPoemSections.AsNoTracking().Include(section => section.Poem).Include(section => section.Poet)
.Where(s =>
s.GanjoorMetreId == metreId
&&
s.RhymeLetters == rhyme
&&
((s.PoemId == section.PoemId && s.Index != section.Index ) || (s.PoemId != section.PoemId))
)
.OrderBy(p => p.Poet.BirthYearInLHijri).ThenBy(p => p.PoetId).ToListAsync();
List<GanjoorCachedRelatedSection> GanjoorCachedRelatedSections = new List<GanjoorCachedRelatedSection>();
int r = 0;
int prePoetId = -1;
foreach (var relatedSection in relatedSections)
{
if (prePoetId != relatedSection.PoetId)
{
r++;
GanjoorCachedRelatedSection newRelatedPoem = new GanjoorCachedRelatedSection()
{
PoemId = relatedSection.PoemId,
SectionIndex = relatedSection.Index,
PoetId = (int)relatedSection.PoetId,
RelationOrder = r,
PoetName = relatedSection.Poet.Nickname,
PoetImageUrl = $"/api/ganjoor/poet/image{(await context.GanjoorCategories.Where(c => c.ParentId == null && c.PoetId == relatedSection.PoetId).AsNoTracking().SingleAsync()).FullUrl}.gif",
FullTitle = relatedSection.Poem.FullTitle,
FullUrl = relatedSection.Poem.FullUrl,
PoetMorePoemsLikeThisCount = 0,
HtmlExcerpt = GetPoemHtmlExcerpt(relatedSection.HtmlText)
};
GanjoorCachedRelatedSections.Add(newRelatedPoem);
prePoetId = (int)relatedSection.PoetId;
}
else
{
GanjoorCachedRelatedSections[GanjoorCachedRelatedSections.Count - 1].PoetMorePoemsLikeThisCount++;
}
}
if (GanjoorCachedRelatedSections.Count > 0)
{
context.GanjoorCachedRelatedSections.AddRange(GanjoorCachedRelatedSections);
}
}
}
}

View File

@ -639,14 +639,14 @@ namespace RMuseum.Services.Implementation
/// <summary>
/// get poem sections
/// get poem whole sections
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoemSection[]>> GetPoemSections(int id)
public async Task<RServiceResult<GanjoorPoemSection[]>> GetPoemWholeSections(int id)
{
return new RServiceResult<GanjoorPoemSection[]>(
await _context.GanjoorPoemSections.AsNoTracking().Include(s => s.GanjoorMetre).Where(s => s.PoemId == id).ToArrayAsync()
await _context.GanjoorPoemSections.AsNoTracking().Include(s => s.GanjoorMetre).Where(s => s.PoemId == id && s.SectionType == PoemSectionType.WholePoem).ToArrayAsync()
);
}
@ -1622,7 +1622,7 @@ namespace RMuseum.Services.Implementation
GanjoorPoemSection[] poemSections = null;
if(sections)
{
var poemSectionsRes = await GetPoemSections(id);
var poemSectionsRes = await GetPoemWholeSections(id);
if (!string.IsNullOrEmpty(poemSectionsRes.ExceptionString))
return new RServiceResult<GanjoorPoemCompleteViewModel>(null, poemSectionsRes.ExceptionString);
poemSections = poemSectionsRes.Result;