/// <summary>Creates new <see cref="FileCabinetRecord"/> instance.</summary> /// <param name="userInputData">User input data.</param> /// <returns>Returns identifier of the new <see cref="FileCabinetRecord"/> instance.</returns> /// <exception cref="ArgumentNullException">Thrown when <em>userInput</em> is <em>null</em>.</exception> public int CreateRecord(UnverifiedData userInputData) { if (userInputData == null) { throw new ArgumentNullException(nameof(userInputData)); } this.Validator.ValidateParameters(userInputData); int id = this.storedIdentifiers.Count != 0 ? this.storedIdentifiers.Max() + 1 : MinValueOfId; var record = new FileCabinetRecord { Id = id, Name = new FullName(userInputData.FirstName, userInputData.LastName), DateOfBirth = userInputData.DateOfBirth, Sex = userInputData.Sex, NumberOfReviews = userInputData.NumberOfReviews, Salary = userInputData.Salary, }; this.AddRecord(record); this.storedIdentifiers.Add(record.Id); this.undeletedRecordsCount++; return(id); }
/// <summary>Edits record by identifier.</summary> /// <param name="id">Identifier.</param> /// <param name="unverifiedData">User input data.</param> public void EditRecord(int id, UnverifiedData unverifiedData) { var timer = new Stopwatch(); timer.Start(); this.service.EditRecord(id, unverifiedData); timer.Stop(); Console.WriteLine($"EditRecord method execution duration is {timer.ElapsedMilliseconds} ticks."); }
/// <summary>Creates new <see cref="FileCabinetRecord"/> instance.</summary> /// <param name="unverifiedData">Raw data.</param> /// <returns>Returns identifier of the new <see cref="FileCabinetRecord"/> instance.</returns> public int CreateRecord(UnverifiedData unverifiedData) { var timer = new Stopwatch(); timer.Start(); var result = this.service.CreateRecord(unverifiedData); timer.Stop(); Console.WriteLine($"CreateRecord method execution duration is {timer.ElapsedMilliseconds} ticks."); return(result); }
/// <summary>Edits record by identifier.</summary> /// <param name="id">Identifier.</param> /// <param name="unverifiedData">Raw data.</param> /// <exception cref="ArgumentNullException">Thrown when <em>userInput </em>is null.</exception> /// <exception cref="ArgumentException">Thrown when identifier is invalid.</exception> public void EditRecord(int id, UnverifiedData unverifiedData) { if (!this.storedIdentifiers.Contains(id)) { throw new ArgumentException($"There is no #{id} record."); } if (unverifiedData == null) { throw new ArgumentNullException(nameof(unverifiedData)); } this.Validator.ValidateParameters(unverifiedData); foreach (var record in this.list) { if (record.Id == id) { this.firstNameDictionary[record.Name.FirstName.ToUpperInvariant()].Remove(record); this.lastNameDictionary[record.Name.LastName.ToUpperInvariant()].Remove(record); this.dateOfBirthDictionary[record.DateOfBirth].Remove(record); record.Name.FirstName = unverifiedData.FirstName; record.Name.LastName = unverifiedData.LastName; record.DateOfBirth = unverifiedData.DateOfBirth; record.Sex = unverifiedData.Sex; record.NumberOfReviews = unverifiedData.NumberOfReviews; record.Salary = unverifiedData.Salary; string firstNameKey = unverifiedData.FirstName.ToUpperInvariant(); if (!this.firstNameDictionary.ContainsKey(firstNameKey)) { this.firstNameDictionary.Add(firstNameKey, new List <FileCabinetRecord>()); } this.firstNameDictionary[firstNameKey].Add(record); string lastNameKey = unverifiedData.LastName.ToUpperInvariant(); if (!this.lastNameDictionary.ContainsKey(lastNameKey)) { this.lastNameDictionary.Add(lastNameKey, new List <FileCabinetRecord>()); } this.lastNameDictionary[lastNameKey].Add(record); if (!this.dateOfBirthDictionary.ContainsKey(unverifiedData.DateOfBirth)) { this.dateOfBirthDictionary.Add(unverifiedData.DateOfBirth, new List <FileCabinetRecord>()); } this.dateOfBirthDictionary[unverifiedData.DateOfBirth].Add(record); return; } } }
/// <summary>Loads records from CSV file.</summary> /// <param name="reader">Reader.</param> /// <param name="validator">Validator.</param> /// <exception cref="ArgumentNullException">Thrown when reader or validator is null.</exception> public void LoadFromCsv(StreamReader reader, IRecordValidator validator) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (validator == null) { throw new ArgumentNullException(nameof(validator)); } var csvReader = new FileCabinetRecordCsvReader(reader); IList <FileCabinetRecord> records; try { records = csvReader.ReadAll(); } catch (FormatException) { throw; } catch (IndexOutOfRangeException) { throw; } UnverifiedData unverifiedData; foreach (var record in records) { unverifiedData = new UnverifiedData(record); try { validator.ValidateParameters(unverifiedData); } catch (ArgumentException ex) { this.invalidRecords.Add(new Tuple <FileCabinetRecord, string>(record, ex.Message)); continue; } this.records.Add(record); } }
/// <summary>Restores the specified snapshot.</summary> /// <param name="snapshot">Snapshot.</param> /// <exception cref="ArgumentNullException">Thrown when snapshot is null.</exception> public void Restore(FileCabinetServiceSnapshot snapshot) { if (snapshot == null) { throw new ArgumentNullException(nameof(snapshot)); } UnverifiedData data; foreach (var record in snapshot.Records) { if (this.storedIdentifiers.Contains(record.Id)) { data = new UnverifiedData(record); this.EditRecord(record.Id, data); } else { this.AddRecord(record); } } }
/// <summary>Edits record by identifier.</summary> /// <param name="id">Identifier.</param> /// <param name="unverifiedData">User input data.</param> /// <exception cref="ArgumentNullException">Thrown when unverifiedData /// is null.</exception> public void EditRecord(int id, UnverifiedData unverifiedData) { using var writer = new StreamWriter(LogFileName, true, Encoding.UTF8); if (unverifiedData == null) { writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - EditRecord() threw an exception: {nameof(unverifiedData)} is null"); throw new ArgumentNullException(nameof(unverifiedData)); } writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - Calling EditRecord() with Id = '{id}', FirstName = '{unverifiedData.FirstName}', " + $"LastName = '{unverifiedData.LastName}', DateOfBirth = '{unverifiedData.DateOfBirth.ToString(InputDateFormat, Culture)}', " + $"Sex = '{unverifiedData.Sex}', NumberOfReviews = '{unverifiedData.NumberOfReviews}', Salary = '{unverifiedData.Salary}'"); try { this.service.EditRecord(id, unverifiedData); writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - EditRecord() executed successfully"); } catch (ArgumentException ex) { writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - EditRecord() threw an exception: {ex.Message}"); throw; } }
/// <summary>Creates new <see cref="FileCabinetRecord"/> instance.</summary> /// <param name="unverifiedData">Raw data.</param> /// <returns>Returns identifier of the new <see cref="FileCabinetRecord"/> instance.</returns> /// <exception cref="ArgumentNullException">Thrown when unverifiedData /// is null.</exception> public int CreateRecord(UnverifiedData unverifiedData) { using var writer = new StreamWriter(LogFileName, true, Encoding.UTF8); if (unverifiedData == null) { writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - CreateRecord() threw an exception: {nameof(unverifiedData)} is null"); throw new ArgumentNullException(nameof(unverifiedData)); } writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - Calling CreateRecord() with FirstName = '{unverifiedData.FirstName}', " + $"LastName = '{unverifiedData.LastName}', DateOfBirth = '{unverifiedData.DateOfBirth.ToString(InputDateFormat, Culture)}', " + $"Sex = '{unverifiedData.Sex}', NumberOfReviews = '{unverifiedData.NumberOfReviews}', Salary = '{unverifiedData.Salary}'"); try { var result = this.service.CreateRecord(unverifiedData); writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - CreateRecord() returned '{result}'"); return(result); } catch (ArgumentException ex) { writer.WriteLine($"{DateTime.Now.ToString(LogDateFormat, Culture)} - CreateRecord() threw an exception: {ex.Message}"); throw; } }
/// <summary>Edits record by identifier.</summary> /// <param name="id">Identifier.</param> /// <param name="userInputData">User input data.</param> /// <exception cref="ArgumentNullException">Thrown when <em>userInput </em>is null.</exception> /// <exception cref="ArgumentException">Thrown when identifier is invalid.</exception> public void EditRecord(int id, UnverifiedData userInputData) { if (!this.storedIdentifiers.Contains(id)) { throw new ArgumentException($"There is no #{id} record."); } if (userInputData == null) { throw new ArgumentNullException(nameof(userInputData)); } this.Validator.ValidateParameters(userInputData); var firstNameCharArray = new char[MaxFirstNameLength]; var lastNameCharArray = new char[MaxLastNameLength]; for (int i = 0; i < userInputData.FirstName.Length; i++) { firstNameCharArray[i] = userInputData.FirstName[i]; } for (int i = 0; i < userInputData.LastName.Length; i++) { lastNameCharArray[i] = userInputData.LastName[i]; } using var reader = new BinaryReader(this.fileStream, Encoding.Unicode, true); using var writer = new BinaryWriter(this.fileStream, Encoding.Unicode, true); this.fileStream.Seek(IdOffset, SeekOrigin.Begin); bool isNotFound = true; do { if (id == reader.ReadInt32()) { var currentRecordOffset = this.fileStream.Position - sizeof(int); var oldFirstNameKey = new string(reader.ReadChars(MaxFirstNameLength)).Trim(NullCharacter).ToUpperInvariant(); this.fileStream.Seek(-2 * MaxFirstNameLength, SeekOrigin.Current); this.firstNamesOffsets[oldFirstNameKey].Remove(currentRecordOffset); var newFirstNameKey = userInputData.FirstName.ToUpperInvariant(); if (!this.firstNamesOffsets.ContainsKey(newFirstNameKey)) { this.firstNamesOffsets.Add(newFirstNameKey, new List <long>()); } this.firstNamesOffsets[newFirstNameKey].Add(currentRecordOffset); writer.Write(firstNameCharArray); var oldLastNameKey = new string(reader.ReadChars(MaxLastNameLength)).Trim(NullCharacter).ToUpperInvariant(); this.fileStream.Seek(-2 * MaxLastNameLength, SeekOrigin.Current); this.lastNamesOffsets[oldLastNameKey].Remove(currentRecordOffset); var newLastNameKey = userInputData.LastName.ToUpperInvariant(); if (!this.lastNamesOffsets.ContainsKey(newLastNameKey)) { this.lastNamesOffsets.Add(newLastNameKey, new List <long>()); } this.lastNamesOffsets[newLastNameKey].Add(currentRecordOffset); writer.Write(lastNameCharArray); var oldDateOfBirthKey = new DateTime(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()); this.fileStream.Seek(-3 * sizeof(int), SeekOrigin.Current); this.dateOfBirthOffsets[oldDateOfBirthKey].Remove(currentRecordOffset); if (!this.dateOfBirthOffsets.ContainsKey(userInputData.DateOfBirth)) { this.dateOfBirthOffsets.Add(userInputData.DateOfBirth, new List <long>()); } this.dateOfBirthOffsets[userInputData.DateOfBirth].Add(currentRecordOffset); writer.Write(userInputData.DateOfBirth.Year); writer.Write(userInputData.DateOfBirth.Month); writer.Write(userInputData.DateOfBirth.Day); writer.Write(userInputData.Sex); writer.Write(userInputData.NumberOfReviews); writer.Write(userInputData.Salary); isNotFound = false; } else { this.fileStream.Seek(RecordLenghtInBytes - sizeof(int), SeekOrigin.Current); } }while (isNotFound); }