Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Core.Commands.Abstracts;
- using Models.Contracts;
- using Models.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Core.Commands
- {
- class ListWorkItems : Command
- {
- public ListWorkItems(IList<string> commandParameters)
- : base(commandParameters)
- {
- }
- public override string Execute()
- {
- string key = this.CommandParameters[0];
- if (key.StartsWith("bug"))
- {
- var bugList = this.Database.WorkItems.Where(item => item is IBug)
- .Select(item => item as IBug)
- .OrderBy(BugKeySelector(key));
- return $"Sorted by key: {SortedString(key, bugList)}";
- }
- else if (key.StartsWith("story"))
- {
- var storyList = this.Database.WorkItems.Where(item => item is IStory)
- .Select(item => item as IStory)
- .OrderBy(StoryKeySelector(key));
- return $"Sorted by key: {SortedString(key, storyList)}";
- }
- else if (key.StartsWith("feedback"))
- {
- var feedbackList = this.Database.WorkItems.Where(item => item is IFeedBack)
- .Select(item => item as IFeedBack)
- .OrderBy(FeedbackKeySelector(key));
- return $"Sorted by key: {SortedString(key, feedbackList)}";
- }
- else if (key == "title")
- {
- var ordered = this.Database.WorkItems.OrderBy(WorkItemKeySelector(key));
- return $"Sorted by key: {SortedString(key, ordered)}";
- }
- throw new ArgumentException($"Invalid key selector: {key}");
- }
- private string SortedString(string key, IEnumerable<IWorkItem> ordered)
- {
- return $"{key}{Environment.NewLine}*" + Environment.NewLine + string.Join(Environment.NewLine + "*" + Environment.NewLine, ordered);
- }
- private static Func<IBug, string> BugKeySelector(string key)
- => key switch
- {
- "bug" => x => x.ToString(),
- "bugpriority" => x => x.Priority.ToString(),
- "bugseverity" => x => x.Severity.ToString(),
- "bugstatus" => x => x.BugStatus.ToString(),
- "bugassignee" => x => x.Assignee.ToString(),
- _ => throw new ArgumentException($"Invalid key selector: {key}")
- };
- private static Func<IStory, string> StoryKeySelector(string key)
- => key switch
- {
- "story" => x => x.ToString(),
- "storysize" => x => x.StorySize.ToString(),
- "storypriority" => x => x.Priority.ToString(),
- "storystatus" => x => x.StoryStatus.ToString(),
- "storyassignee" => x => x.Assignee.ToString(),
- _ => throw new ArgumentException($"Invalid key selector: {key}")
- };
- private static Func<IFeedBack, string> FeedbackKeySelector(string key)
- => key switch
- {
- "feedback" => x => x.ToString(),
- "feedbackstatus" => x => x.FeedBackStatus.ToString(),
- "feedbackrating" => x => x.Rating.ToString(),
- _ => throw new ArgumentException($"Invalid key selector: {key}")
- };
- private static Func<IWorkItem, string> WorkItemKeySelector(string key)
- => key switch
- {
- "title" => x => x.Title,
- _ => throw new ArgumentException($"Invalid key selector: {key}")
- };
- //public List<IWorkItem> ListMethod(string param)
- //{
- // return this.Database.WorkItems.Where(item => GetWorkItemType(param).Equals(item.GetType())).ToList();
- //}
- //public BugStatus GetBugStatus(string bugStatus)
- //{
- // switch (bugStatus.ToLower())
- // {
- // case "active":
- // return BugStatus.Active;
- // case "fixed":
- // return BugStatus.Fixed;
- // default:
- // throw new ArgumentException($"BugStatus with name: {bugStatus} does not exist!");
- // }
- //}
- //public StoryStatus GetStoryStatus(string storyStatus)
- //{
- // switch (storyStatus.ToLower())
- // {
- // case "notdone":
- // return StoryStatus.NotDone;
- // case "inprogress":
- // return StoryStatus.InProgress;
- // case "done":
- // return StoryStatus.Done;
- // default:
- // throw new ArgumentException($"StoryStatus with name: {storyStatus} does not exist!");
- // }
- //}
- //public Type GetWorkItemType(string type)
- //{
- // switch (type)
- // {
- // case "bug":
- // return typeof(Bug);
- // case "story":
- // return typeof(Story);
- // case "feedback":
- // return typeof(FeedBack);
- // default:
- // throw new ArgumentException($"{type} is not a valid type");
- // }
- //}
- //// if (this.CommandParameters.Count != 1)
- // // throw new ArgumentException("AddCommentToWorkItem expect 3 paramenter");
- // string typeAsString = this.CommandParameters[0];
- //sortedItems = this.Database.WorkItems.Where(item => GetWorkItemType(typeAsString).Equals(item.GetType())).ToList();
- //foreach (var workItem in sortedItems)
- //{
- // msg.Append(workItem.GetDetailedInfo() + Environment.NewLine);
- //}
- //return msg.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement