Advertisement
dxeqtr

SampleListing

Feb 13th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. using Core.Commands.Abstracts;
  2. using Models.Contracts;
  3. using Models.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace Core.Commands
  10. {
  11. class ListWorkItems : Command
  12. {
  13. public ListWorkItems(IList<string> commandParameters)
  14. : base(commandParameters)
  15. {
  16.  
  17. }
  18. public override string Execute()
  19. {
  20. string key = this.CommandParameters[0];
  21.  
  22. if (key.StartsWith("bug"))
  23. {
  24. var bugList = this.Database.WorkItems.Where(item => item is IBug)
  25. .Select(item => item as IBug)
  26. .OrderBy(BugKeySelector(key));
  27.  
  28. return $"Sorted by key: {SortedString(key, bugList)}";
  29. }
  30. else if (key.StartsWith("story"))
  31. {
  32. var storyList = this.Database.WorkItems.Where(item => item is IStory)
  33. .Select(item => item as IStory)
  34. .OrderBy(StoryKeySelector(key));
  35.  
  36. return $"Sorted by key: {SortedString(key, storyList)}";
  37. }
  38. else if (key.StartsWith("feedback"))
  39. {
  40. var feedbackList = this.Database.WorkItems.Where(item => item is IFeedBack)
  41. .Select(item => item as IFeedBack)
  42. .OrderBy(FeedbackKeySelector(key));
  43.  
  44. return $"Sorted by key: {SortedString(key, feedbackList)}";
  45. }
  46. else if (key == "title")
  47. {
  48. var ordered = this.Database.WorkItems.OrderBy(WorkItemKeySelector(key));
  49.  
  50. return $"Sorted by key: {SortedString(key, ordered)}";
  51. }
  52.  
  53. throw new ArgumentException($"Invalid key selector: {key}");
  54.  
  55. }
  56. private string SortedString(string key, IEnumerable<IWorkItem> ordered)
  57. {
  58. return $"{key}{Environment.NewLine}*" + Environment.NewLine + string.Join(Environment.NewLine + "*" + Environment.NewLine, ordered);
  59. }
  60. private static Func<IBug, string> BugKeySelector(string key)
  61. => key switch
  62. {
  63. "bug" => x => x.ToString(),
  64. "bugpriority" => x => x.Priority.ToString(),
  65. "bugseverity" => x => x.Severity.ToString(),
  66. "bugstatus" => x => x.BugStatus.ToString(),
  67. "bugassignee" => x => x.Assignee.ToString(),
  68.  
  69. _ => throw new ArgumentException($"Invalid key selector: {key}")
  70. };
  71.  
  72. private static Func<IStory, string> StoryKeySelector(string key)
  73. => key switch
  74. {
  75. "story" => x => x.ToString(),
  76. "storysize" => x => x.StorySize.ToString(),
  77. "storypriority" => x => x.Priority.ToString(),
  78. "storystatus" => x => x.StoryStatus.ToString(),
  79. "storyassignee" => x => x.Assignee.ToString(),
  80.  
  81. _ => throw new ArgumentException($"Invalid key selector: {key}")
  82. };
  83.  
  84. private static Func<IFeedBack, string> FeedbackKeySelector(string key)
  85. => key switch
  86. {
  87. "feedback" => x => x.ToString(),
  88. "feedbackstatus" => x => x.FeedBackStatus.ToString(),
  89. "feedbackrating" => x => x.Rating.ToString(),
  90.  
  91. _ => throw new ArgumentException($"Invalid key selector: {key}")
  92. };
  93.  
  94. private static Func<IWorkItem, string> WorkItemKeySelector(string key)
  95. => key switch
  96. {
  97. "title" => x => x.Title,
  98.  
  99. _ => throw new ArgumentException($"Invalid key selector: {key}")
  100. };
  101.  
  102.  
  103. //public List<IWorkItem> ListMethod(string param)
  104. //{
  105.  
  106. // return this.Database.WorkItems.Where(item => GetWorkItemType(param).Equals(item.GetType())).ToList();
  107. //}
  108.  
  109. //public BugStatus GetBugStatus(string bugStatus)
  110. //{
  111. // switch (bugStatus.ToLower())
  112. // {
  113. // case "active":
  114. // return BugStatus.Active;
  115. // case "fixed":
  116. // return BugStatus.Fixed;
  117. // default:
  118. // throw new ArgumentException($"BugStatus with name: {bugStatus} does not exist!");
  119. // }
  120. //}
  121.  
  122. //public StoryStatus GetStoryStatus(string storyStatus)
  123. //{
  124. // switch (storyStatus.ToLower())
  125. // {
  126. // case "notdone":
  127. // return StoryStatus.NotDone;
  128. // case "inprogress":
  129. // return StoryStatus.InProgress;
  130. // case "done":
  131. // return StoryStatus.Done;
  132. // default:
  133. // throw new ArgumentException($"StoryStatus with name: {storyStatus} does not exist!");
  134. // }
  135. //}
  136.  
  137.  
  138. //public Type GetWorkItemType(string type)
  139. //{
  140.  
  141. // switch (type)
  142. // {
  143. // case "bug":
  144. // return typeof(Bug);
  145. // case "story":
  146. // return typeof(Story);
  147. // case "feedback":
  148. // return typeof(FeedBack);
  149. // default:
  150. // throw new ArgumentException($"{type} is not a valid type");
  151.  
  152. // }
  153. //}
  154.  
  155. //// if (this.CommandParameters.Count != 1)
  156. // // throw new ArgumentException("AddCommentToWorkItem expect 3 paramenter");
  157.  
  158. // string typeAsString = this.CommandParameters[0];
  159.  
  160. //sortedItems = this.Database.WorkItems.Where(item => GetWorkItemType(typeAsString).Equals(item.GetType())).ToList();
  161.  
  162.  
  163.  
  164.  
  165.  
  166. //foreach (var workItem in sortedItems)
  167. //{
  168. // msg.Append(workItem.GetDetailedInfo() + Environment.NewLine);
  169. //}
  170.  
  171. //return msg.ToString();
  172.  
  173.  
  174.  
  175. }
  176.  
  177.  
  178.  
  179. }
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement