docs: #293 moving BreakPoemAsync implementation to a separate file

This commit is contained in:
Hamid Reza Mohammadi 2022-03-19 09:02:05 +03:30
parent 3d1f632f14
commit e105edf113
3 changed files with 208 additions and 188 deletions

View File

@ -12576,6 +12576,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>
@ -12646,6 +12649,15 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.BreakPoemAsync(System.Int32,System.Int32,System.Guid)">
<summary>
break a poem from a verse forward
</summary>
<param name="poemId"></param>
<param name="vOrder"></param>
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetCenturiesAsync">
<summary>
get centuries with published poets
@ -13275,15 +13287,6 @@
<param name="moderation"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.BreakPoemAsync(System.Int32,System.Int32,System.Guid)">
<summary>
break a poem from a verse forward
</summary>
<param name="poemId"></param>
<param name="vOrder"></param>
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService._GetRandomPoemId(System.Int32,System.Int32)">
<summary>
random poem id from hafez sonnets and old c.ganjoor.net service

View File

@ -0,0 +1,196 @@
using Microsoft.EntityFrameworkCore;
using RMuseum.Models.Ganjoor;
using RSecurityBackend.Models.Generic;
using System;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using DNTPersianUtils.Core;
using RMuseum.Models.Ganjoor.ViewModels;
namespace RMuseum.Services.Implementation
{
/// <summary>
/// IGanjoorService implementation
/// </summary>
public partial class GanjoorService : IGanjoorService
{
/// <summary>
/// break a poem from a verse forward
/// </summary>
/// <param name="poemId"></param>
/// <param name="vOrder"></param>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<RServiceResult<int>> BreakPoemAsync(int poemId, int vOrder, Guid userId)
{
try
{
var dbMainPoem = await _context.GanjoorPoems.Include(p => p.GanjoorMetre).Where(p => p.Id == poemId).SingleOrDefaultAsync();
var dbPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.Id == poemId).SingleOrDefaultAsync();
GanjoorModifyPageViewModel pageViewModel = new GanjoorModifyPageViewModel()
{
Title = dbMainPoem.Title,
UrlSlug = dbMainPoem.UrlSlug,
OldTag = dbMainPoem.OldTag,
OldTagPageUrl = dbMainPoem.OldTagPageUrl,
RhymeLetters = dbMainPoem.RhymeLetters,
Rhythm = dbMainPoem.GanjoorMetre == null ? null : dbMainPoem.GanjoorMetre.Rhythm,
HtmlText = dbMainPoem.HtmlText,
SourceName = dbMainPoem.SourceName,
SourceUrlSlug = dbMainPoem.SourceUrlSlug,
RedirectFromFullUrl = dbPage.RedirectFromFullUrl,
NoIndex = dbPage.NoIndex,
Language = dbMainPoem.Language,
MixedModeOrder = dbMainPoem.MixedModeOrder,
Published = dbMainPoem.Published,
Note = "گام اول شکستن شعر به اشعار مجزا"
};
var res = await UpdatePageAsync(poemId, userId, pageViewModel);
if (!string.IsNullOrEmpty(res.ExceptionString))
return new RServiceResult<int>(0, res.ExceptionString);
var poem = (await GetPoemById(poemId, true, false, true, false, false, false, false, true, true)).Result;
if (poem.Next != null)
return new RServiceResult<int>(-1, "poem.Next != null");
if (poem.UrlSlug.IndexOf("sh") != 0)
return new RServiceResult<int>(-1, "poem.UrlSlug.IndexOf(\"sh\") != 0");
if (!int.TryParse(poem.UrlSlug.Substring("sh".Length), out int slugNumber))
return new RServiceResult<int>(-1, $"slug error: {poem.UrlSlug}");
var poemPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.Id == poem.Id).SingleAsync();
var parentPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.GanjoorPageType == GanjoorPageType.CatPage && p.CatId == poem.Category.Cat.Id).SingleAsync();
string nextPoemUrlSluf = $"sh{slugNumber + 1}";
var maxPoemId = await _context.GanjoorPoems.MaxAsync(p => p.Id);
if (await _context.GanjoorPages.MaxAsync(p => p.Id) > maxPoemId)
maxPoemId = await _context.GanjoorPages.MaxAsync(p => p.Id);
var nextPoemId = 1 + maxPoemId;
string nextPoemTitle = $"بخش {(slugNumber + 1).ToPersianNumbers()}";
GanjoorPoem dbNewPoem = new GanjoorPoem()
{
Id = nextPoemId,
CatId = poem.Category.Cat.Id,
Title = nextPoemTitle,
UrlSlug = nextPoemUrlSluf,
FullTitle = $"{parentPage.FullTitle} » {nextPoemTitle}",
FullUrl = $"{parentPage.FullUrl}/{nextPoemUrlSluf}",
GanjoorMetreId = poem.GanjoorMetre == null ? null : poem.GanjoorMetre.Id,
SourceName = poem.SourceName,
SourceUrlSlug = poem.SourceUrlSlug,
Language = dbMainPoem.Language,
MixedModeOrder = dbMainPoem.MixedModeOrder,
Published = dbMainPoem.Published,
};
var poemVerses = await _context.GanjoorVerses.Where(v => v.PoemId == poemId && v.VOrder >= vOrder).OrderBy(v => v.VOrder).ToListAsync();
for (int i = 0; i < poemVerses.Count; i++)
{
poemVerses[i].VOrder = i + 1;
poemVerses[i].PoemId = nextPoemId;
}
dbNewPoem.PlainText = PreparePlainText(poemVerses);
dbNewPoem.HtmlText = PrepareHtmlText(poemVerses);
try
{
var poemRhymeLettersRes = LanguageUtils.FindRhyme(poemVerses);
if (!string.IsNullOrEmpty(poemRhymeLettersRes.Rhyme))
{
dbNewPoem.RhymeLetters = poemRhymeLettersRes.Rhyme;
}
}
catch
{
}
_context.GanjoorPoems.Add(dbNewPoem);
_context.GanjoorVerses.UpdateRange(poemVerses);
GanjoorPage dbPoemNewPage = new GanjoorPage()
{
Id = nextPoemId,
GanjoorPageType = GanjoorPageType.PoemPage,
Published = true,
PageOrder = -1,
Title = dbNewPoem.Title,
FullTitle = dbNewPoem.FullTitle,
UrlSlug = dbNewPoem.UrlSlug,
FullUrl = dbNewPoem.FullUrl,
HtmlText = dbNewPoem.HtmlText,
PoetId = parentPage.PoetId,
CatId = poem.Category.Cat.Id,
PoemId = nextPoemId,
PostDate = DateTime.Now,
ParentId = parentPage.Id,
};
_context.GanjoorPages.Add(dbPoemNewPage);
await _context.SaveChangesAsync();
GanjoorModifyPageViewModel afterUpdatePageViewModel = new GanjoorModifyPageViewModel()
{
Title = dbMainPoem.Title,
UrlSlug = dbMainPoem.UrlSlug,
OldTag = dbMainPoem.OldTag,
OldTagPageUrl = dbMainPoem.OldTagPageUrl,
RhymeLetters = dbMainPoem.RhymeLetters,
Rhythm = dbMainPoem.GanjoorMetre == null ? null : dbMainPoem.GanjoorMetre.Rhythm,
HtmlText = dbMainPoem.HtmlText,
SourceName = dbMainPoem.SourceName,
SourceUrlSlug = dbMainPoem.SourceUrlSlug,
RedirectFromFullUrl = dbPage.RedirectFromFullUrl,
NoIndex = dbPage.NoIndex,
Language = dbMainPoem.Language,
MixedModeOrder = dbMainPoem.MixedModeOrder,
Published = dbMainPoem.Published,
Note = "گام نهایی شکستن شعر به اشعار مجزا"
};
var afterUpdatePoemVerses = await _context.GanjoorVerses.Where(v => v.PoemId == poemId).OrderBy(v => v.VOrder).ToListAsync();
afterUpdatePageViewModel.HtmlText = PrepareHtmlText(afterUpdatePoemVerses);
try
{
var newRhyme = LanguageUtils.FindRhyme(afterUpdatePoemVerses);
if (!string.IsNullOrEmpty(newRhyme.Rhyme) &&
(string.IsNullOrEmpty(dbMainPoem.RhymeLetters) || (!string.IsNullOrEmpty(dbMainPoem.RhymeLetters) && newRhyme.Rhyme.Length > dbMainPoem.RhymeLetters.Length))
)
{
afterUpdatePageViewModel.RhymeLetters = newRhyme.Rhyme;
}
}
catch
{
}
var res2 = await UpdatePageAsync(poemId, userId, afterUpdatePageViewModel);
if (!string.IsNullOrEmpty(res2.ExceptionString))
return new RServiceResult<int>(0, res2.ExceptionString);
return new RServiceResult<int>(nextPoemId);
}
catch (Exception exp)
{
return new RServiceResult<int>(0, exp.ToString());
}
}
}
}

