using Microsoft.EntityFrameworkCore; using RMuseum.Models.Diwan; using RMuseum.Models.Diwan.ViewModels; using RSecurityBackend.Models.Generic; using System; using System.Data; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace RMuseum.Services.Implementation { /// /// IDiwanService implementation /// public partial class DiwanService : IDiwanService { /// /// moderate cat correction /// /// /// /// public async Task> ModerateCatCorrectionAsync(Guid userId, DiwanCatCorrectionViewModel moderation) { try { var dbCorrection = await _context.DiwanCatCorrections.Include(c => c.User) .Where(c => c.Id == moderation.Id) .FirstOrDefaultAsync(); if (dbCorrection == null) return new RServiceResult(null); dbCorrection.ReviewerUserId = userId; dbCorrection.ReviewDate = DateTime.Now; dbCorrection.ApplicationOrder = await _context.DiwanCatCorrections.Where(c => c.Reviewed).AnyAsync() ? 1 + await _context.DiwanCatCorrections.Where(c => c.Reviewed).MaxAsync(c => c.ApplicationOrder) : 1; dbCorrection.AffectedTheCat = false; dbCorrection.ReviewNote = moderation.ReviewNote; int catId = dbCorrection.CatId; var dbCat = await _context.DiwanCategories.Where(c => c.Id == catId).SingleAsync(); var dbPage = await _context.DiwanPages.Where(p => p.DiwanPageType == DiwanPageType.CatPage && p.CatId == catId).SingleOrDefaultAsync(); if (dbPage == null) { dbPage = await _context.DiwanPages.Where(p => p.DiwanPageType == DiwanPageType.PoetPage && p.PoetId == dbCat.PoetId).SingleAsync(); } bool updateCat = false; if (dbCorrection.DescriptionHtml != null) { if (moderation.Result == CorrectionReviewResult.NotReviewed) return new RServiceResult(null, "تغییرات شرح بررسی نشده است."); dbCorrection.Result = moderation.Result; if (dbCorrection.Result == CorrectionReviewResult.Approved) { DiwanPageSnapshot snapshot = new DiwanPageSnapshot() { DiwanPageId = dbPage.Id, MadeObsoleteByUserId = userId, RecordDate = DateTime.Now, Note = $"بررسی پیشنهاد ویرایش بخش با شناسهٔ {dbCorrection.Id}", Title = dbPage.Title, UrlSlug = dbPage.UrlSlug, HtmlText = dbPage.HtmlText, }; _context.DiwanPageSnapshots.Add(snapshot); await _context.SaveChangesAsync(); dbCorrection.AffectedTheCat = true; dbCat.DescriptionHtml = moderation.DescriptionHtml.Replace("ۀ", "هٔ").Replace("ك", "ک"); dbCat.Description = moderation.Description; if(dbCat.ParentId == null) { var dbPoet = await _context.DiwanPoets.Where(p => p.Id == dbCat.PoetId).SingleAsync(); dbPoet.Description = dbCat.Description; _context.Update(dbPoet); } updateCat = true; } } if (updateCat) { _context.Update(dbCat); await _context.SaveChangesAsync(); var tocRes = await GenerateTableOfContents(userId, dbCorrection.CatId, dbCat.TableOfContentsStyle); if (!string.IsNullOrEmpty(tocRes.ExceptionString)) { return new RServiceResult(null, tocRes.ExceptionString); } dbPage.HtmlText = tocRes.Result; _context.Update(dbPage); } dbCorrection.Reviewed = true; _context.DiwanCatCorrections.Update(dbCorrection); await _context.SaveChangesAsync(); await _notificationService.PushNotification(dbCorrection.UserId, "بررسی ویرایش پیشنهادی شما برای بخش", $"با سپاس از زحمت و همت شما ویرایش پیشنهادیتان برای {dbPage.FullTitle} بررسی شد.{Environment.NewLine}" + $"جهت مشاهدهٔ نتیجهٔ بررسی در میز کاربری خود بخش «ویرایش‌های بخش‌های من» را مشاهده بفرمایید.{Environment.NewLine}" ); return new RServiceResult(moderation); } catch (Exception exp) { return new RServiceResult(null, exp.ToString()); } } } }