diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs index 3becb9ff..04554d8b 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs @@ -166,182 +166,219 @@ namespace RMuseum.Services.Implementation { return await _GenerateTableOfContents(_context, catId, options); } - - private async Task> _GenerateTableOfContents(RMuseumDbContext context, int catId, GanjoorTOC options) { - string html = ""; - var subCats = await context.GanjoorCategories.Where(c => c.ParentId == catId).OrderBy(c => c.Id).AsNoTracking().ToArrayAsync(); - - foreach (var cat in subCats) + try { - html += $"

{cat.Title}

{Environment.NewLine}"; - } + var cat = await context.GanjoorCategories.AsNoTracking().Where(c => c.Id == catId).SingleAsync(); + string html = string.IsNullOrEmpty(cat.DescriptionHtml) ? "" : cat.DescriptionHtml; + var subCats = await context.GanjoorCategories.AsNoTracking().Where(c => c.ParentId == catId).OrderBy(c => c.MixedModeOrder).ThenBy(c => c.Id) .ToListAsync(); + var poems = await context.GanjoorPoems.AsNoTracking().Where(p => p.CatId == catId).OrderBy(p => p.MixedModeOrder).ThenBy(p => p.Id).ToListAsync(); - var poems = await context.GanjoorPoems.Where(p => p.CatId == catId).OrderBy(p => p.Id).AsNoTracking().ToArrayAsync(); - - if (poems.Length > 0) - { - if (options == GanjoorTOC.Analyse) + if (subCats.Where(c => c.MixedModeOrder != 0).Any() || poems.Where(p => p.MixedModeOrder != 0).Any())//ignore options parameter { - if (poems.Where(p => !string.IsNullOrEmpty(p.RhymeLetters)).Count() * 100 / poems.Length > 50) + if (subCats.Where(c => c.MixedModeOrder == 0).Any() || poems.Where(p => p.MixedModeOrder == 0).Any()) { - options = GanjoorTOC.AlphabeticWithFirstVerse; + return new RServiceResult(null, "subCats.Where(c => c.MixedModeOrder == 0).Any() || poems.Where(p => p.MixedModeOrder == 0).Any()"); } - else + + int nMixedModeOrder = 1; + while (subCats.Where(c => c.MixedModeOrder == nMixedModeOrder).Any() || poems.Where(p => p.MixedModeOrder == nMixedModeOrder).Any()) { - options = GanjoorTOC.TitlesAndFirstVerse; + var subCatWithThisMixedOrder = subCats.Where(c => c.MixedModeOrder == nMixedModeOrder).ToArray(); + foreach (var subCat in subCatWithThisMixedOrder) + { + html += $"
{Environment.NewLine}"; + html += $"{subCat.Title}{Environment.NewLine}"; + html += $"
{Environment.NewLine}"; + } + + var poemsWithThisMixedOrder = poems.Where(c => c.MixedModeOrder == nMixedModeOrder).ToArray(); + foreach (var poem in poemsWithThisMixedOrder) + { + html += $"
{Environment.NewLine}"; + html += $"{poem.Title}{Environment.NewLine}"; + html += $"
{Environment.NewLine}"; + } + + nMixedModeOrder++; + } + + return new RServiceResult(html); + } + + foreach (var subCat in subCats) + { + html += $"
{Environment.NewLine}"; + html += $"{subCat.Title}{Environment.NewLine}"; + html += $"
{Environment.NewLine}"; + } + + if (poems.Count > 0) + { + if (options == GanjoorTOC.Analyse) + { + if (poems.Where(p => !string.IsNullOrEmpty(p.RhymeLetters)).Count() * 100 / poems.Count > 50) + { + options = GanjoorTOC.AlphabeticWithFirstVerse; + } + else + { + options = GanjoorTOC.TitlesAndFirstVerse; + } } } - } - if - ( - options == GanjoorTOC.AlphabeticWithFirstCouplet - || - options == GanjoorTOC.AlphabeticWithFirstVerse - || - options == GanjoorTOC.AlphabeticWithSecondVerse - ) - { - var taggedPoems = poems.Where(p => !string.IsNullOrEmpty(p.RhymeLetters)).ToArray(); - if (taggedPoems.Length > 0) + if + ( + options == GanjoorTOC.AlphabeticWithFirstCouplet + || + options == GanjoorTOC.AlphabeticWithFirstVerse + || + options == GanjoorTOC.AlphabeticWithSecondVerse + ) { - html += $"

فهرست شعرها به ترتیب آخر حرف قافیه گردآوری شده است. برای پیدا کردن یک شعر کافی است حرف آخر قافیهٔ آن را در نظر بگیرید تا بتوانید آن را پیدا کنید.

{Environment.NewLine}"; - var randomPoem = taggedPoems[new Random(DateTime.Now.Millisecond).Next(taggedPoems.Length)]; - var randomPoemVerses = await context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == randomPoem.Id).OrderBy(v => v.VOrder).ToArrayAsync(); - if (randomPoemVerses.Length > 2) + var taggedPoems = poems.Where(p => !string.IsNullOrEmpty(p.RhymeLetters)).ToArray(); + if (taggedPoems.Length > 0) { - html += $"

مثلاً برای پیدا کردن شعری که مصرع {randomPoemVerses[1].Text} مصرع دوم یکی از بیتهای آن است باید شعرهایی را نگاه کنید که آخر حرف قافیهٔ آنها «{randomPoem.RhymeLetters.Substring(randomPoem.RhymeLetters.Length - 1)}» است.

{Environment.NewLine}"; - } - - html += $"

دسترسی سریع به حروف

{Environment.NewLine}"; - html += $"

{Environment.NewLine}"; - string lastChar = ""; - List visitedLastChart = new List(); - foreach (var poem in taggedPoems) - { - string poemLastChar = poem.RhymeLetters.Substring(poem.RhymeLetters.Length - 1); - if (poemLastChar != lastChar) + html += $"

فهرست شعرها به ترتیب آخر حرف قافیه گردآوری شده است. برای پیدا کردن یک شعر کافی است حرف آخر قافیهٔ آن را در نظر بگیرید تا بتوانید آن را پیدا کنید.

{Environment.NewLine}"; + var randomPoem = taggedPoems[new Random(DateTime.Now.Millisecond).Next(taggedPoems.Length)]; + var randomPoemVerses = await context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == randomPoem.Id).OrderBy(v => v.VOrder).ToArrayAsync(); + if (randomPoemVerses.Length > 2) { - if (visitedLastChart.IndexOf(poemLastChar) == -1) - { - if (lastChar != "") - { - html += " | "; - } - html += $"{poemLastChar}"; - lastChar = poemLastChar; + html += $"

مثلاً برای پیدا کردن شعری که مصرع {randomPoemVerses[1].Text} مصرع دوم یکی از بیتهای آن است باید شعرهایی را نگاه کنید که آخر حرف قافیهٔ آنها «{randomPoem.RhymeLetters.Substring(randomPoem.RhymeLetters.Length - 1)}» است.

{Environment.NewLine}"; + } - visitedLastChart.Add(poemLastChar); + html += $"

دسترسی سریع به حروف

{Environment.NewLine}"; + html += $"

{Environment.NewLine}"; + string lastChar = ""; + List visitedLastChart = new List(); + foreach (var poem in taggedPoems) + { + string poemLastChar = poem.RhymeLetters.Substring(poem.RhymeLetters.Length - 1); + if (poemLastChar != lastChar) + { + if (visitedLastChart.IndexOf(poemLastChar) == -1) + { + if (lastChar != "") + { + html += " | "; + } + html += $"{poemLastChar}"; + lastChar = poemLastChar; + + visitedLastChart.Add(poemLastChar); + } + } + } + html += $"

{Environment.NewLine}"; + } + } + + string last = ""; + List visitedLast = new List(); + foreach (var poem in poems) + { + if + ( + options == GanjoorTOC.AlphabeticWithFirstCouplet + || + options == GanjoorTOC.AlphabeticWithFirstVerse + || + options == GanjoorTOC.AlphabeticWithSecondVerse + ) + { + + if (!string.IsNullOrEmpty(poem.RhymeLetters)) + { + string poemLast = poem.RhymeLetters.Substring(poem.RhymeLetters.Length - 1); + if (poemLast != last) + { + if (visitedLast.IndexOf(poemLast) == -1) + { + html += $"

{poemLast}

{Environment.NewLine}"; + last = poemLast; + visitedLast.Add(poemLast); + } } } } + + html += $"

{poem.Title}"; + + var verses = await context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == poem.Id).OrderBy(v => v.VOrder).ToArrayAsync(); + + if (verses.Length > 0) + { + if (options == GanjoorTOC.TitlesAndFirstVerse || options == GanjoorTOC.AlphabeticWithFirstVerse) + { + html += $": {verses[0].Text}"; + } + else + if (options == GanjoorTOC.AlphabeticWithSecondVerse || options == GanjoorTOC.TitlesAndSecondVerse) + { + if (verses.Length > 1) + { + html += $": {verses[1].Text}"; + } + else + { + html += $": {verses[0].Text}"; + } + } + else + if (options == GanjoorTOC.AlphabeticWithFirstCouplet || options == GanjoorTOC.TitlesAndFirstCouplet) + { + if (verses.Length > 1) + { + html += $": {verses[0].Text} - {verses[1].Text}"; + } + else + { + html += $": {verses[0].Text}"; + } + } + else + if (options == GanjoorTOC.TitlesAndFirstCenteredVerse) + { + if (verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).Any()) + { + html += $": {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).First().Text}"; + } + else + { + html += $": {verses[0].Text}"; + } + } + else + if (options == GanjoorTOC.TitlesAndFirstCenteredCouplet) + { + if ( + verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).Any() + && + verses.Where(v => v.VersePosition == VersePosition.CenteredVerse2).Any() + ) + { + html += $": {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).First().Text} - {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse2).First().Text}"; + } + else + { + html += $": {verses[0].Text}"; + } + } + } + + html += $"

