Exemple #1
0
        /// <summary>
        /// Check data by validators.
        /// </summary>
        /// <param name="recordData">Check data.</param>
        public void ValidateParameters(RecordData recordData)
        {
            ICollection <string> keys = this.validators.Keys;

            foreach (var k in keys)
            {
                this.validators[k].ValidateParameters(recordData);
            }
        }
        /// <summary>
        /// Create a new record and print tick that it took.
        /// </summary>
        /// <param name="recordData">User's data.</param>
        /// <returns>Id of a new record.</returns>
        public int CreateRecord(RecordData recordData)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            var result = this.service.CreateRecord(recordData);

            stopWatch.Stop();
            Console.WriteLine($"Create method execution duration is {stopWatch.ElapsedTicks} ticks.");
            return(result);
        }
        /// <summary>Creates the record.</summary>
        /// <param name="newRecordData">Container for the record's fields.</param>
        /// <returns>Returns the new record's ID.</returns>
        public int CreateRecord(RecordData newRecordData)
        {
            if (newRecordData != null)
            {
                this.validator.Validate(newRecordData.FirstName, newRecordData.LastName, newRecordData.Code, newRecordData.Letter, newRecordData.Balance, newRecordData.DateOfBirth);
                var record = new FileCabinetRecord
                {
                    Id          = this.list.Count + 1,
                    FirstName   = newRecordData.FirstName,
                    LastName    = newRecordData.LastName,
                    Code        = newRecordData.Code,
                    Letter      = newRecordData.Letter,
                    Balance     = newRecordData.Balance,
                    DateOfBirth = newRecordData.DateOfBirth,
                };

                this.list.Add(record);
                if (!this.firstNameDictionary.ContainsKey(newRecordData.FirstName))
                {
                    this.firstNameDictionary.Add(newRecordData.FirstName, new List <FileCabinetRecord>());
                    this.firstNameDictionary[newRecordData.FirstName].Add(record);
                }
                else
                {
                    this.firstNameDictionary[newRecordData.FirstName].Add(record);
                }

                if (!this.lastNameDictionary.ContainsKey(newRecordData.LastName))
                {
                    this.lastNameDictionary.Add(newRecordData.LastName, new List <FileCabinetRecord>());
                    this.lastNameDictionary[newRecordData.LastName].Add(record);
                }
                else
                {
                    this.lastNameDictionary[newRecordData.LastName].Add(record);
                }

                if (!this.dateOfBirthDictionary.ContainsKey(newRecordData.DateOfBirth))
                {
                    this.dateOfBirthDictionary.Add(newRecordData.DateOfBirth, new List <FileCabinetRecord>());
                    this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(record);
                }
                else
                {
                    this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(record);
                }

                this.selectDictionary = new Dictionary <string, string>();
                return(record.Id);
            }

            throw new ArgumentNullException(nameof(newRecordData), $"{nameof(newRecordData)} is null.");
        }
Exemple #4
0
        /// <summary>
        /// Check passport id from data.
        /// </summary>
        /// <param name="recordData">Data.</param>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        /// <exception cref="ArgumentException">Throw when PassportId more than maxValue or less than minValue.</exception>
        public void ValidateParameters(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            if (recordData.PassportId < this.minValue || recordData.PassportId > this.maxValue)
            {
                throw new ArgumentException($"Passport Id can't be less than {this.minValue} and more than {this.maxValue}", nameof(recordData));
            }
        }
Exemple #5
0
        /// <summary>
        /// Check salary from data.
        /// </summary>
        /// <param name="recordData">Data.</param>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        /// <exception cref="ArgumentException">Throw when Salary less than minSalary.</exception>
        public void ValidateParameters(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            if (recordData.Salary < this.minSalary)
            {
                throw new ArgumentException($"Salary can't be less than {this.minSalary}", nameof(recordData));
            }
        }
        /// <summary>
        /// Check date of birth from date.
        /// </summary>
        /// <param name="recordData">Data.</param>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        /// <exception cref="ArgumentException">Throw when DateOfBirth more than to or less than from.</exception>
        public void ValidateParameters(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            if ((DateTime.Compare(this.from, recordData.DateOfBirth) > 0) ||
                (DateTime.Compare(this.to, recordData.DateOfBirth) < 0))
            {
                throw new ArgumentException("Date of Birth can't be less than 01-Jan-1900 and more than today", nameof(recordData));
            }
        }
