From e105edf113d5a4ba0e8ec0f0de8a2fe43f123f51 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 19 Mar 2022 09:02:05 +0330 Subject: [PATCH] docs: #293 moving BreakPoemAsync implementation to a separate file --- RMuseum/RMuseum.xml | 21 +- .../GanjoorService-BreakPoem.cs | 196 ++++++++++++++++++ .../Services/Implementation/GanjoorService.cs | 179 ---------------- 3 files changed, 208 insertions(+), 188 deletions(-) create mode 100644 RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index af539158..d1948a97 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -12576,6 +12576,9 @@ IGanjoorService implementation + + IGanjoorService implementation + @@ -12646,6 +12649,15 @@ + + + break a poem from a verse forward + + + + + + get centuries with published poets @@ -13275,15 +13287,6 @@ - - - break a poem from a verse forward - - - - - - random poem id from hafez sonnets and old c.ganjoor.net service diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs new file mode 100644 index 00000000..01f8fdeb --- /dev/null +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs @@ -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 +{ + /// + /// IGanjoorService implementation + /// + public partial class GanjoorService : IGanjoorService + { + /// + /// break a poem from a verse forward + /// + /// + /// + /// + /// + public async Task> 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(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(-1, "poem.Next != null"); + + if (poem.UrlSlug.IndexOf("sh") != 0) + return new RServiceResult(-1, "poem.UrlSlug.IndexOf(\"sh\") != 0"); + + if (!int.TryParse(poem.UrlSlug.Substring("sh".Length), out int slugNumber)) + return new RServiceResult(-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(0, res2.ExceptionString); + + + + return new RServiceResult(nextPoemId); + } + catch (Exception exp) + { + return new RServiceResult(0, exp.ToString()); + } + } + } +} \ No newline at end of file diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 4aa39b92..f765b3c0 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -2068,185 +2068,6 @@ namespace RMuseum.Services.Implementation return new RServiceResult(moderation); } - /// - /// break a poem from a verse forward - /// - /// - /// - /// - /// - public async Task> 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(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(-1, "poem.Next != null"); - - if (poem.UrlSlug.IndexOf("sh") != 0) - return new RServiceResult(-1, "poem.UrlSlug.IndexOf(\"sh\") != 0"); - - if (!int.TryParse(poem.UrlSlug.Substring("sh".Length), out int slugNumber)) - return new RServiceResult(-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(0, res2.ExceptionString); - - - - return new RServiceResult(nextPoemId); - } - catch (Exception exp) - { - return new RServiceResult(0, exp.ToString()); - } - } - - /// /// random poem id from hafez sonnets and old c.ganjoor.net service ///