From 7fdabd93d7b690074e55c176276a9b34d6f9cd90 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 22 Dec 2020 19:44:39 +0330 Subject: [PATCH] #55 reducing data redundancy using view models --- .../Ganjoor/ViewModels/GanjoorCatViewModel.cs | 42 +++++++++++ .../ViewModels/GanjoorPoemSummaryViewModel.cs | 29 ++++++++ .../GanjoorPoetCompleteViewModel.cs | 16 +--- RMuseum/RMuseum.xml | 70 +++++++++++++++--- .../Services/Implementation/GanjoorService.cs | 74 ++++++++++++------- 5 files changed, 181 insertions(+), 50 deletions(-) create mode 100644 RMuseum/Models/Ganjoor/ViewModels/GanjoorCatViewModel.cs create mode 100644 RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemSummaryViewModel.cs diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCatViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCatViewModel.cs new file mode 100644 index 00000000..5beb0d97 --- /dev/null +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCatViewModel.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; + +namespace RMuseum.Models.Ganjoor.ViewModels +{ + /// + /// cat view model + /// + public class GanjoorCatViewModel + { + /// + /// id + /// + public int Id { get; set; } + + /// + /// text + /// + public string Title { get; set; } + + /// + /// url => slug + /// + public string UrlSlug { get; set; } + + /// + /// ancestors + /// + public ICollection Ancestors { get; set; } + + /// + /// cat children + /// + public ICollection Children { get; set; } + + /// + /// poems + /// + public ICollection Poems { get; set; } + + + } +} diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemSummaryViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemSummaryViewModel.cs new file mode 100644 index 00000000..07ea04fc --- /dev/null +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemSummaryViewModel.cs @@ -0,0 +1,29 @@ +namespace RMuseum.Models.Ganjoor.ViewModels +{ + /// + /// poem summary + /// + public class GanjoorPoemSummaryViewModel + { + /// + /// id + /// + public int Id { get; set; } + + /// + /// title + /// + public string Title { get; set; } + + /// + /// url => slug + /// + public string UrlSlug { get; set; } + + /// + /// excerpt text + /// + public string Excerpt { get; set; } + + } +} diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoetCompleteViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoetCompleteViewModel.cs index 38625aa1..82f9ac8d 100644 --- a/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoetCompleteViewModel.cs +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoetCompleteViewModel.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace RMuseum.Models.Ganjoor.ViewModels +namespace RMuseum.Models.Ganjoor.ViewModels { /// /// Poet Complete Information @@ -15,16 +13,6 @@ namespace RMuseum.Models.Ganjoor.ViewModels /// /// poet cat info /// - public GanjoorCat Cat { get; set; } - - /// - /// poet cat children - /// - public ICollection Children { get; set; } - - /// - /// poems - /// - public ICollection Poems { get; set; } + public GanjoorCatViewModel Cat { get; set; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 81285628..b0e632e5 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -3430,6 +3430,41 @@ Non-poem paragraph + + + cat view model + + + + + id + + + + + text + + + + + url => slug + + + + + ancestors + + + + + cat children + + + + + poems + + a more complete GanjoorPoem View Model @@ -3450,6 +3485,31 @@ Images + + + poem summary + + + + + id + + + + + title + + + + + url => slug + + + + + excerpt text + + Poet Complete Information @@ -3465,16 +3525,6 @@ poet cat info - - - poet cat children - - - - - poems - - Import Job diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 0a3361d9..eca90339 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -10,6 +10,7 @@ using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.GanjoorIntegration.ViewModels; using RSecurityBackend.Models.Generic; using System; +using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; @@ -59,17 +60,7 @@ namespace RMuseum.Services.Implementation if (poet == null) return new RServiceResult(null); var cat = await _context.GanjoorCategories.Where(c => c.ParentId == null && c.PoetId == id).FirstOrDefaultAsync(); - - return new RServiceResult - ( - new GanjoorPoetCompleteViewModel() - { - Poet = poet, - Cat = cat, - Children = await _context.GanjoorCategories.Where(c => c.ParentId == cat.Id).ToListAsync(), - Poems = await _context.GanjoorPoems.Where(p => p.CatId == cat.Id).ToListAsync() - } - ); + return await GetCatById(cat.Id); } catch(Exception exp) { @@ -91,26 +82,57 @@ namespace RMuseum.Services.Implementation if (cat == null) return new RServiceResult(null); - if(cat.Parent != null) + List ancetors = new List(); + + var parent = cat.Parent; + while (parent != null) { - var parent = cat.Parent; - while(parent.ParentId != null) + ancetors.Insert(0, new GanjoorCatViewModel() { - parent.Parent = await _context.GanjoorCategories.Where(c => c.Id == parent.ParentId).FirstOrDefaultAsync(); - parent = parent.Parent; - } + Id = parent.Id, + Title = parent.Title, + UrlSlug = parent.UrlSlug + }); + + parent = await _context.GanjoorCategories.Where(c => c.Id == parent.ParentId).FirstOrDefaultAsync(); } + GanjoorCatViewModel catViewModel = new GanjoorCatViewModel() + { + Id = cat.Id, + Title = cat.Title, + UrlSlug = cat.UrlSlug, + Ancestors = ancetors, + Children = await _context.GanjoorCategories.Where(c => c.ParentId == cat.Id).Select + ( + c => new GanjoorCatViewModel() + { + Id = c.Id, + Title = c.Title, + UrlSlug = c.UrlSlug + } + ).ToListAsync(), + Poems = await _context.GanjoorPoems.Where(p => p.CatId == cat.Id).Select + ( + p => new GanjoorPoemSummaryViewModel() + { + Id = p.Id, + Title = p.Title, + UrlSlug = p.UrlSlug, + Excerpt = _context.GanjoorVerses.Where(v => v.PoemId == p.Id && v.VOrder == 1).FirstOrDefault().Text + } + ).ToListAsync() + }; + return new RServiceResult - ( - new GanjoorPoetCompleteViewModel() - { - Poet = cat.Poet, - Cat = cat, - Children = await _context.GanjoorCategories.Where(c => c.ParentId == cat.Id).ToListAsync(), - Poems = await _context.GanjoorPoems.Where(p => p.CatId == cat.Id).ToListAsync() - } - ); + ( + new GanjoorPoetCompleteViewModel() + { + Poet = await _context.GanjoorPoets.Where(p => p.Id == cat.PoetId).FirstOrDefaultAsync(), + Cat = catViewModel + } + ); + } catch (Exception exp) {