#55 reducing data redundancy using view models

This commit is contained in:
Hamid Reza Mohammadi 2020-12-22 19:44:39 +03:30
parent 4b1f1ba9f0
commit 7fdabd93d7
5 changed files with 181 additions and 50 deletions

View File

@ -0,0 +1,42 @@
using System.Collections.Generic;
namespace RMuseum.Models.Ganjoor.ViewModels
{
/// <summary>
/// cat view model
/// </summary>
public class GanjoorCatViewModel
{
/// <summary>
/// id
/// </summary>
public int Id { get; set; }
/// <summary>
/// text
/// </summary>
public string Title { get; set; }
/// <summary>
/// url => slug
/// </summary>
public string UrlSlug { get; set; }
/// <summary>
/// ancestors
/// </summary>
public ICollection<GanjoorCatViewModel> Ancestors { get; set; }
/// <summary>
/// cat children
/// </summary>
public ICollection<GanjoorCatViewModel> Children { get; set; }
/// <summary>
/// poems
/// </summary>
public ICollection<GanjoorPoemSummaryViewModel> Poems { get; set; }
}
}

View File

@ -0,0 +1,29 @@
namespace RMuseum.Models.Ganjoor.ViewModels
{
/// <summary>
/// poem summary
/// </summary>
public class GanjoorPoemSummaryViewModel
{
/// <summary>
/// id
/// </summary>
public int Id { get; set; }
/// <summary>
/// title
/// </summary>
public string Title { get; set; }
/// <summary>
/// url => slug
/// </summary>
public string UrlSlug { get; set; }
/// <summary>
/// excerpt text
/// </summary>
public string Excerpt { get; set; }
}
}

View File

@ -1,6 +1,4 @@
using System.Collections.Generic;
namespace RMuseum.Models.Ganjoor.ViewModels
namespace RMuseum.Models.Ganjoor.ViewModels
{
/// <summary>
/// Poet Complete Information
@ -15,16 +13,6 @@ namespace RMuseum.Models.Ganjoor.ViewModels
/// <summary>
/// poet cat info
/// </summary>
public GanjoorCat Cat { get; set; }
/// <summary>
/// poet cat children
/// </summary>
public ICollection<GanjoorCat> Children { get; set; }
/// <summary>
/// poems
/// </summary>
public ICollection<GanjoorPoem> Poems { get; set; }
public GanjoorCatViewModel Cat { get; set; }
}
}

View File

@ -3430,6 +3430,41 @@
Non-poem paragraph
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel">
<summary>
cat view model
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel.Id">
<summary>
id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel.Title">
<summary>
text
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel.UrlSlug">
<summary>
url => slug
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel.Ancestors">
<summary>
ancestors
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel.Children">
<summary>
cat children
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCatViewModel.Poems">
<summary>
poems
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoemCompleteViewModel">
<summary>
a more complete GanjoorPoem View Model
@ -3450,6 +3485,31 @@
Images
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoemSummaryViewModel">
<summary>
poem summary
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoemSummaryViewModel.Id">
<summary>
id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoemSummaryViewModel.Title">
<summary>
title
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoemSummaryViewModel.UrlSlug">
<summary>
url => slug
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoemSummaryViewModel.Excerpt">
<summary>
excerpt text
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoetCompleteViewModel">
<summary>
Poet Complete Information
@ -3465,16 +3525,6 @@
poet cat info
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoetCompleteViewModel.Children">
<summary>
poet cat children
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPoetCompleteViewModel.Poems">
<summary>
poems
</summary>
</member>
<member name="T:RMuseum.Models.ImportJob.ImportJob">
<summary>
Import Job

View File

@ -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<GanjoorPoetCompleteViewModel>(null);
var cat = await _context.GanjoorCategories.Where(c => c.ParentId == null && c.PoetId == id).FirstOrDefaultAsync();
return new RServiceResult<GanjoorPoetCompleteViewModel>
(
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<GanjoorPoetCompleteViewModel>(null);
if(cat.Parent != null)
List<GanjoorCatViewModel> ancetors = new List<GanjoorCatViewModel>();
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<GanjoorPoetCompleteViewModel>
(
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)
{