コード例 #1
0
        public static Command Parse(string currentCommandLine)
        {
            int numberOfArguments = (currentCommandLine.Split(new char[]{'|'}, StringSplitOptions.RemoveEmptyEntries)).Length;

            int commandNameLastIndex = currentCommandLine.IndexOf(' ');
            if (commandNameLastIndex == -1)
            {
                throw new FormatException("Invalid command line !");
            }

            string commandName = currentCommandLine.Substring(0, commandNameLastIndex);
            string commandParameters = currentCommandLine.Substring(commandNameLastIndex + 1);
            string[] commandArguments = commandParameters.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            if (numberOfArguments != commandArguments.Length)
            {
                throw new FormatException("Invalid  command or spaces !");
            }

            if (commandName.Contains('|'))
            {
                throw new FormatException("Invalid command: " + currentCommandLine);
            }

            for (int i = 0; i < commandArguments.Length; i++)
            {
                commandArguments[i] = commandArguments[i].Trim();
            }

            Command command = new Command(commandName, commandArguments);
            return command;
        }
コード例 #2
0
 public string ProcessCommand(Command currentCommand)
 {
     switch (currentCommand.CommandName)
     {
         case "AddEvent": return this.AddEvent(currentCommand);
         case "DeleteEvents": return this.DeleteEvent(currentCommand);
         case "ListEvents": return this.GetListEvents(currentCommand);
         default:
             throw new FormatException("WTF " + currentCommand.CommandName + " is?");
     }
 }
コード例 #3
0
        private string DeleteEvent(Command currentCommand)
        {
            if (currentCommand.Paramms.Length == 1)
            {
                int numberOfDeletedEvents = this.eventsManager.DeleteEventsByTitle(currentCommand.Paramms[0]);

                if (numberOfDeletedEvents == 0)
                {
                    return "No events found.";///1
                }

                return numberOfDeletedEvents + " events deleted";
            }

            throw new FormatException("Invalid params");
        }
コード例 #4
0
        private string AddEvent(Command currentCommand)
        {
            if (currentCommand.Paramms.Length == 2)
            {
                var date = DateTime.ParseExact(currentCommand.Paramms[0], "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
                Event createdEventFromCommand = new Event(date, currentCommand.Paramms[1]);
                this.eventsManager.AddEvent(createdEventFromCommand);
                return "Event added";
            }

            if (currentCommand.Paramms.Length == 3)
            {
                var date = DateTime.ParseExact(currentCommand.Paramms[0], "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
                var createdEventFromCommand = new Event(date, currentCommand.Paramms[1], currentCommand.Paramms[2]);
                this.eventsManager.AddEvent(createdEventFromCommand);
                return "Event added";
            }

            throw new FormatException("Invalid params");
        }
コード例 #5
0
        private string GetListEvents(Command currentCommand)
        {
            if (currentCommand.Paramms.Length == 2)
            {
                DateTime date = DateTime.ParseExact(currentCommand.Paramms[0], "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
                int commandNumber = int.Parse(currentCommand.Paramms[1]);
                List<Event> events = this.eventsManager.ListEvents(date, commandNumber).ToList();
                StringBuilder outputText = new StringBuilder();

                if (!events.Any())
                {
                    return "No events found";
                }

                foreach (var e in events)
                {
                    outputText.AppendLine(e.ToString());
                }

                return outputText.ToString().Trim();
            }

            throw new FormatException("Invalid params!");
        }