Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- namespace CategoryProblemExtended
- {
- /// <summary>
- /// Entity interface
- /// </summary>
- public interface ICategory
- {
- int CategoryId { get; set; }
- int ParentCategoryId { get; set; }
- string Name { get; set; }
- IEnumerable<string> Keywords { get; set; }
- }
- /// <summary>
- /// Extended entity interface
- /// </summary>
- public interface IExtendedCategory
- {
- IList<Category> ParentItems { get; set; }
- }
- /// <summary>
- /// Category entity
- /// </summary>
- public class Category : ICategory
- {
- public int CategoryId { get; set; }
- public int ParentCategoryId { get; set; }
- public string Name { get; set; }
- public IEnumerable<string> Keywords { get; set; }
- }
- /// <summary>
- /// Extended category entity
- /// </summary>
- public class ExtendedCategory : ICategory, IExtendedCategory
- {
- public int CategoryId { get; set; }
- public int ParentCategoryId { get; set; }
- public string Name { get; set; }
- public IEnumerable<string> Keywords { get; set; }
- public IList<Category> ParentItems { get; set; }
- public int Level => ParentItems?.Count ?? 0;
- }
- /// <summary>
- /// Extensions methods
- /// </summary>
- public static class CategoryExtensions
- {
- /// <summary>
- /// Extension method returns all parent categories
- /// </summary>
- /// <param name="originalCategories"></param>
- /// <param name="parentCategoryId"></param>
- /// <param name="parentCategories"></param>
- /// <returns></returns>
- public static IList<Category> GetParentCategories(this IEnumerable<Category> originalCategories, int parentCategoryId, List<Category> parentCategories = null)
- {
- parentCategories = parentCategories ?? new List<Category>();
- var parentItem = originalCategories.FirstOrDefault(p => p.CategoryId == parentCategoryId);
- if (parentItem == null)
- {
- return parentCategories;
- }
- // for root level
- if (parentItem.ParentCategoryId == -1)
- {
- parentCategories.Add(parentItem);
- return parentCategories;
- }
- parentCategories.Add(parentItem);
- parentCategories.AddRange(originalCategories.GetParentCategories(parentItem.ParentCategoryId, parentCategories));
- return parentCategories.Distinct().ToList();
- }
- /// <summary>
- /// Get categories by category id
- /// </summary>
- /// <param name="originalCategories"></param>
- /// <param name="categoryId"></param>
- /// <returns></returns>
- public static IEnumerable<Category> GetByCategoryId(this IEnumerable<ExtendedCategory> originalCategories, int categoryId)
- {
- var result = originalCategories.Where(x => x.CategoryId == categoryId).Select(x => new Category
- {
- CategoryId = x.CategoryId,
- Name = x.Name,
- ParentCategoryId = x.ParentCategoryId,
- Keywords = x.ParentItems.FirstOrDefault(a => a.Keywords != null)?.Keywords
- }).ToList();
- return result;
- }
- /// <summary>
- /// Get categories by level id
- /// </summary>
- /// <param name="originalCategories"></param>
- /// <param name="levelId"></param>
- /// <returns></returns>
- public static IEnumerable<Category> GetByLevelId(this IEnumerable<ExtendedCategory> originalCategories, int levelId)
- {
- var result = originalCategories.Where(item => item.Level == levelId).Select(item => new Category
- {
- CategoryId = item.CategoryId,
- Name = item.Name,
- ParentCategoryId = item.ParentCategoryId,
- Keywords = item.Keywords
- }).ToList();
- return result;
- }
- /// <summary>
- /// Converting to extended entity
- /// </summary>
- /// <param name="originalCategories"></param>
- /// <returns></returns>
- public static IEnumerable<ExtendedCategory> GetExtendedCategories(this IEnumerable<Category> originalCategories)
- {
- var extendedCategories = originalCategories.Select(item => new ExtendedCategory
- {
- CategoryId = item.CategoryId,
- Keywords = item.Keywords,
- Name = item.Name,
- ParentCategoryId = item.ParentCategoryId,
- ParentItems = originalCategories.GetParentCategories(item.ParentCategoryId)
- }).ToList();
- return extendedCategories;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- RunByCategoryTest();
- RunByLevelTest();
- }
- public static List<Category> InitializeOriginalCategories()
- {
- var originalCategories = new List<Category>
- {
- new Category {CategoryId = 100, ParentCategoryId = -1, Name = "Business", Keywords = new List<string> {"Money"}},
- new Category {CategoryId = 200, ParentCategoryId = -1, Name = "Tutoring", Keywords = new List<string> {"Teaching"}},
- new Category {CategoryId = 101, ParentCategoryId = 100, Name = "Accounting", Keywords = new List<string> {"Taxes"}},
- new Category {CategoryId = 102, ParentCategoryId = 100, Name = "Taxation"},
- new Category {CategoryId = 201, ParentCategoryId = 200, Name = "Computer"},
- new Category {CategoryId = 103, ParentCategoryId = 101, Name = "Corporate Tax"},
- new Category {CategoryId = 202, ParentCategoryId = 201, Name = "Operating System"},
- new Category {CategoryId = 109, ParentCategoryId = 101, Name = "Small business Tax"}
- };
- return originalCategories;
- }
- static void RunByCategoryTest()
- {
- var originalCategories = InitializeOriginalCategories();
- // for category 201
- var expectedResult = new Category
- {
- CategoryId = 201,
- ParentCategoryId = 200,
- Name = "Computer",
- Keywords = new List<string> {"Teaching"}
- };
- var actualResult = originalCategories.GetExtendedCategories().GetByCategoryId(201).First();
- Debug.Assert(expectedResult.ParentCategoryId == actualResult.ParentCategoryId);
- Debug.Assert(expectedResult.CategoryId == actualResult.CategoryId);
- Debug.Assert(expectedResult.Name == actualResult.Name);
- Debug.Assert(expectedResult.Keywords.SequenceEqual(actualResult.Keywords));
- }
- static void RunByLevelTest()
- {
- var originalCategories = InitializeOriginalCategories();
- var level = 2;
- var expectedCategoriesNames = new List<string> { "Corporate Tax", "Operating System", "Small business Tax" };
- var actualCategoriesCount = originalCategories.GetExtendedCategories().GetByLevelId(level).Select(x => x.Name);
- Debug.Assert(expectedCategoriesNames.SequenceEqual(actualCategoriesCount));
- }
- }
- }
Add Comment
Please, Sign In to add comment