View File

@ -2068,185 +2068,6 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<GanjoorPoemCorrectionViewModel>(moderation);
}
/// <summary>
/// break a poem from a verse forward
/// </summary>
/// <param name="poemId"></param>
/// <param name="vOrder"></param>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<RServiceResult<int>> BreakPoemAsync(int poemId, int vOrder, Guid userId)
{
try
{
var dbMainPoem = await _context.GanjoorPoems.Include(p => p.GanjoorMetre).Where(p => p.Id == poemId).SingleOrDefaultAsync();
var dbPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.Id == poemId).SingleOrDefaultAsync();
GanjoorModifyPageViewModel pageViewModel = new GanjoorModifyPageViewModel()
{
Title = dbMainPoem.Title,
UrlSlug = dbMainPoem.UrlSlug,
OldTag = dbMainPoem.OldTag,
OldTagPageUrl = dbMainPoem.OldTagPageUrl,
RhymeLetters = dbMainPoem.RhymeLetters,
Rhythm = dbMainPoem.GanjoorMetre == null ? null : dbMainPoem.GanjoorMetre.Rhythm,
HtmlText = dbMainPoem.HtmlText,
SourceName = dbMainPoem.SourceName,
SourceUrlSlug = dbMainPoem.SourceUrlSlug,
RedirectFromFullUrl = dbPage.RedirectFromFullUrl,
NoIndex = dbPage.NoIndex,
Language = dbMainPoem.Language,
MixedModeOrder = dbMainPoem.MixedModeOrder,
Published = dbMainPoem.Published,
Note = "گام اول شکستن شعر به اشعار مجزا"
};
var res = await UpdatePageAsync(poemId, userId, pageViewModel);
if (!string.IsNullOrEmpty(res.ExceptionString))
return new RServiceResult<int>(0, res.ExceptionString);
var poem = (await GetPoemById(poemId, true, false, true, false, false, false, false, true, true)).Result;
if (poem.Next != null)
return new RServiceResult<int>(-1, "poem.Next != null");
if (poem.UrlSlug.IndexOf("sh") != 0)
return new RServiceResult<int>(-1, "poem.UrlSlug.IndexOf(\"sh\") != 0");
if (!int.TryParse(poem.UrlSlug.Substring("sh".Length), out int slugNumber))
return new RServiceResult<int>(-1, $"slug error: {poem.UrlSlug}");
var poemPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.Id == poem.Id).SingleAsync();
var parentPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.GanjoorPageType == GanjoorPageType.CatPage && p.CatId == poem.Category.Cat.Id).SingleAsync();
string nextPoemUrlSluf = $"sh{slugNumber + 1}";
var maxPoemId = await _context.GanjoorPoems.MaxAsync(p => p.Id);
if (await _context.GanjoorPages.MaxAsync(p => p.Id) > maxPoemId)
maxPoemId = await _context.GanjoorPages.MaxAsync(p => p.Id);
var nextPoemId = 1 + maxPoemId;
string nextPoemTitle = $"بخش {(slugNumber + 1).ToPersianNumbers()}";
GanjoorPoem dbNewPoem = new GanjoorPoem()
{
Id = nextPoemId,
CatId = poem.Category.Cat.Id,
Title = nextPoemTitle,
UrlSlug = nextPoemUrlSluf,
FullTitle = $"{parentPage.FullTitle} » {nextPoemTitle}",
FullUrl = $"{parentPage.FullUrl}/{nextPoemUrlSluf}",
GanjoorMetreId = poem.GanjoorMetre == null ? null : poem.GanjoorMetre.Id,
SourceName = poem.SourceName,
SourceUrlSlug = poem.SourceUrlSlug,
Language = dbMainPoem.Language,
MixedModeOrder = dbMainPoem.MixedModeOrder,
Published = dbMainPoem.Published,
};
var poemVerses = await _context.GanjoorVerses.Where(v => v.PoemId == poemId && v.VOrder >= vOrder).OrderBy(v => v.VOrder).ToListAsync();
for (int i = 0; i < poemVerses.Count; i++)
{
poemVerses[i].VOrder = i + 1;
poemVerses[i].PoemId = nextPoemId;
}
dbNewPoem.PlainText = PreparePlainText(poemVerses);
dbNewPoem.HtmlText = PrepareHtmlText(poemVerses);
try
{
var poemRhymeLettersRes = LanguageUtils.FindRhyme(poemVerses);
if (!string.IsNullOrEmpty(poemRhymeLettersRes.Rhyme))
{
dbNewPoem.RhymeLetters = poemRhymeLettersRes.Rhyme;
}
}
catch
{
}
_context.GanjoorPoems.Add(dbNewPoem);
_context.GanjoorVerses.UpdateRange(poemVerses);
GanjoorPage dbPoemNewPage = new GanjoorPage()
{
Id = nextPoemId,
GanjoorPageType = GanjoorPageType.PoemPage,
Published = true,
PageOrder = -1,
Title = dbNewPoem.Title,
FullTitle = dbNewPoem.FullTitle,
UrlSlug = dbNewPoem.UrlSlug,
FullUrl = dbNewPoem.FullUrl,
HtmlText = dbNewPoem.HtmlText,
PoetId = parentPage.PoetId,
CatId = poem.Category.Cat.Id,
PoemId = nextPoemId,
PostDate = DateTime.Now,
ParentId = parentPage.Id,
};
_context.GanjoorPages.Add(dbPoemNewPage);
await _context.SaveChangesAsync();
GanjoorModifyPageViewModel afterUpdatePageViewModel = new GanjoorModifyPageViewModel()
{
Title = dbMainPoem.Title,
UrlSlug = dbMainPoem.UrlSlug,
OldTag = dbMainPoem.OldTag,
OldTagPageUrl = dbMainPoem.OldTagPageUrl,
RhymeLetters = dbMainPoem.RhymeLetters,
Rhythm = dbMainPoem.GanjoorMetre == null ? null : dbMainPoem.GanjoorMetre.Rhythm,
HtmlText = dbMainPoem.HtmlText,
SourceName = dbMainPoem.SourceName,
SourceUrlSlug = dbMainPoem.SourceUrlSlug,
RedirectFromFullUrl = dbPage.RedirectFromFullUrl,
NoIndex = dbPage.NoIndex,
Language = dbMainPoem.Language,
MixedModeOrder = dbMainPoem.MixedModeOrder,
Published = dbMainPoem.Published,
Note = "گام نهایی شکستن شعر به اشعار مجزا"
};
var afterUpdatePoemVerses = await _context.GanjoorVerses.Where(v => v.PoemId == poemId).OrderBy(v => v.VOrder).ToListAsync();
afterUpdatePageViewModel.HtmlText = PrepareHtmlText(afterUpdatePoemVerses);
try
{
var newRhyme = LanguageUtils.FindRhyme(afterUpdatePoemVerses);
if (!string.IsNullOrEmpty(newRhyme.Rhyme) &&
(string.IsNullOrEmpty(dbMainPoem.RhymeLetters) || (!string.IsNullOrEmpty(dbMainPoem.RhymeLetters) && newRhyme.Rhyme.Length > dbMainPoem.RhymeLetters.Length))
)
{
afterUpdatePageViewModel.RhymeLetters = newRhyme.Rhyme;
}
}
catch
{
}
var res2 = await UpdatePageAsync(poemId, userId, afterUpdatePageViewModel);
if (!string.IsNullOrEmpty(res2.ExceptionString))
return new RServiceResult<int>(0, res2.ExceptionString);
return new RServiceResult<int>(nextPoemId);
}
catch (Exception exp)
{
return new RServiceResult<int>(0, exp.ToString());
}
}
/// <summary>
/// random poem id from hafez sonnets and old c.ganjoor.net service
/// </summary>