コード例 #1
0
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">Input request.</param>
        /// <returns>AppCommandRequest instance.</returns>
        public override AppCommandRequest Handle(AppCommandRequest request)
        {
            if (request == null)
            {
                throw new ArgumentException($"The {nameof(request)} is null");
            }

            const string name = "update";

            if (string.Equals(request.Command, name, StringComparison.OrdinalIgnoreCase))
            {
                if (this.service is FileCabinetMemoryService)
                {
                    Caсhe.ClearCashe();
                }

                this.Update(request.Parameters);
                return(null);
            }
            else
            {
                return(base.Handle(request));
            }
        }
コード例 #2
0
        private void Select(string parameters)
        {
            try
            {
                if (parameters.Split().Length == 1 && parameters.ToUpperInvariant() == "ALL")
                {
                    this.recordsToShow    = this.service.GetRecords();
                    this.propertiesToShow = this.find.GetAllProperties();
                }
                else
                {
                    var parametersArray = parameters.Split(Where, StringSplitOptions.None);

                    if (parametersArray.Length == 1)
                    {
                        if (!IsRightCommandSyntax(ParametersTemplateWithoutCondition, string.Join(string.Empty, parametersArray)))
                        {
                            throw new FormatException(InvalidFormat);
                        }
                    }
                    else if (!IsRightCommandSyntax(ParametersTemplateWithCondition, parameters))
                    {
                        throw new FormatException(InvalidFormat);
                    }

                    this.propertiesToShow = parametersArray[BeforeWhere].Trim().Split(",").Select(prop => prop.Trim().ToUpperInvariant()).AsEnumerable();

                    if (parametersArray.Length == 2)
                    {
                        var isContainsAnd        = this.find.ContainsAnd(parametersArray[AfterWhere], And);
                        var separators           = new string[] { Or, And };
                        var parametersAfterWhere = parametersArray[AfterWhere].Trim().Split(' ');
                        var parametersAfterWhereWithoutSeparators = parametersAfterWhere.Select(item => separators
                                                                                                .Contains(item) ? item.Replace(item, ",", StringComparison.InvariantCultureIgnoreCase) : item).ToArray();

                        var keyValuePairs = GetKeyValuePairs(string.Join(string.Empty, parametersAfterWhereWithoutSeparators)).ToList();

                        foreach (var pair in keyValuePairs)
                        {
                            if (this.service is FileCabinetMemoryService)
                            {
                                var recordsFromCashe = Caсhe.FindRecordInCashe(pair.key, pair.value);
                                if (!recordsFromCashe.Any())
                                {
                                    var recordsFromService = this.find.FindRecordByKey(pair.key, pair.value);
                                    foreach (var record in recordsFromService)
                                    {
                                        Caсhe.AddInCashe(pair.key, pair.value, record);
                                    }
                                }
                                else
                                {
                                    foreach (var record in recordsFromCashe)
                                    {
                                        if (!this.list.Exists(x => x.Id == record.Id))
                                        {
                                            this.list.Add(record);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                this.find.FindRecordByKey(pair.key, pair.value);
                            }
                        }

                        if (isContainsAnd)
                        {
                            foreach (var pair in keyValuePairs)
                            {
                                this.list = this.find.FindRecordByMatchingCriteria(pair.key, pair.value);
                            }
                        }

                        this.recordsToShow = this.list.OrderBy(record => record.Id).AsEnumerable();
                    }
                }

                if (!this.recordsToShow.Any())
                {
                    Console.WriteLine("No records were found with the specified search criteria");
                }
                else
                {
                    TableRecordDraw.PrintTable(this.recordsToShow, this.propertiesToShow);
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("The properties you specified for the selection do not exist");
            }
        }