{Environment.NewLine}"; } + return new RServiceResult(html); } - - string last = ""; - List visitedLast = new List(); - foreach (var poem in poems) + catch (Exception exp) { - if - ( - options == GanjoorTOC.AlphabeticWithFirstCouplet - || - options == GanjoorTOC.AlphabeticWithFirstVerse - || - options == GanjoorTOC.AlphabeticWithSecondVerse - ) - { - - if (!string.IsNullOrEmpty(poem.RhymeLetters)) - { - string poemLast = poem.RhymeLetters.Substring(poem.RhymeLetters.Length - 1); - if (poemLast != last) - { - if (visitedLast.IndexOf(poemLast) == -1) - { - html += $"

{poemLast}

{Environment.NewLine}"; - last = poemLast; - visitedLast.Add(poemLast); - } - } - } - } - - html += $"

{poem.Title}"; - - var verses = await context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == poem.Id).OrderBy(v => v.VOrder).ToArrayAsync(); - - if (verses.Length > 0) - { - if (options == GanjoorTOC.TitlesAndFirstVerse || options == GanjoorTOC.AlphabeticWithFirstVerse) - { - html += $": {verses[0].Text}"; - } - else - if (options == GanjoorTOC.AlphabeticWithSecondVerse || options == GanjoorTOC.TitlesAndSecondVerse) - { - if (verses.Length > 1) - { - html += $": {verses[1].Text}"; - } - else - { - html += $": {verses[0].Text}"; - } - } - else - if (options == GanjoorTOC.AlphabeticWithFirstCouplet || options == GanjoorTOC.TitlesAndFirstCouplet) - { - if (verses.Length > 1) - { - html += $": {verses[0].Text} - {verses[1].Text}"; - } - else - { - html += $": {verses[0].Text}"; - } - } - else - if (options == GanjoorTOC.TitlesAndFirstCenteredVerse) - { - if (verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).Any()) - { - html += $": {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).First().Text}"; - } - else - { - html += $": {verses[0].Text}"; - } - } - else - if (options == GanjoorTOC.TitlesAndFirstCenteredCouplet) - { - if ( - verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).Any() - && - verses.Where(v => v.VersePosition == VersePosition.CenteredVerse2).Any() - ) - { - html += $": {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse1).First().Text} - {verses.Where(v => v.VersePosition == VersePosition.CenteredVerse2).First().Text}"; - } - else - { - html += $": {verses[0].Text}"; - } - } - } - - - html += $"

{Environment.NewLine}"; + return new RServiceResult(null, exp.ToString()); } - return new RServiceResult(html); } - - /// /// make plain text ///