Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.Linq;
  4.  
  5. namespace CategoryProblemExtended
  6. {
  7. /// <summary>
  8. /// Entity interface
  9. /// </summary>
  10. public interface ICategory
  11. {
  12. int CategoryId { get; set; }
  13. int ParentCategoryId { get; set; }
  14. string Name { get; set; }
  15. IEnumerable<string> Keywords { get; set; }
  16. }
  17.  
  18. /// <summary>
  19. /// Extended entity interface
  20. /// </summary>
  21. public interface IExtendedCategory
  22. {
  23. IList<Category> ParentItems { get; set; }
  24. }
  25.  
  26. /// <summary>
  27. /// Category entity
  28. /// </summary>
  29. public class Category : ICategory
  30. {
  31. public int CategoryId { get; set; }
  32. public int ParentCategoryId { get; set; }
  33. public string Name { get; set; }
  34. public IEnumerable<string> Keywords { get; set; }
  35. }
  36.  
  37. /// <summary>
  38. /// Extended category entity
  39. /// </summary>
  40. public class ExtendedCategory : ICategory, IExtendedCategory
  41. {
  42. public int CategoryId { get; set; }
  43. public int ParentCategoryId { get; set; }
  44. public string Name { get; set; }
  45. public IEnumerable<string> Keywords { get; set; }
  46. public IList<Category> ParentItems { get; set; }
  47.  
  48. public int Level => ParentItems?.Count ?? 0;
  49. }
  50.  
  51. /// <summary>
  52. /// Extensions methods
  53. /// </summary>
  54. public static class CategoryExtensions
  55. {
  56. /// <summary>
  57. /// Extension method returns all parent categories
  58. /// </summary>
  59. /// <param name="originalCategories"></param>
  60. /// <param name="parentCategoryId"></param>
  61. /// <param name="parentCategories"></param>
  62. /// <returns></returns>
  63. public static IList<Category> GetParentCategories(this IEnumerable<Category> originalCategories, int parentCategoryId, List<Category> parentCategories = null)
  64. {
  65. parentCategories = parentCategories ?? new List<Category>();
  66.  
  67. var parentItem = originalCategories.FirstOrDefault(p => p.CategoryId == parentCategoryId);
  68.  
  69. if (parentItem == null)
  70. {
  71. return parentCategories;
  72. }
  73.  
  74. // for root level
  75. if (parentItem.ParentCategoryId == -1)
  76. {
  77. parentCategories.Add(parentItem);
  78.  
  79. return parentCategories;
  80. }
  81.  
  82. parentCategories.Add(parentItem);
  83. parentCategories.AddRange(originalCategories.GetParentCategories(parentItem.ParentCategoryId, parentCategories));
  84.  
  85. return parentCategories.Distinct().ToList();
  86. }
  87.  
  88. /// <summary>
  89. /// Get categories by category id
  90. /// </summary>
  91. /// <param name="originalCategories"></param>
  92. /// <param name="categoryId"></param>
  93. /// <returns></returns>
  94. public static IEnumerable<Category> GetByCategoryId(this IEnumerable<ExtendedCategory> originalCategories, int categoryId)
  95. {
  96. var result = originalCategories.Where(x => x.CategoryId == categoryId).Select(x => new Category
  97. {
  98. CategoryId = x.CategoryId,
  99. Name = x.Name,
  100. ParentCategoryId = x.ParentCategoryId,
  101. Keywords = x.ParentItems.FirstOrDefault(a => a.Keywords != null)?.Keywords
  102. }).ToList();
  103.  
  104. return result;
  105. }
  106.  
  107. /// <summary>
  108. /// Get categories by level id
  109. /// </summary>
  110. /// <param name="originalCategories"></param>
  111. /// <param name="levelId"></param>
  112. /// <returns></returns>
  113. public static IEnumerable<Category> GetByLevelId(this IEnumerable<ExtendedCategory> originalCategories, int levelId)
  114. {
  115. var result = originalCategories.Where(item => item.Level == levelId).Select(item => new Category
  116. {
  117. CategoryId = item.CategoryId,
  118. Name = item.Name,
  119. ParentCategoryId = item.ParentCategoryId,
  120. Keywords = item.Keywords
  121. }).ToList();
  122.  
  123. return result;
  124. }
  125.  
  126. /// <summary>
  127. /// Converting to extended entity
  128. /// </summary>
  129. /// <param name="originalCategories"></param>
  130. /// <returns></returns>
  131. public static IEnumerable<ExtendedCategory> GetExtendedCategories(this IEnumerable<Category> originalCategories)
  132. {
  133. var extendedCategories = originalCategories.Select(item => new ExtendedCategory
  134. {
  135. CategoryId = item.CategoryId,
  136. Keywords = item.Keywords,
  137. Name = item.Name,
  138. ParentCategoryId = item.ParentCategoryId,
  139. ParentItems = originalCategories.GetParentCategories(item.ParentCategoryId)
  140. }).ToList();
  141.  
  142. return extendedCategories;
  143. }
  144. }
  145.  
  146.  
  147.  
  148. class Program
  149. {
  150. static void Main(string[] args)
  151. {
  152. RunByCategoryTest();
  153. RunByLevelTest();
  154. }
  155.  
  156.  
  157. public static List<Category> InitializeOriginalCategories()
  158. {
  159. var originalCategories = new List<Category>
  160. {
  161. new Category {CategoryId = 100, ParentCategoryId = -1, Name = "Business", Keywords = new List<string> {"Money"}},
  162. new Category {CategoryId = 200, ParentCategoryId = -1, Name = "Tutoring", Keywords = new List<string> {"Teaching"}},
  163. new Category {CategoryId = 101, ParentCategoryId = 100, Name = "Accounting", Keywords = new List<string> {"Taxes"}},
  164. new Category {CategoryId = 102, ParentCategoryId = 100, Name = "Taxation"},
  165. new Category {CategoryId = 201, ParentCategoryId = 200, Name = "Computer"},
  166. new Category {CategoryId = 103, ParentCategoryId = 101, Name = "Corporate Tax"},
  167. new Category {CategoryId = 202, ParentCategoryId = 201, Name = "Operating System"},
  168. new Category {CategoryId = 109, ParentCategoryId = 101, Name = "Small business Tax"}
  169. };
  170.  
  171. return originalCategories;
  172. }
  173.  
  174. static void RunByCategoryTest()
  175. {
  176. var originalCategories = InitializeOriginalCategories();
  177.  
  178. // for category 201
  179. var expectedResult = new Category
  180. {
  181. CategoryId = 201,
  182. ParentCategoryId = 200,
  183. Name = "Computer",
  184. Keywords = new List<string> {"Teaching"}
  185. };
  186.  
  187. var actualResult = originalCategories.GetExtendedCategories().GetByCategoryId(201).First();
  188.  
  189. Debug.Assert(expectedResult.ParentCategoryId == actualResult.ParentCategoryId);
  190. Debug.Assert(expectedResult.CategoryId == actualResult.CategoryId);
  191. Debug.Assert(expectedResult.Name == actualResult.Name);
  192. Debug.Assert(expectedResult.Keywords.SequenceEqual(actualResult.Keywords));
  193. }
  194.  
  195. static void RunByLevelTest()
  196. {
  197. var originalCategories = InitializeOriginalCategories();
  198.  
  199. var level = 2;
  200. var expectedCategoriesNames = new List<string> { "Corporate Tax", "Operating System", "Small business Tax" };
  201.  
  202. var actualCategoriesCount = originalCategories.GetExtendedCategories().GetByLevelId(level).Select(x => x.Name);
  203.  
  204. Debug.Assert(expectedCategoriesNames.SequenceEqual(actualCategoriesCount));
  205. }
  206. }
  207. }
Add Comment
Please, Sign In to add comment