From 13860fad44aca41de7c30c6ff5fb73b5ca66d3f2 Mon Sep 17 00:00:00 2001 From: ikswi Date: Thu, 16 May 2024 08:51:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B5=D1=89=D1=91=20=D1=80=D0=B0=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionInfo.cs | 22 ++++++---- .../MassiveGenericObjects.cs | 3 +- .../StorageCollection.cs | 40 +++++++++---------- .../Exceptions/CollectionInfoException.cs | 4 +- .../Exceptions/CollectionInsertException.cs | 3 +- .../Exceptions/DrawningEquitablesException.cs | 14 +++++++ .../Exceptions/EmptyFileExeption.cs | 15 +++++++ .../Exceptions/FileFormatException.cs | 14 +++++++ .../Exceptions/FileNotFoundException.cs | 15 +++++++ AirFighter/AirFighter/serilogConfig.json | 2 +- 10 files changed, 101 insertions(+), 31 deletions(-) create mode 100644 AirFighter/AirFighter/Exceptions/DrawningEquitablesException.cs create mode 100644 AirFighter/AirFighter/Exceptions/EmptyFileExeption.cs create mode 100644 AirFighter/AirFighter/Exceptions/FileFormatException.cs create mode 100644 AirFighter/AirFighter/Exceptions/FileNotFoundException.cs diff --git a/AirFighter/AirFighter/CollectionGenericObjects/CollectionInfo.cs b/AirFighter/AirFighter/CollectionGenericObjects/CollectionInfo.cs index 6ed31db..eb35195 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/CollectionInfo.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/CollectionInfo.cs @@ -1,8 +1,7 @@ -namespace ProjectAirFighter.CollectionGenericObjects; +using ProjectAirFighter.CollectionGenericObjects; + +namespace AirFighter; -/// -/// Класс, хранящиий информацию по коллекции -/// public class CollectionInfo : IEquatable { /// @@ -31,7 +30,8 @@ public class CollectionInfo : IEquatable /// Название /// Тип /// Описание - public CollectionInfo(string name, CollectionType collectionType, string description) + public CollectionInfo(string name, CollectionType collectionType, string + description) { Name = name; CollectionType = collectionType; @@ -45,13 +45,15 @@ public class CollectionInfo : IEquatable /// Объект или null public static CollectionInfo? GetCollectionInfo(string data) { - string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries); + string[] strs = data.Split(_separator, + StringSplitOptions.RemoveEmptyEntries); if (strs.Length < 1 || strs.Length > 3) { return null; } - return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty); + return new CollectionInfo(strs[0], + (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty); } public override string ToString() @@ -69,6 +71,12 @@ public class CollectionInfo : IEquatable return Equals(obj as CollectionInfo); } + public bool IsEmpty() + { + if (string.IsNullOrEmpty(Name) && CollectionType != CollectionType.None) return true; + return false; + } + public override int GetHashCode() { return Name.GetHashCode(); diff --git a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs index ba309ef..7e1c8a5 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectAirFighter.Exceptions; +using AirFighter; +using ProjectAirFighter.Exceptions; namespace ProjectAirFighter.CollectionGenericObjects; diff --git a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs index 2e13ac2..65d8560 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/StorageCollection.cs @@ -1,5 +1,8 @@ -using ProjectAirFighter.Drawnings; +using AirFighter; +using Microsoft.AspNetCore.Http; +using ProjectAirFighter.Drawnings; using ProjectAirFighter.Exceptions; +using System.Collections.Generic; using System.Text; namespace ProjectAirFighter.CollectionGenericObjects; @@ -46,16 +49,15 @@ public class StorageCollection /// /// Добавление коллекции в хранилище /// - /// Название коллекции - /// тип коллекции - public void AddCollection(string name, CollectionType collectionType) + /// тип коллекции + public void AddCollection(CollectionInfo collectionInfo) { - if (name == null || _storages.ContainsKey(name)) { return; } + if (_storages.ContainsKey(collectionInfo)) throw new CollectionAlreadyExistsException(collectionInfo); switch (collectionType) { case CollectionType.None: - return; + throw new CollectionTypeException("Пустой тип коллекции"); case CollectionType.Massive: _storages[name] = new MassiveGenericObjects(); return; @@ -69,10 +71,10 @@ public class StorageCollection /// Удаление коллекции /// /// Название коллекции - public void DelCollection(string name) + public void DelCollection(CollectionInfo collectionInfo) { - if (_storages.ContainsKey(name)) - _storages.Remove(name); + if (_storages.ContainsKey(collectionInfo)) + _storages.Remove(collectionInfo); } /// @@ -80,12 +82,13 @@ public class StorageCollection /// /// Название коллекции /// - public ICollectionGenericObjects? this[string name] + public ICollectionGenericObjects? this[CollectionInfo collectionInfo] { get { - if (name == null || !_storages.ContainsKey(name)) { return null; } - return _storages[name]; + if(_storages.ContainsKey(collectionInfo)) + return _storages[collectionInfo]; + return null; } } @@ -152,7 +155,7 @@ public class StorageCollection { if (!File.Exists(filename)) { - throw new FileNotFoundException("Файл не существует"); + throw new FileNotFoundException(filename); } using (StreamReader fs = File.OpenText(filename)) { @@ -165,7 +168,7 @@ public class StorageCollection if (!str.StartsWith(_collectionKey)) { - throw new FormatException("В файле неверные данные"); + throw new FormatException(filename); } _storages.Clear(); @@ -196,14 +199,11 @@ public class StorageCollection { try { - if (collection.Insert(militaryAircraft) == -1) - { - throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]); - } + collection.Insert(militaryAircraft); } - catch (CollectionOverflowException ex) + catch (Exception ex) { - throw new CollectionOverflowException("Коллекция переполнена", ex); + throw new FileFormatException(filename, ex); } } } diff --git a/AirFighter/AirFighter/Exceptions/CollectionInfoException.cs b/AirFighter/AirFighter/Exceptions/CollectionInfoException.cs index f50a7af..3d0c490 100644 --- a/AirFighter/AirFighter/Exceptions/CollectionInfoException.cs +++ b/AirFighter/AirFighter/Exceptions/CollectionInfoException.cs @@ -1,4 +1,6 @@ -namespace AirFighter; +using System.Runtime.Serialization; + +namespace AirFighter; public class CollectionInfoException : Exception { diff --git a/AirFighter/AirFighter/Exceptions/CollectionInsertException.cs b/AirFighter/AirFighter/Exceptions/CollectionInsertException.cs index 4214c43..460ba22 100644 --- a/AirFighter/AirFighter/Exceptions/CollectionInsertException.cs +++ b/AirFighter/AirFighter/Exceptions/CollectionInsertException.cs @@ -1,4 +1,5 @@ -namespace AirFighter; +using System.Runtime.Serialization; +namespace AirFighter; public class CollectionInsertException : Exception { diff --git a/AirFighter/AirFighter/Exceptions/DrawningEquitablesException.cs b/AirFighter/AirFighter/Exceptions/DrawningEquitablesException.cs new file mode 100644 index 0000000..325a2ca --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/DrawningEquitablesException.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace AirFighter; + +public class DrawningEquitablesException : Exception +{ + public DrawningEquitablesException() : base("Объекты прорисовки одинаковые") { } + public DrawningEquitablesException(string message) : base(message) { } + public DrawningEquitablesException(string message, Exception exception) : + base(message, exception) + { } + protected DrawningEquitablesException(SerializationInfo info, StreamingContext + contex) : base(info, contex) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/EmptyFileExeption.cs b/AirFighter/AirFighter/Exceptions/EmptyFileExeption.cs new file mode 100644 index 0000000..a48974a --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/EmptyFileExeption.cs @@ -0,0 +1,15 @@ +using System.Runtime.Serialization; + +namespace AirFighter; + +public class EmptyFileExeption : Exception +{ + public EmptyFileExeption(string name) : base($"Файл {name} пустой ") { } + public EmptyFileExeption() : base("В хранилище отсутствуют коллекции для сохранения") { } + public EmptyFileExeption(string name, string message) : base(message) { } + public EmptyFileExeption(string name, string message, Exception exception) : + base(message, exception) + { } + protected EmptyFileExeption(SerializationInfo info, StreamingContext + contex) : base(info, contex) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/FileFormatException.cs b/AirFighter/AirFighter/Exceptions/FileFormatException.cs new file mode 100644 index 0000000..528ff6a --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/FileFormatException.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization; + +namespace AirFighter; + +public class FileFormatException : Exception +{ + public FileFormatException() : base() { } + public FileFormatException(string message) : base(message) { } + public FileFormatException(string name, Exception exception) : + base($"Файл {name} имеет неверный формат. Ошибка: {exception.Message}", exception) + { } + protected FileFormatException(SerializationInfo info, StreamingContext + contex) : base(info, contex) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/Exceptions/FileNotFoundException.cs b/AirFighter/AirFighter/Exceptions/FileNotFoundException.cs new file mode 100644 index 0000000..153cfea --- /dev/null +++ b/AirFighter/AirFighter/Exceptions/FileNotFoundException.cs @@ -0,0 +1,15 @@ +using System.Runtime.Serialization; + +namespace AirFighter; + +public class FileNotFoundException : Exception +{ + public FileNotFoundException(string name) : base($"Файл {name} не существует ") { } + public FileNotFoundException() : base() { } + public FileNotFoundException(string name, string message) : base(message) { } + public FileNotFoundException(string name, string message, Exception exception) : + base(message, exception) + { } + protected FileNotFoundException(SerializationInfo info, StreamingContext + contex) : base(info, contex) { } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/serilogConfig.json b/AirFighter/AirFighter/serilogConfig.json index d97e26c..4ca9b41 100644 --- a/AirFighter/AirFighter/serilogConfig.json +++ b/AirFighter/AirFighter/serilogConfig.json @@ -14,7 +14,7 @@ ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { - "Application": "Stormtrooper" + "Application": "AirFighter" } } }