Exemple #7
0
        /// <summary>
        /// Read all records from stream.
        /// </summary>
        /// <returns>List of records.</returns>
        public IList <FileCabinetRecord> ReadAll()
        {
            List <FileCabinetRecord> list = new List <FileCabinetRecord>();
            var    validator = new ValidatorBuilder().CreateDefault();
            string rec       = this.stream.ReadLine();

            if (string.IsNullOrEmpty(rec))
            {
                return(list);
            }

            do
            {
                string[] elements = rec.Split(", ");
                if (int.TryParse(elements[0], out int id) &&
                    DateTime.TryParse(elements[3], out DateTime dateOfBirth) &&
                    char.TryParse(elements[4], out char gender) &&
                    short.TryParse(elements[5], out short passportId) &&
                    decimal.TryParse(elements[6], out decimal salary))
                {
                    RecordData recordData = new RecordData(elements[1], elements[2], dateOfBirth, gender, passportId, salary);
                    try
                    {
                        validator.ValidateParameters(recordData);
                        var record = new FileCabinetRecord
                        {
                            Id          = id,
                            FirstName   = recordData.FirstName,
                            LastName    = recordData.LastName,
                            DateOfBirth = recordData.DateOfBirth,
                            Gender      = recordData.Gender,
                            PassportId  = recordData.PassportId,
                            Salary      = recordData.Salary,
                        };
                        list.Add(record);
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                rec = this.stream.ReadLine();
            }while (rec != null);

            return(list);
        }
Exemple #8
0
        /// <summary>
        /// Read all records from stream.
        /// </summary>
        /// <returns>List of records.</returns>
        public IList <FileCabinetRecord> ReadAll()
        {
            List <FileCabinetRecord> result = new List <FileCabinetRecord>();
            RecordForSerializer      list;

            var validator = new ValidatorBuilder().CreateDefault();

            XmlSerializer ser = new XmlSerializer(typeof(RecordForSerializer));

            try
            {
                list = (RecordForSerializer)ser.Deserialize(this.stream);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
                return(result);
            }

            foreach (var record in list.Record)
            {
                try
                {
                    RecordData data = new RecordData(record.Name.FirstName, record.Name.LastName, record.DateOfBirth, record.Gender, record.PassportId, record.Salary);
                    validator.ValidateParameters(data);
                    result.Add(new FileCabinetRecord()
                    {
                        Id          = record.Id,
                        FirstName   = record.Name.FirstName,
                        LastName    = record.Name.LastName,
                        DateOfBirth = record.DateOfBirth,
                        Gender      = record.Gender,
                        PassportId  = record.PassportId,
                        Salary      = record.Salary,
                    });
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(result);
        }
        /// <summary>
        /// Check last name from data.
        /// </summary>
        /// <param name="recordData">Data.</param>
        /// <exception cref="ArgumentNullException">Throw when recordData, recordData.LastName is null.</exception>
        /// <exception cref="ArgumentException">Throw when LastName length more than maxLength or less than minLength.</exception>
        public void ValidateParameters(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            if (recordData.LastName is null)
            {
                throw new ArgumentNullException(nameof(recordData), "Last name can't be null");
            }

            if (recordData.LastName.Trim().Length < this.minLength || recordData.LastName.Trim().Length > this.maxLength)
            {
                throw new ArgumentException($"Length of last name can't be less than {this.minLength} and more than {this.maxLength}", nameof(recordData));
            }
        }
        /// <summary>
        /// Check gender from data.
        /// </summary>
        /// <param name="recordData">Data.</param>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        /// <exception cref="ArgumentException">Throw when Gender isn't manSymbol or womanSymbol.</exception>
        public void ValidateParameters(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            if (
                !string.Equals(
                    recordData.Gender.ToString(CultureInfo.InvariantCulture),
                    this.womanSymbol.ToString(CultureInfo.InvariantCulture),
                    StringComparison.InvariantCultureIgnoreCase) &&
                !string.Equals(
                    recordData.Gender.ToString(CultureInfo.InvariantCulture),
                    this.manSymbol.ToString(CultureInfo.InvariantCulture),
                    StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException($"Gender should be {this.womanSymbol} or {this.manSymbol}", nameof(recordData));
            }
        }
Exemple #11
0
        /// <summary>
        /// Create a new record.
        /// </summary>
        /// <param name="recordData">User's data.</param>
        /// <returns>Id of a new record.</returns>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        public int CreateRecord(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            this.validator.ValidateParameters(recordData);
            int id;

            if (recordData.Id > 0)
            {
                id = recordData.Id;
                this.Remove(id);
                this.presentIdList.Add(id, this.list.Count);
            }
            else
            {
                id = this.GetNextFreeId();
            }

            var record = new FileCabinetRecord
            {
                Id          = id,
                FirstName   = recordData.FirstName,
                LastName    = recordData.LastName,
                DateOfBirth = recordData.DateOfBirth,
                Gender      = recordData.Gender,
                PassportId  = recordData.PassportId,
                Salary      = recordData.Salary,
            };

            this.list.Add(record);
            this.AddToDictionary(this.firstNameDictionary, recordData.FirstName.ToUpperInvariant(), record.Id);
            this.AddToDictionary(this.lastNameDictionary, recordData.LastName.ToUpperInvariant(), record.Id);
            this.AddToDictionary(this.dateOfBirthDictionary, recordData.DateOfBirth.ToString("M/d/yyyy", this.culture), record.Id);
            this.cache.Clear();
            return(record.Id);
        }
Exemple #12
0
        /// <summary>
        /// Check data by validator.
        /// </summary>
        /// <param name="validator">Name of Validator.</param>
        /// <param name="recordData">Checking data.</param>
        /// <exception cref="ArgumentNullException">Throw when validator or recordData is null.</exception>
        public void ValidateParameter(string validator, RecordData recordData)
        {
            if (validator is null)
            {
                throw new ArgumentNullException(nameof(validator), "Validator can't be null");
            }

            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "Record can't be null");
            }

            string validatorStr = validator.ToUpperInvariant();

            if (this.validators.ContainsKey(validatorStr))
            {
                this.validators[validatorStr].ValidateParameters(recordData);
            }
            else
            {
                throw new ArgumentException("No such validator.", validator);
            }
        }
        /// <summary>
        /// Create a new record and print tick that it took.
        /// </summary>
        /// <param name="recordData">User's data.</param>
        /// <returns>Id of a new record.</returns>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        public int CreateRecord(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "Record data can't be null.");
            }

            this.LogWriter($"{DateTime.Now} - Calling Create() with FirstName = '{recordData.FirstName}', " +
                           $"LastName = '{recordData.LastName}', DateOfBirth = '{recordData.DateOfBirth.ToString("yyyy - MMM - dd", Culture)}', " +
                           $"Gender = '{recordData.Gender}', PassportId = '{recordData.PassportId}', " +
                           $"Salary = '{recordData.Salary}'");

            try
            {
                var result = this.service.CreateRecord(recordData);
                this.LogWriter($"{DateTime.Now} - Create() returned {result}");
                return(result);
            }
            catch (Exception ex)
            {
                this.LogWriter($"{DateTime.Now} - Create() throw {ex.Message}");
                throw;
            }
        }
