Exemplo n.º 1
0
        /// <summary>
        /// Create record with the specified id.
        /// </summary>
        /// <param name="parameters">Input parameters.</param>
        /// <param name="id">Input id record.</param>
        /// <returns>Id of the created record.</returns>
        public int Create(FileCabinetServiceContext parameters, int id)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            this.contextStrategy.ValidateParameters(parameters);

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

            this.AddRecordInAllDictionary(record);
            this.list.Add(record);
            this.list = this.list.OrderBy(x => x.Id).ToList();
            return(record.Id);
        }
        /// <summary>
        /// Changing data in an existing record.
        /// </summary>
        /// <param name="id">Id of the record to edit.</param>
        /// <param name="parameters">Input new FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
        public void EditRecord(int id, FileCabinetServiceContext parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using (var file = File.Open(this.fileStream.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                this.contextStrategy.ValidateParameters(parameters);
                byte[] recordBuffer = new byte[file.Length];
                file.Read(recordBuffer, 0, recordBuffer.Length);

                this.list = recordBuffer.ToListFileCabinetRecord();
                var updateRecord            = this.list.Find(record => record.Id == id);
                FileCabinetRecord oldrecord = updateRecord;
                var positionInFile          = this.GetPositionInFileById(oldrecord.Id);
                this.RemoveRecordInAllDictionary(oldrecord, positionInFile);
                updateRecord.Id          = id;
                updateRecord.Age         = parameters.Age;
                updateRecord.Salary      = parameters.Salary;
                updateRecord.Gender      = parameters.Gender;
                updateRecord.FirstName   = parameters.FirstName;
                updateRecord.LastName    = parameters.LastName;
                updateRecord.DateOfBirth = parameters.DateOfBirth;
                this.AddRecordInAllDictionary(updateRecord, positionInFile);
                this.FileCabinetRecordToBytes(updateRecord);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Сhanging data in an existing record.
 /// Displays the run time of the method.
 /// </summary>
 /// <param name="id">Id of the record to edit.</param>
 /// <param name="parameters">Input new FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
 public void EditRecord(int id, FileCabinetServiceContext parameters)
 {
     this.stopwatch.Reset();
     this.stopwatch.Start();
     this.service.EditRecord(id, parameters);
     this.stopwatch.Stop();
     Console.WriteLine($"{nameof(this.service.EditRecord)} method execution duration is {this.stopwatch.ElapsedTicks} ticks.");
 }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new records.
        /// Displays the run time of the method.
        /// </summary>
        /// <param name="parameters">Input FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
        /// <param name="id">Input id record.</param>
        /// <returns>Id of the new record.</returns>
        public int CreateRecord(FileCabinetServiceContext parameters, int id)
        {
            this.stopwatch.Reset();
            this.stopwatch.Start();
            var rezult = this.service.CreateRecord(parameters, id);

            this.stopwatch.Stop();
            Console.WriteLine($"{nameof(this.service.CreateRecord)} method execution duration is {this.stopwatch.ElapsedTicks} ticks.");
            return(rezult);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public int CreateRecord(FileCabinetServiceContext parameters, int id)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var recordId = this.Create(parameters, id);

            return(recordId);
        }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public int CreateRecord(FileCabinetServiceContext parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var id = this.Create(parameters, this.list.Count == 0 ? 1 : this.list.Max(maxId => maxId.Id) + 1);

            return(id);
        }
        /// <summary>
        /// Creates a new records.
        /// </summary>
        /// <param name="parameters">Input FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
        /// <returns>Id of the new record.</returns>
        public int CreateRecord(FileCabinetServiceContext parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            this.contextStrategy.ValidateParameters(parameters);
            this.isRecordIdSet = false;
            var recordId = this.Create(parameters);

            return(recordId);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Сhanging data in an existing record and writes the execution result to the log.
        /// </summary>
        /// <param name="id">Id of the record to edit.</param>
        /// <param name="parameters">Input new FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
        public void EditRecord(int id, FileCabinetServiceContext parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using (TextWriter writer = File.AppendText(this.path))
            {
                writer.WriteLine($"{DateTime.Now.ToString("dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture)} - Calling {nameof(this.service.EditRecord)}() with id = '{id}', FirstName = '{parameters.FirstName}'," +
                                 $" LastName = '{parameters.LastName}', DateOfBirth = '{parameters.DateOfBirth.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)}', " +
                                 $" Salary = '{parameters.Salary}', Gender = '{parameters.Gender}'");
                this.service.EditRecord(id, parameters);
                writer.WriteLine($"{DateTime.Now.ToString("dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture)} - {nameof(this.service.EditRecord)}() changed a record by id = '{id}' ");
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a new records.
        /// Displays the run time of the method.
        /// </summary>
        /// <param name="parameters">Input FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
        /// <param name="id">Input id record.</param>
        /// <returns>Id of the new record.</returns>
        public int CreateRecord(FileCabinetServiceContext parameters, int id)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using (TextWriter writer = File.AppendText(this.path))
            {
                writer.WriteLine($"{DateTime.Now.ToString("dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture)} - Calling {nameof(this.service.CreateRecord)}() with FirstName = '{parameters.FirstName}'," +
                                 $" LastName = '{parameters.LastName}', DateOfBirth = '{parameters.DateOfBirth.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)}' " +
                                 $" Salary = '{parameters.Salary}', Gender = '{parameters.Gender}'");
                var rezult = this.service.CreateRecord(parameters, id);
                writer.WriteLine($"{DateTime.Now.ToString("dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture)} - {nameof(this.service.CreateRecord)}() returned '{rezult}'");
                return(rezult);
            }
        }
        /// <summary>
        /// Create record with the specified id.
        /// </summary>
        /// <param name="parameters">Input parameters.</param>
        /// <returns>Id of the created record.</returns>
        public int Create(FileCabinetServiceContext parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using (var file = File.Open(this.fileStream.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                if (file.Length <= 0)
                {
                    this.recordId++;
                }
                else
                {
                    byte[] recordBuffer = new byte[RecordSize];
                    int    offset       = (int)file.Length - RecordSize;
                    file.Seek(offset, SeekOrigin.Begin);
                    file.Read(recordBuffer, 0, recordBuffer.Length);
                    byte[] bufferStatus = new byte[sizeof(short)];
                    if (!this.isRecordIdSet)
                    {
                        this.recordId = BitConverter.ToInt32(recordBuffer, bufferStatus.Length) + 1;
                    }
                }

                var record = new FileCabinetRecord
                {
                    Id          = this.recordId,
                    Age         = parameters.Age,
                    Salary      = parameters.Salary,
                    Gender      = parameters.Gender,
                    FirstName   = parameters.FirstName,
                    LastName    = parameters.LastName,
                    DateOfBirth = parameters.DateOfBirth,
                };
                this.AddRecordInAllDictionary(record, ((int)this.fileStream.Length / RecordSize) + 1);
                this.FileCabinetRecordToBytes(record);
                return(this.recordId);
            }
        }
Exemplo n.º 11
0
        /// <inheritdoc/>
        public void EditRecord(int id, FileCabinetServiceContext parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            this.contextStrategy.ValidateParameters(parameters);

            FileCabinetRecord record = this.list.Find(x => x.Id == id);

            this.RemoveRecordInAllDictionary(record);
            record.Id          = record.Id;
            record.FirstName   = parameters.FirstName;
            record.LastName    = parameters.LastName;
            record.DateOfBirth = parameters.DateOfBirth;
            record.Gender      = parameters.Gender;
            record.Age         = parameters.Age;
            record.Salary      = parameters.Salary;
            this.AddRecordInAllDictionary(record);
        }
Exemplo n.º 12
0
 /// <summary>
 /// virtual method for checking the correctness of user input.
 /// </summary>
 /// <param name="parameters">Input FirstName, LastName, DateOfBirth, Gender, Salary, Age.</param>
 public void ValidateParameters(FileCabinetServiceContext parameters)
 {
 }