diff --git a/SushiBar/SushiBarBusinessLogic/OrderLogic.cs b/SushiBar/SushiBarBusinessLogic/OrderLogic.cs index 564aed8..fe20448 100644 --- a/SushiBar/SushiBarBusinessLogic/OrderLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/OrderLogic.cs @@ -16,151 +16,141 @@ namespace SushiBarBusinessLogic.BusinessLogics public class OrderLogic : IOrderLogic { private readonly ILogger _logger; - private readonly IOrderStorage _orderStorage; - static readonly object _locker = new object(); - - public OrderLogic(ILogger Logger, IOrderStorage OrderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage) { - _logger = Logger; - _orderStorage = OrderStorage; + _logger = logger; + _orderStorage = orderStorage; } - - public List? ReadList(OrderSearchModel? Model) + public List? ReadList(OrderSearchModel? model) { - _logger.LogInformation("ReadList. ClientId: {ClientId}. Status: {Status}. ImplementerId: {ImplementerId}. DateFrom: {DateFrom}. DateTo: {DateTo}. OrderId: {Id}", - Model?.ClientId, Model?.Status, Model?.ImplementerId, Model?.DateFrom, Model?.DateTo, Model?.Id); - - var List = Model is null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(Model); - if (List is null) + _logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } - _logger.LogInformation("ReadList. Count: {Count}", List.Count); - return List; + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; } - public bool CreateOrder(OrderBindingModel Model) + public bool CreateOrder(OrderBindingModel model) { - CheckModel(Model); - - if (Model.Status != OrderStatus.Неизвестен) - { - _logger.LogWarning("Invalid order status"); + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) return false; - } - - Model.Status = OrderStatus.Принят; - - if (_orderStorage.Insert(Model) is null) + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } - return true; } - private bool ChangeOrderStatus(OrderBindingModel Model, OrderStatus NewStatus) - { - CheckModel(Model, false); - - var Order = _orderStorage.GetElement(new OrderSearchModel { Id = Model.Id }); - - if (Order == null) - { - _logger.LogWarning("Change status operation failed. Order not found"); - return false; - } - - if (Order.Status + 1 != NewStatus) - { - _logger.LogWarning("Change status operation failed. Incorrect new status: {NewStatus}. Current status: {currStatus}", - NewStatus, Order.Status); - return false; - } - - Model.SushiId = Order.SushiId; - Model.ClientId = Order.ClientId; - if (!Model.ImplementerId.HasValue) Model.ImplementerId = Order.ImplementerId; - Model.Count = Order.Count; - Model.Sum = Order.Sum; - Model.DateCreate = Order.DateCreate; - Model.Status = NewStatus; - - if (Model.Status == OrderStatus.Готов) - Model.DateImplement = DateTime.Now; - else - Model.DateImplement = Order.DateImplement; - - if (_orderStorage.Update(Model) == null) - { - _logger.LogWarning("Change status operation failed"); - return false; - } - - return true; - } - - public bool TakeOrderInWork(OrderBindingModel Model) + public bool TakeOrderInWork(OrderBindingModel model) { lock (_locker) { - return ChangeOrderStatus(Model, OrderStatus.Выполняется); + return ChangeStatus(model, OrderStatus.Выполняется); } } - public bool FinishOrder(OrderBindingModel Model) + public bool FinishOrder(OrderBindingModel model) { - return ChangeOrderStatus(Model, OrderStatus.Готов); + return ChangeStatus(model, OrderStatus.Готов); } - public bool DeliveryOrder(OrderBindingModel Model) + public bool DeliveryOrder(OrderBindingModel model) { - return ChangeOrderStatus(Model, OrderStatus.Выдан); + return ChangeStatus(model, OrderStatus.Выдан); } - private void CheckModel(OrderBindingModel Model, bool WithParams = true) + private void CheckModel(OrderBindingModel model, bool withParams = true) { - if (Model == null) - throw new ArgumentNullException(nameof(Model)); - - if (!WithParams) - return; - - if (Model.Count <= 0) - throw new ArgumentNullException("Количество суши в заказе должно быть больше 0", nameof(Model.Count)); - - if (Model.Sum <= 0) - throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(Model.Sum)); - - _logger.LogInformation("Order. SushiId:{SushiId}. Count:{Count}. Sum:{Sum}. " + - "Status:{Status}. DateCreate:{DateCreate}. DateImplement:{DateImplement}. Id: {Id}", - Model.SushiId, Model.Count, Model.Sum, Model.Status, Model.DateCreate, - Model.DateImplement, Model.Id); - } - - public OrderViewModel? ReadElement(OrderSearchModel Model) - { - if (Model == null) + if (model == null) { - throw new ArgumentNullException(nameof(Model)); + throw new ArgumentNullException(nameof(model)); } + if (!withParams) + { + return; + } + if (model.Count <= 0) + { + throw new ArgumentException("Колличество суши в заказе не может быть меньше 1", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum)); + } + if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) + { + throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}"); + } + _logger.LogInformation("Sushi. SushiId:{SushiId}.Count:{Count}.Sum:{Sum}Id:{Id}", + model.SushiId, model.Count, model.Sum, model.Id); + } - _logger.LogInformation("ReadElement. ClientId: {ClientId}. Status: {Status}. ImplementerId: {ImplementerId}. DateFrom: {DateFrom}. DateTo: {DateTo}. OrderId: {Id}", - Model.ClientId, Model.Status, Model.ImplementerId, Model.DateFrom, Model.DateTo, Model.Id); + private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel() + { + Id = model.Id + }); + if (element == null) + { + throw new InvalidOperationException(nameof(element)); + } + model.DateCreate = element.DateCreate; + model.SushiId = element.SushiId; + model.DateImplement = element.DateImplement; + model.ClientId = element.ClientId; + if (!model.ImplementerId.HasValue) + { + model.ImplementerId = element.ImplementerId; + } + model.Status = element.Status; + model.Count = element.Count; + model.Sum = element.Sum; + if (requiredStatus - model.Status == 1) + { + model.Status = requiredStatus; + if (model.Status == OrderStatus.Готов) + { + model.DateImplement = DateTime.Now; + } + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); + throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); - var Order = _orderStorage.GetElement(Model); + } - if (Order == null) + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id); + var element = _orderStorage.GetElement(model); + if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } - - _logger.LogInformation("ReadElement find. Id: {Id}", Order.Id); - return Order; + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; } } } diff --git a/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs b/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs index e6a7365..24811b1 100644 --- a/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs +++ b/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs @@ -8,17 +8,17 @@ using System.Threading.Tasks; namespace SushiBarContracts.BindingModels { - public class OrderBindingModel : IOrderModel - { - public int Id { get; set; } - public int SushiId { get; set; } - public int ClientId { get; set; } + public class OrderBindingModel : IOrderModel + { + public int Id { get; set; } + public int SushiId { get; set; } + public int ClientId { get; set; } public int? ImplementerId { get; set; } public int Count { get; set; } - public double Sum { get; set; } - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplement { get; set; } - } + public double Sum { get; set; } + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + } } diff --git a/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs index 1413876..19d3cfd 100644 --- a/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs @@ -9,28 +9,35 @@ using System.Threading.Tasks; namespace SushiBarContracts.ViewModels { - public class OrderViewModel : IOrderModel - { - [DisplayName("Номер")] - public int Id { get; set; } - public int SushiId { get; set; } - public int ClientId { get; set; } + public class OrderViewModel : IOrderModel + { + [DisplayName("Номер")] + public int Id { get; set; } public int? ImplementerId { get; set; } - [DisplayName("Клиент")] - public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Изделие")] - public string SushiName { get; set; } = string.Empty; [DisplayName("Исполнитель")] public string? ImplementerFIO { get; set; } = null; + public int SushiId { get; set; } + public int ClientId { get; set; } + + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + + [DisplayName("Изделие")] + public string SushiName { get; set; } = string.Empty; + [DisplayName("Количество")] public int Count { get; set; } - [DisplayName("Сумма")] - public double Sum { get; set; } - [DisplayName("Статус")] - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Дата создания")] - public DateTime DateCreate { get; set; } = DateTime.Now; - [DisplayName("Дата выполнения")] - public DateTime? DateImplement { get; set; } - } + + [DisplayName("Сумма")] + public double Sum { get; set; } + + [DisplayName("Статус")] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + + [DisplayName("Дата создания")] + public DateTime DateCreate { get; set; } = DateTime.Now; + + [DisplayName("Дата выполнения")] + public DateTime? DateImplement { get; set; } + } } diff --git a/SushiBar/SushiBarDatabaseImplement/Order.cs b/SushiBar/SushiBarDatabaseImplement/Order.cs index c218a5c..66e54d0 100644 --- a/SushiBar/SushiBarDatabaseImplement/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Order.cs @@ -6,74 +6,80 @@ using System.ComponentModel.DataAnnotations; namespace SushiBarDatabaseImplement.Models { - public class Order : IOrderModel - { - public int Id { get; private set; } + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] public int ClientId { get; private set; } - public virtual Client Client { get; private set; } = new(); + [Required] + public int SushiId { get; private set; } + + public virtual Sushi Sushi { get; set; } = new(); [Required] - public int SushiId { get; private set; } + public int Count { get; private set; } - public virtual Sushi Sushi { get; set; } = new(); + [Required] + public double Sum { get; private set; } - [Required] - public int Count { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - [Required] - public double Sum { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; - [Required] - public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - - [Required] - public DateTime DateCreate { get; private set; } = DateTime.Now; - - public DateTime? DateImplement { get; private set; } + public DateTime? DateImplement { get; private set; } public int? ImplementerId { get; private set; } + public virtual Implementer? Implementer { get; set; } = new(); public static Order Create(SushiBarDatabase context, OrderBindingModel model) - { - return new Order() - { - Id = model.Id, + { + return new Order() + { + Id = model.Id, ClientId = model.ClientId, Client = context.Clients.First(x => x.Id == model.ClientId), SushiId = model.SushiId, - Sushi = context.Sushis.First(x => x.Id == model.SushiId), - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement, - }; - } + Sushi = context.Sushis.First(x => x.Id == model.SushiId), + ImplementerId = model.ImplementerId, + Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } - public void Update(OrderBindingModel? model) - { - if (model == null) - { - return; - } - Status = model.Status; - DateImplement = model.DateImplement; - } + public void Update(SushiBarDatabase context, OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; + Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null; + } - public OrderViewModel GetViewModel => new() - { - Id = Id, + public OrderViewModel GetViewModel => new() + { + Id = Id, ClientId = ClientId, ClientFIO = Client.ClientFIO, SushiId = SushiId, - SushiName = Sushi.SushiName, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - }; - } + SushiName = Sushi.SushiName, + ImplementerId = ImplementerId, + ImplementerFIO = Implementer != null ? Implementer.ImplementerFIO : null, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } } \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs index 5927eb0..7bf6daa 100644 --- a/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs @@ -12,7 +12,7 @@ namespace SushiBarDatabaseImplement.Implements public List GetFullList() { using var context = new SushiBarDatabase(); - return context.Orders.Include(x => x.Sushi).Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Sushi).Include(x => x.Client).Include(y => y.Implementer).Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -65,7 +65,7 @@ namespace SushiBarDatabaseImplement.Implements { return null; } - order.Update(model); + order.Update(context, model); context.SaveChanges(); return order.GetViewModel; } diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs index d2cdc80..71d4143 100644 --- a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -8,55 +8,59 @@ using System.Xml.Linq; namespace SushiBarFileImplement { - internal class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string ComponentFileName = "Component.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string SushiFileName = "Sushi.xml"; - private readonly string ClientFileName = "Client.xml"; - public List Components { get; private set; } - public List Orders { get; private set; } - public List Sushis { get; private set; } - public List Clients { get; private set; } + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string SushiFileName = "Sushi.xml"; + private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Sushis { get; private set; } + public List Clients { get; private set; } + public List Implementers { get; private set; } - public static DataFileSingleton GetInstance() - { - if (instance == null) - { - instance = new DataFileSingleton(); - } - return instance; - } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } - public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); - public void SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); - private DataFileSingleton() - { - Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; - } + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + } - private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) - { - if (File.Exists(filename)) - { - return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); - } - return new List(); - } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } - private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) - { - if (data != null) - { - new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); - } - } - } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } } \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/Implementer.cs b/SushiBar/SushiBarFileImplement/Implementer.cs new file mode 100644 index 0000000..71cbab3 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implementer.cs @@ -0,0 +1,87 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SushiBarFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + }; + } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("Password", Password), + new XElement("FIO", ImplementerFIO), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/SushiBar/SushiBarFileImplement/ImplementerStorage.cs b/SushiBar/SushiBarFileImplement/ImplementerStorage.cs new file mode 100644 index 0000000..3562d55 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/ImplementerStorage.cs @@ -0,0 +1,99 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _source.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + if (model.ImplementerFIO != null) + { + return _source.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (model.Id.HasValue) + { + return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + if (model.ImplementerFIO != null && model.Password != null) + { + return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))?.GetViewModel; + } + if (model.ImplementerFIO != null) + { + return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + } + return null; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + _source.Implementers.Remove(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + } +} diff --git a/SushiBar/SushiBarListImplement/DataListSingleton.cs b/SushiBar/SushiBarListImplement/DataListSingleton.cs index c633093..c7c987e 100644 --- a/SushiBar/SushiBarListImplement/DataListSingleton.cs +++ b/SushiBar/SushiBarListImplement/DataListSingleton.cs @@ -7,27 +7,29 @@ using System.Threading.Tasks; namespace SushiBarListImplement { - public class DataListSingleton - { - private static DataListSingleton? _instance; - public List Components { get; set; } - public List Orders { get; set; } - public List Sushis { get; set; } - public List Clients { get; set; } - private DataListSingleton() - { - Components = new List(); - Orders = new List(); - Sushis = new List(); - Clients = new List(); - } - public static DataListSingleton GetInstance() - { - if (_instance == null) - { - _instance = new DataListSingleton(); - } - return _instance; - } - } + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Sushis { get; set; } + public List Clients { get; set; } + public List Implementers { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Sushis = new List(); + Clients = new List(); + Implementers = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } } diff --git a/SushiBar/SushiBarListImplement/Implementer.cs b/SushiBar/SushiBarListImplement/Implementer.cs new file mode 100644 index 0000000..8a23220 --- /dev/null +++ b/SushiBar/SushiBarListImplement/Implementer.cs @@ -0,0 +1,60 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarListImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + } +} diff --git a/SushiBar/SushiBarListImplement/ImplementerStorage.cs b/SushiBar/SushiBarListImplement/ImplementerStorage.cs new file mode 100644 index 0000000..4375bb3 --- /dev/null +++ b/SushiBar/SushiBarListImplement/ImplementerStorage.cs @@ -0,0 +1,123 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + foreach (var x in _source.Implementers) + { + if (model.Id.HasValue && x.Id == model.Id) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && model.Password != null && x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + { + return x.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + + List result = new(); + if (model.ImplementerFIO != null) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; + } + } +} diff --git a/SushiBar/SushiBarView/Program.cs b/SushiBar/SushiBarView/Program.cs index d152b99..c6929d4 100644 --- a/SushiBar/SushiBarView/Program.cs +++ b/SushiBar/SushiBarView/Program.cs @@ -37,39 +37,40 @@ namespace SushiBarView option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); - - services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); } } } \ No newline at end of file