Exemple #14
0
        /// <summary>
        /// Create a new record.
        /// </summary>
        /// <param name="recordData">User's data.</param>
        /// <returns>Id of a new record.</returns>
        /// <exception cref="ArgumentNullException">Throw when recordData is null.</exception>
        public int CreateRecord(RecordData recordData)
        {
            if (recordData is null)
            {
                throw new ArgumentNullException(nameof(recordData), "RecordData name can't be null");
            }

            this.validator.ValidateParameters(recordData);

            int id = recordData.Id;

            if (id < 1)
            {
                id = this.GetNextFreeId();
            }
            else
            {
                this.CheckId(id);
            }

            this.WriteToFile(recordData, id, this.offsetById[id]);

            return(id);
        }
        /// <summary>Edits the record.</summary>
        /// <param name="newRecordData">The new record data.</param>
        public void EditRecord(RecordData newRecordData)
        {
            if (newRecordData != null)
            {
                this.validator.Validate(newRecordData.FirstName, newRecordData.LastName, newRecordData.Code, newRecordData.Letter, newRecordData.Balance, newRecordData.DateOfBirth);
                foreach (var record in this.GetRecords())
                {
                    if (record.Id == newRecordData.Id)
                    {
                        if (this.firstNameDictionary[record.FirstName].Count > 1)
                        {
                            this.firstNameDictionary[record.FirstName].Remove(this.fileStream.Position);
                        }
                        else
                        {
                            this.firstNameDictionary.Remove(record.FirstName);
                        }

                        if (this.lastNameDictionary[record.LastName].Count > 1)
                        {
                            this.lastNameDictionary[record.LastName].Remove(this.fileStream.Position);
                        }
                        else
                        {
                            this.lastNameDictionary.Remove(record.LastName);
                        }

                        if (this.dateOfBirthDictionary[record.DateOfBirth].Count > 1)
                        {
                            this.dateOfBirthDictionary[record.DateOfBirth].Remove(this.fileStream.Position);
                        }
                        else
                        {
                            this.dateOfBirthDictionary.Remove(record.DateOfBirth);
                        }
                    }
                }

                long positionBackup = this.fileStream.Position;
                int  i = 0;
                this.fileStream.Position = 0;
                byte[] cleanArr = new byte[280];
                byte[] buffer   = new byte[280];
                this.SetPositionToId(newRecordData.Id);

                if (!this.firstNameDictionary.ContainsKey(newRecordData.FirstName))
                {
                    this.firstNameDictionary.Add(newRecordData.FirstName, new List <long>());
                    this.firstNameDictionary[newRecordData.FirstName].Add(this.fileStream.Position - 6);
                }
                else
                {
                    this.firstNameDictionary[newRecordData.FirstName].Add(this.fileStream.Position - 6);
                }

                if (!this.lastNameDictionary.ContainsKey(newRecordData.LastName))
                {
                    this.lastNameDictionary.Add(newRecordData.LastName, new List <long>());
                    this.lastNameDictionary[newRecordData.LastName].Add(this.fileStream.Position - 6);
                }
                else
                {
                    this.lastNameDictionary[newRecordData.LastName].Add(this.fileStream.Position - 6);
                }

                if (!this.dateOfBirthDictionary.ContainsKey(newRecordData.DateOfBirth))
                {
                    this.dateOfBirthDictionary.Add(newRecordData.DateOfBirth, new List <long>());
                    this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(this.fileStream.Position - 6);
                }
                else
                {
                    this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(this.fileStream.Position - 6);
                }

                foreach (var element in Encoding.Default.GetBytes(newRecordData.FirstName))
                {
                    buffer[i] = element;
                    i++;
                }

                i = 0;
                this.fileStream.Write(buffer, 0, 120);
                Array.Copy(cleanArr, buffer, 120);
                foreach (var element in Encoding.Default.GetBytes(newRecordData.LastName))
                {
                    buffer[i] = element;
                    i++;
                }

                this.fileStream.Write(buffer, 0, 120);
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.DateOfBirth.Year));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.DateOfBirth.Month));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.DateOfBirth.Day));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.Code));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.Letter));
                foreach (int value in decimal.GetBits(newRecordData.Balance))
                {
                    this.fileStream.Write(BitConverter.GetBytes(value));
                }

                this.fileStream.Position = positionBackup;
                this.selectDictionary    = new Dictionary <string, string>();
            }
        }
        /// <summary>Edits the record.</summary>
        /// <param name="newRecordData">Container for the record's fields.</param>
        /// <exception cref="ArgumentException">Thrown when id is incorrect.</exception>
        public void EditRecord(RecordData newRecordData)
        {
            if (newRecordData != null)
            {
                this.validator.Validate(newRecordData.FirstName, newRecordData.LastName, newRecordData.Code, newRecordData.Letter, newRecordData.Balance, newRecordData.DateOfBirth);
                foreach (var record in this.list)
                {
                    if (record.Id == newRecordData.Id)
                    {
                        if (this.firstNameDictionary[record.FirstName].Count > 1)
                        {
                            this.firstNameDictionary[record.FirstName].Remove(record);
                        }
                        else
                        {
                            this.firstNameDictionary.Remove(record.FirstName);
                        }

                        if (this.lastNameDictionary[record.LastName].Count > 1)
                        {
                            this.lastNameDictionary[record.LastName].Remove(record);
                        }
                        else
                        {
                            this.lastNameDictionary.Remove(record.LastName);
                        }

                        if (this.dateOfBirthDictionary[record.DateOfBirth].Count > 1)
                        {
                            this.dateOfBirthDictionary[record.DateOfBirth].Remove(record);
                        }
                        else
                        {
                            this.dateOfBirthDictionary.Remove(record.DateOfBirth);
                        }

                        record.FirstName   = newRecordData.FirstName;
                        record.LastName    = newRecordData.LastName;
                        record.Code        = newRecordData.Code;
                        record.Letter      = newRecordData.Letter;
                        record.Balance     = newRecordData.Balance;
                        record.DateOfBirth = newRecordData.DateOfBirth;
                        if (!this.firstNameDictionary.ContainsKey(newRecordData.FirstName))
                        {
                            this.firstNameDictionary.Add(newRecordData.FirstName, new List <FileCabinetRecord>());
                            this.firstNameDictionary[newRecordData.FirstName].Add(record);
                        }
                        else
                        {
                            this.firstNameDictionary[newRecordData.FirstName].Add(record);
                        }

                        if (!this.lastNameDictionary.ContainsKey(newRecordData.LastName))
                        {
                            this.lastNameDictionary.Add(newRecordData.LastName, new List <FileCabinetRecord>());
                            this.lastNameDictionary[newRecordData.LastName].Add(record);
                        }
                        else
                        {
                            this.lastNameDictionary[newRecordData.LastName].Add(record);
                        }

                        if (!this.dateOfBirthDictionary.ContainsKey(newRecordData.DateOfBirth))
                        {
                            this.dateOfBirthDictionary.Add(newRecordData.DateOfBirth, new List <FileCabinetRecord>());
                            this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(record);
                        }
                        else
                        {
                            this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(record);
                        }

                        this.selectDictionary = new Dictionary <string, string>();
                        return;
                    }
                }

                throw new ArgumentException($"{nameof(newRecordData.Id)} is incorrect.");
            }
        }
        /// <summary>Creates the record.</summary>
        /// <param name="newRecordData">The new record data.</param>
        /// <returns>Id of the record.</returns>
        public int CreateRecord(RecordData newRecordData)
        {
            if (newRecordData != null)
            {
                this.validator.Validate(newRecordData.FirstName, newRecordData.LastName, newRecordData.Code, newRecordData.Letter, newRecordData.Balance, newRecordData.DateOfBirth);
                if (!this.firstNameDictionary.ContainsKey(newRecordData.FirstName))
                {
                    this.firstNameDictionary.Add(newRecordData.FirstName, new List <long>());
                    this.firstNameDictionary[newRecordData.FirstName].Add(this.fileStream.Position);
                }
                else
                {
                    this.firstNameDictionary[newRecordData.FirstName].Add(this.fileStream.Position);
                }

                if (!this.lastNameDictionary.ContainsKey(newRecordData.LastName))
                {
                    this.lastNameDictionary.Add(newRecordData.LastName, new List <long>());
                    this.lastNameDictionary[newRecordData.LastName].Add(this.fileStream.Position);
                }
                else
                {
                    this.lastNameDictionary[newRecordData.LastName].Add(this.fileStream.Position);
                }

                if (!this.dateOfBirthDictionary.ContainsKey(newRecordData.DateOfBirth))
                {
                    this.dateOfBirthDictionary.Add(newRecordData.DateOfBirth, new List <long>());
                    this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(this.fileStream.Position);
                }
                else
                {
                    this.dateOfBirthDictionary[newRecordData.DateOfBirth].Add(this.fileStream.Position);
                }

                newRecordData.Id = ++this.identifiersCount;
                byte[] buffer   = new byte[120];
                byte[] cleanArr = new byte[120];
                this.fileStream.Write(BitConverter.GetBytes((short)0), 0, 2);
                int i = 0;
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.Id));
                foreach (var element in Encoding.Default.GetBytes(newRecordData.FirstName))
                {
                    buffer[i] = element;
                    i++;
                }

                i = 0;
                this.fileStream.Write(buffer, 0, 120);
                Array.Copy(cleanArr, buffer, 120);
                foreach (var element in Encoding.Default.GetBytes(newRecordData.LastName))
                {
                    buffer[i] = element;
                    i++;
                }

                this.fileStream.Write(buffer, 0, 120);
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.DateOfBirth.Year));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.DateOfBirth.Month));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.DateOfBirth.Day));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.Code));
                this.fileStream.Write(BitConverter.GetBytes(newRecordData.Letter));
                foreach (int value in decimal.GetBits(newRecordData.Balance))
                {
                    this.fileStream.Write(BitConverter.GetBytes(value));
                }

                this.selectDictionary = new Dictionary <string, string>();
                return(newRecordData.Id);
            }

            #pragma warning disable CA1303 // Do not pass literals as localized parameters
            throw new ArgumentException($"{nameof(newRecordData)} is incorrect.");
            #pragma warning restore CA1303 // Do not pass literals as localized parameters
        }