From 3b9a028cea479e65408a271e32e7e1cb0c88a28b Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 18 Apr 2022 20:01:57 +0430 Subject: [PATCH] docs: #306 trying to make ModeratePoemCorrection indepenent from _UpdatePageAsync (in progress) --- RMuseum/RMuseum.xml | 19 +- .../GanjoorService-ModeratePoemCorrection.cs | 194 ++++++++++++++++++ .../Services/Implementation/GanjoorService.cs | 123 ----------- 3 files changed, 205 insertions(+), 131 deletions(-) create mode 100644 RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ModeratePoemCorrection.cs diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index f47f68d7..bbafaf1b 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -13154,6 +13154,9 @@ IGanjoorService implementation + + IGanjoorService implementation + @@ -13311,6 +13314,14 @@ + + + moderate poem correction + + + + + start updating stats page @@ -13928,14 +13939,6 @@ - - - moderate poem correction - - - - - random poem id from hafez sonnets and old c.ganjoor.net service diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ModeratePoemCorrection.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ModeratePoemCorrection.cs new file mode 100644 index 00000000..36d0e959 --- /dev/null +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ModeratePoemCorrection.cs @@ -0,0 +1,194 @@ +using Microsoft.EntityFrameworkCore; +using RMuseum.Models.Ganjoor; +using RMuseum.Models.Ganjoor.ViewModels; +using RSecurityBackend.Models.Generic; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; + +namespace RMuseum.Services.Implementation +{ + /// + /// IGanjoorService implementation + /// + public partial class GanjoorService : IGanjoorService + { + + /// + /// moderate poem correction + /// + /// + /// + /// + public async Task> ModeratePoemCorrection(Guid userId, + GanjoorPoemCorrectionViewModel moderation) + { + var dbCorrection = await _context.GanjoorPoemCorrections.Include(c => c.VerseOrderText).Include(c => c.User) + .Where(c => c.Id == moderation.Id) + .FirstOrDefaultAsync(); + + dbCorrection.ReviewerUserId = userId; + dbCorrection.ReviewDate = DateTime.Now; + dbCorrection.ApplicationOrder = await _context.GanjoorPoemCorrections.Where(c => c.Reviewed).AnyAsync() ? 1 + await _context.GanjoorPoemCorrections.Where(c => c.Reviewed).MaxAsync(c => c.ApplicationOrder) : 1; + dbCorrection.Reviewed = true; + dbCorrection.AffectedThePoem = false; + + if (dbCorrection == null) + return new RServiceResult(null); + + var dbPoem = await _context.GanjoorPoems.Include(p => p.GanjoorMetre).Where(p => p.Id == moderation.PoemId).SingleOrDefaultAsync(); + var dbPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.Id == moderation.PoemId).SingleOrDefaultAsync(); + + bool updatePoem = false; + if (dbCorrection.Title != null) + { + if (moderation.Result == CorrectionReviewResult.NotReviewed) + return new RServiceResult(null, "تغییرات عنوان بررسی نشده است."); + dbCorrection.Result = moderation.Result; + dbCorrection.ReviewNote = moderation.ReviewNote; + if (dbCorrection.Result == CorrectionReviewResult.Approved) + { + dbCorrection.AffectedThePoem = true; + dbPoem.Title = moderation.Title.Replace("ۀ", "هٔ").Replace("ك", "ک"); + updatePoem = true; + } + } + + var sections = await _context.GanjoorPoemSections.Where(s => s.PoemId == moderation.PoemId).OrderBy(s => s.SectionType).ThenBy(s => s.Index).ToListAsync(); + + + bool metreChanged = false; + if (dbCorrection.Rhythm != null) + { + if (moderation.RhythmResult == CorrectionReviewResult.NotReviewed) + return new RServiceResult(null, "تغییرات وزن بررسی نشده است."); + dbCorrection.RhythmResult = moderation.RhythmResult; + dbCorrection.ReviewNote = moderation.ReviewNote; + if (dbCorrection.RhythmResult == CorrectionReviewResult.Approved) + { + dbCorrection.AffectedThePoem = true; + var mainSection = sections.First(s => s.SectionType == PoemSectionType.WholePoem && s.VerseType == VersePoemSectionType.First); + if (mainSection == null) + { + return new RServiceResult(null, "شعر فاقد بخش اصلی برای ذخیرهٔ وزن است."); + } + var metre = await _context.GanjoorMetres.AsNoTracking().Where(m => m.Rhythm == moderation.Rhythm).SingleOrDefaultAsync(); + if (metre == null) + { + metre = new GanjoorMetre() + { + Rhythm = moderation.Rhythm, + VerseCount = 0 + }; + _context.GanjoorMetres.Add(metre); + await _context.SaveChangesAsync(); + } + metreChanged = mainSection.GanjoorMetreId != metre.Id; + mainSection.GanjoorMetreId = metre.Id; + } + } + + if (moderation.VerseOrderText.Length != dbCorrection.VerseOrderText.Count) + return new RServiceResult(null, "moderation.VerseOrderText.Length != dbCorrection.VerseOrderText.Count"); + + var poemVerses = await _context.GanjoorVerses.Where(p => p.PoemId == dbCorrection.PoemId).OrderBy(v => v.VOrder).ToListAsync(); + + var modifiedVerses = new List(); + + foreach (var moderatedVerse in moderation.VerseOrderText) + { + if (moderatedVerse.Result == CorrectionReviewResult.NotReviewed) + return new RServiceResult(null, $"تغییرات مصرع {moderatedVerse.VORder} بررسی نشده است."); + var dbVerse = dbCorrection.VerseOrderText.Where(c => c.VORder == moderatedVerse.VORder).Single(); + dbVerse.Result = moderatedVerse.Result; + dbVerse.ReviewNote = moderatedVerse.ReviewNote; + if (dbVerse.Result == CorrectionReviewResult.Approved) + { + dbCorrection.AffectedThePoem = true; + var poemVerse = poemVerses.Where(v => v.VOrder == moderatedVerse.VORder).Single(); + poemVerse.Text = moderatedVerse.Text.Replace("ۀ", "هٔ").Replace("ك", "ک"); + modifiedVerses.Add(poemVerse); + } + } + + if(modifiedVerses.Count > 0) + { + _context.UpdateRange(modifiedVerses); + dbPoem.HtmlText = PrepareHtmlText(poemVerses); + dbPoem.PlainText = PreparePlainText(poemVerses); + updatePoem = true; + } + + if(updatePoem) + { + _context.Update(dbPoem); + } + + await _context.SaveChangesAsync(); + + if (metreChanged || modifiedVerses.Count > 0) + { + foreach (var section in sections) + { + var sectionVerses = poemVerses.Where(v => + (section.VerseType == VersePoemSectionType.First && v.SectionIndex == section.Index) + || + (section.VerseType == VersePoemSectionType.Second && v.SecondSectionIndex == section.Index) + || + (section.VerseType == VersePoemSectionType.Third && v.ThirdSectionIndex == section.Index) + || + (section.VerseType == VersePoemSectionType.Forth && v.ForthSectionIndex == section.Index) + ).ToList(); + if(sectionVerses.Any(v => modifiedVerses.Contains(v))) + { + section.HtmlText = PrepareHtmlText(sectionVerses); + section.PlainText = PreparePlainText(sectionVerses); + } + } + } + + + /* + string originalRhyme = ""; + if(mainSection != null) + { + try + { + var newRhyme = LanguageUtils.FindRhyme(poemVerses); + if (!string.IsNullOrEmpty(newRhyme.Rhyme) && + (string.IsNullOrEmpty(mainSection.RhymeLetters) || (!string.IsNullOrEmpty(mainSection.RhymeLetters) && newRhyme.Rhyme.Length > mainSection.RhymeLetters.Length)) + ) + { + originalRhyme = mainSection.RhymeLetters ?? ""; + if(originalRhyme != newRhyme.Rhyme) + { + mainSection.RhymeLetters = newRhyme.Rhyme; + _context.Update(mainSection); + } + + } + } + catch + { + + } + } + */ + + + + _context.GanjoorPoemCorrections.Update(dbCorrection); + await _context.SaveChangesAsync(); + + await _notificationService.PushNotification(dbCorrection.UserId, + "بررسی ویرایش پیشنهادی شما", + $"با سپاس از زحمت و همت شما ویرایش پیشنهادیتان برای {dbPoem.FullTitle} بررسی شد.{Environment.NewLine}" + + $"جهت مشاهدهٔ نتیجهٔ بررسی در میز کاربری خود بخش «ویرایش‌های من» را مشاهده بفرمایید.{Environment.NewLine}" + ); + + return new RServiceResult(moderation); + } + } +} diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 7bbb4f19..1a453136 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -1982,129 +1982,6 @@ namespace RMuseum.Services.Implementation .CountAsync()); } - - /// - /// moderate poem correction - /// - /// - /// - /// - public async Task> ModeratePoemCorrection(Guid userId, - GanjoorPoemCorrectionViewModel moderation) - { - var dbCorrection = await _context.GanjoorPoemCorrections.Include(c => c.VerseOrderText).Include(c => c.User) - .Where(c => c.Id == moderation.Id) - .FirstOrDefaultAsync(); - - dbCorrection.ReviewerUserId = userId; - dbCorrection.ReviewDate = DateTime.Now; - dbCorrection.ApplicationOrder = await _context.GanjoorPoemCorrections.Where(c => c.Reviewed).AnyAsync() ? 1 + await _context.GanjoorPoemCorrections.Where(c => c.Reviewed).MaxAsync(c => c.ApplicationOrder) : 1; - dbCorrection.Reviewed = true; - dbCorrection.AffectedThePoem = false; - - if (dbCorrection == null) - return new RServiceResult(null); - - var dbPoem = await _context.GanjoorPoems.Include(p => p.GanjoorMetre).Where(p => p.Id == moderation.PoemId).SingleOrDefaultAsync(); - var dbPage = await _context.GanjoorPages.AsNoTracking().Where(p => p.Id == moderation.PoemId).SingleOrDefaultAsync(); - - GanjoorModifyPageViewModel pageViewModel = new GanjoorModifyPageViewModel() - { - Title = dbPoem.Title, - UrlSlug = dbPoem.UrlSlug, - OldTag = dbPoem.OldTag, - OldTagPageUrl = dbPoem.OldTagPageUrl, - RhymeLetters = dbPoem.RhymeLetters, - Rhythm = dbPoem.GanjoorMetre == null ? null : dbPoem.GanjoorMetre.Rhythm, - HtmlText = dbPoem.HtmlText, - SourceName = dbPoem.SourceName, - SourceUrlSlug = dbPoem.SourceUrlSlug, - MixedModeOrder = dbPoem.MixedModeOrder, - Published = dbPoem.Published, - Language = dbPoem.Language, - NoIndex = dbPage.NoIndex, - RedirectFromFullUrl = dbPage.RedirectFromFullUrl, - Note = dbCorrection.Note - }; - - if (dbCorrection.Title != null) - { - if (moderation.Result == CorrectionReviewResult.NotReviewed) - return new RServiceResult(null, "تغییرات عنوان بررسی نشده است."); - dbCorrection.Result = moderation.Result; - dbCorrection.ReviewNote = moderation.ReviewNote; - if (dbCorrection.Result == CorrectionReviewResult.Approved) - { - dbCorrection.AffectedThePoem = true; - pageViewModel.Title = moderation.Title.Replace("ۀ", "هٔ").Replace("ك", "ک"); - } - } - - if (dbCorrection.Rhythm != null) - { - if (moderation.RhythmResult == CorrectionReviewResult.NotReviewed) - return new RServiceResult(null, "تغییرات وزن بررسی نشده است."); - dbCorrection.RhythmResult = moderation.RhythmResult; - dbCorrection.ReviewNote = moderation.ReviewNote; - if (dbCorrection.RhythmResult == CorrectionReviewResult.Approved) - { - dbCorrection.AffectedThePoem = true; - pageViewModel.Rhythm = moderation.Rhythm; - } - } - - if (moderation.VerseOrderText.Length != dbCorrection.VerseOrderText.Count) - return new RServiceResult(null, "moderation.VerseOrderText.Length != dbCorrection.VerseOrderText.Count"); - - var poemVerses = await _context.GanjoorVerses.AsNoTracking().Where(p => p.PoemId == dbCorrection.PoemId).OrderBy(v => v.VOrder).ToListAsync(); - - foreach (var moderatedVerse in moderation.VerseOrderText) - { - if (moderatedVerse.Result == CorrectionReviewResult.NotReviewed) - return new RServiceResult(null, $"تغییرات مصرع {moderatedVerse.VORder} بررسی نشده است."); - var dbVerse = dbCorrection.VerseOrderText.Where(c => c.VORder == moderatedVerse.VORder).Single(); - dbVerse.Result = moderatedVerse.Result; - dbVerse.ReviewNote = moderatedVerse.ReviewNote; - if (dbVerse.Result == CorrectionReviewResult.Approved) - { - dbCorrection.AffectedThePoem = true; - poemVerses.Where(v => v.VOrder == moderatedVerse.VORder).Single().Text = moderatedVerse.Text.Replace("ۀ", "هٔ").Replace("ك", "ک"); - } - } - - pageViewModel.HtmlText = PrepareHtmlText(poemVerses); - try - { - var newRhyme = LanguageUtils.FindRhyme(poemVerses); - if (!string.IsNullOrEmpty(newRhyme.Rhyme) && - (string.IsNullOrEmpty(dbPoem.RhymeLetters) || (!string.IsNullOrEmpty(dbPoem.RhymeLetters) && newRhyme.Rhyme.Length > dbPoem.RhymeLetters.Length)) - ) - { - pageViewModel.RhymeLetters = newRhyme.Rhyme; - } - } - catch - { - - } - - var res = await UpdatePageAsync(moderation.PoemId, userId, pageViewModel); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return new RServiceResult(null, res.ExceptionString); - - - _context.GanjoorPoemCorrections.Update(dbCorrection); - await _context.SaveChangesAsync(); - - await _notificationService.PushNotification(dbCorrection.UserId, - "بررسی ویرایش پیشنهادی شما", - $"با سپاس از زحمت و همت شما ویرایش پیشنهادیتان برای {dbPoem.FullTitle} بررسی شد.{Environment.NewLine}" + - $"جهت مشاهدهٔ نتیجهٔ بررسی در میز کاربری خود بخش «ویرایش‌های من» را مشاهده بفرمایید.{Environment.NewLine}" - ); - - return new RServiceResult(moderation); - } - /// /// random poem id from hafez sonnets and old c.ganjoor.net service ///