diff --git a/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs b/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs index 28e7364..3eb40c7 100644 --- a/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs +++ b/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs @@ -12,9 +12,7 @@ namespace CarpentryWorkshopContracts.BindingModels { public int Id { get; set; } public int WoodId { get; set; } - public string WoodName { get; set; } = string.Empty; public int ClientId { get; set; } - public string ClientFIO { get; set; } = string.Empty; public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/CarpentryWorkshopDataModels/Models/IOrderModel.cs b/CarpentryWorkshopDataModels/Models/IOrderModel.cs index 7276b02..4d3bddb 100644 --- a/CarpentryWorkshopDataModels/Models/IOrderModel.cs +++ b/CarpentryWorkshopDataModels/Models/IOrderModel.cs @@ -10,12 +10,8 @@ namespace CarpentryWorkshopDataModels.Models public interface IOrderModel : IId { int WoodId { get; } - string WoodName - { - get; - } - - int Count { get; } + int ClientId { get; } + int Count { get; } double Sum { get; } OrderStatus Status { get; } DateTime DateCreate { get; } diff --git a/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs b/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs index 08ac385..6cc1310 100644 --- a/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -13,6 +13,8 @@ namespace CarpentryWorkshopDatabaseImplement.Implements { using var context = new CarpentryWorkshopDatabase(); return context.Orders + .Include(x => x.Wood) + .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } @@ -26,19 +28,23 @@ namespace CarpentryWorkshopDatabaseImplement.Implements if (model.ClientId.HasValue) { return context.Orders - .Where(x => x.ClientId == model.ClientId) + .Include(x => x.Wood) + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); } if (model.DateFrom.HasValue) { - return context.Orders - .Where(x => (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)) + return context.Orders.Include(x => x.Wood) + .Include(x => x.Client) + .Where(x => (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)) .Select(x => x.GetViewModel) .ToList(); } - return context.Orders - .Where(x => x.Id == model.Id) + return context.Orders.Include(x => x.Wood) + .Include(x => x.Client) + .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); } @@ -49,26 +55,30 @@ namespace CarpentryWorkshopDatabaseImplement.Implements return null; } using var context = new CarpentryWorkshopDatabase(); - return context.Orders + return context.Orders.Include(x => x.Wood).Include(x => x.Client) .FirstOrDefault(x => (model.Id != null && x.Id == model.Id) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } - using var context = new CarpentryWorkshopDatabase(); - context.Orders.Add(newOrder); - context.SaveChanges(); - return newOrder.GetViewModel; - } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Wood) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new CarpentryWorkshopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.Include(x => x.Wood).Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Orders.Remove(element); @@ -83,7 +93,7 @@ namespace CarpentryWorkshopDatabaseImplement.Implements public OrderViewModel? Update(OrderBindingModel model) { using var context = new CarpentryWorkshopDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders.Include(x => x.Wood).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; diff --git a/CarpentryWorkshopDatabaseImplement/Models/Order.cs b/CarpentryWorkshopDatabaseImplement/Models/Order.cs index afa50da..d93cd99 100644 --- a/CarpentryWorkshopDatabaseImplement/Models/Order.cs +++ b/CarpentryWorkshopDatabaseImplement/Models/Order.cs @@ -13,13 +13,9 @@ namespace CarpentryWorkshopDatabaseImplement.Models public int Id { get; set; } [ForeignKey("WoodId")] public int WoodId { get; set; } - [Required] - public string WoodName { get; set; } = string.Empty; [ForeignKey("ClientId")] public int ClientId { get; set; } [Required] - public string ClientFIO { get; set; } = string.Empty; - [Required] public int Count { get; set; } [Required] public double Sum { get; set; } @@ -29,9 +25,11 @@ namespace CarpentryWorkshopDatabaseImplement.Models public DateTime DateCreate { get; set; } public DateTime? DateImplement { get; set; } + public Wood Wood { get; set; } + public Client Client { get; set; } - public static Order? Create(OrderBindingModel? model) + public static Order? Create(OrderBindingModel? model) { if (model == null) { @@ -41,9 +39,7 @@ namespace CarpentryWorkshopDatabaseImplement.Models { Id = model.Id, WoodId = model.WoodId, - WoodName = model.WoodName, ClientId = model.ClientId, - ClientFIO = model.ClientFIO, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -58,15 +54,7 @@ namespace CarpentryWorkshopDatabaseImplement.Models { return; } - Id = model.Id; - WoodId = model.WoodId; - WoodName = model.WoodName; - ClientId = model.ClientId; - ClientFIO = model.ClientFIO; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } @@ -74,9 +62,9 @@ namespace CarpentryWorkshopDatabaseImplement.Models { Id = Id, WoodId = WoodId, - WoodName = WoodName, + WoodName = Wood.WoodName, ClientId = ClientId, - ClientFIO = ClientFIO, + ClientFIO = Client.ClientFIO, Count = Count, Sum = Sum, Status = Status, diff --git a/CarpentryWorkshopFileImplement/DataFileSingleton.cs b/CarpentryWorkshopFileImplement/DataFileSingleton.cs index 7b190ee..6652d83 100644 --- a/CarpentryWorkshopFileImplement/DataFileSingleton.cs +++ b/CarpentryWorkshopFileImplement/DataFileSingleton.cs @@ -9,10 +9,12 @@ namespace CarpentryWorkshopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string WoodFileName = "Wood.xml"; - public List Components { get; private set; } + private readonly string ClientFileName = "Client.xml"; + public List Components { get; private set; } public List Orders { get; private set; } public List Woods { get; private set; } - public static DataFileSingleton GetInstance() + public List Clients { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) { @@ -23,12 +25,14 @@ namespace CarpentryWorkshopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveWoods() => SaveData(Woods, WoodFileName, "Woods", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private DataFileSingleton() + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Woods = LoadData(WoodFileName, "Wood", x => Wood.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - } + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { if (File.Exists(filename)) diff --git a/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs b/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs index 39f7ba2..e1e31f4 100644 --- a/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs +++ b/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs @@ -18,66 +18,98 @@ namespace CarpentryWorkshopFileImplement.Implements { source = DataFileSingleton.GetInstance(); } - public List GetFullList() - { - return source.Orders - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(OrderSearchModel model) - { - if (model.Id == null) - { - return new(); - } - return source.Orders - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (model.Id == null && !model.Id.HasValue) - { - return null; - } - return source.Orders - .FirstOrDefault(x => (model.Id != null && x.Id == model.Id) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - public OrderViewModel? Insert(OrderBindingModel model) - { - model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; - var newOrder = Order.Create(model); if (newOrder == null) - { - return null; - } - source.Orders.Add(newOrder); - source.SaveOrders(); - return newOrder.GetViewModel; - } - public OrderViewModel? Update(OrderBindingModel model) - { - var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - source.SaveOrders(); - return order.GetViewModel; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (element != null) - { - source.Orders.Remove(element); - source.SaveOrders(); - return element.GetViewModel; - } - return null; - } - } + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (model.DateFrom.HasValue) + { + return source.Orders + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => GetViewModel(x)) + .ToList(); + } + if (model.ClientId.HasValue && !model.Id.HasValue) + { + return source.Orders + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Id.HasValue) + { + return source.Orders + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .ToList(); + } + return new(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + return source.Orders.FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var wood = source + .Woods.FirstOrDefault(x => x.Id == order.WoodId); + var client = source + .Clients.FirstOrDefault(x => x.Id == order.ClientId); + + if (wood != null) + viewModel.WoodName = wood.WoodName; + if (client != null) + viewModel.ClientFIO = client.ClientFIO; + return viewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return GetViewModel(element); + } + return null; + } + } } diff --git a/CarpentryWorkshopFileImplement/Models/Order.cs b/CarpentryWorkshopFileImplement/Models/Order.cs index 1d03e65..5ea84eb 100644 --- a/CarpentryWorkshopFileImplement/Models/Order.cs +++ b/CarpentryWorkshopFileImplement/Models/Order.cs @@ -14,85 +14,83 @@ namespace CarpentryWorkshopFileImplement.Models { public class Order : IOrderModel { - public int Id { get; set; } - public int WoodId { get; set; } - public string WoodName { get; set; } = string.Empty; - 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 static Order? Create(OrderBindingModel model) - { - if (model == null) - { - return null; - } - return new Order() - { - Id = model.Id, - WoodId = model.WoodId, - WoodName = model.WoodName, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; - } - public static Order? Create(XElement element) - { - if (element == null) - { - return null; - } - return new Order() - { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - WoodId = Convert.ToInt32(element.Element("WoodId")!.Value), - WoodName = element.Element("WoodName")!.Value, - Count = Convert.ToInt32(element.Element("Count")!.Value), - Sum = Convert.ToDouble(element.Element("Sum")!.Value), - Status = (OrderStatus)Convert.ToInt32(element.Element("Status").Value), - DateCreate = Convert.ToDateTime(element.Element("DateCreate").Value), - DateImplement = String.IsNullOrEmpty(element.Element("DateImplement").Value) ? DateTime.MinValue : Convert.ToDateTime(element.Element("DateImplement").Value) - - }; - } - public void Update(OrderBindingModel model) - { - if (model == null) - { - return; - } - Id = model.Id; - WoodId = model.WoodId; - WoodName = model.WoodName; - Count = model.Count; - Sum = model.Sum; - Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; - } - public OrderViewModel GetViewModel => new() - { - Id = Id, - WoodId = WoodId, - WoodName = WoodName, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - }; - public XElement GetXElement => new("Order", - new XAttribute("Id", Id), - new XElement("WoodId", WoodId), - new XElement("WoodName", WoodName), - new XElement("Count", Count), - new XElement("Sum", Sum.ToString()), - new XElement("Status", (int)Status), - new XElement("DateCreate", DateCreate), - new XElement("DateImplement", DateImplement)); - } + public int Id { get; private set; } + public int WoodId { get; private set; } + public int ClientId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + WoodId = model.WoodId, + ClientId = model.ClientId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + WoodId = Convert.ToInt32(element.Element("WoodId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? + null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + WoodId = WoodId, + ClientId = ClientId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("WoodId", WoodId), + new XElement("ClientId", ClientId), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } } diff --git a/CarpentryWorkshopListImplement/DataListSingleton.cs b/CarpentryWorkshopListImplement/DataListSingleton.cs index 9813b2a..d33bd5b 100644 --- a/CarpentryWorkshopListImplement/DataListSingleton.cs +++ b/CarpentryWorkshopListImplement/DataListSingleton.cs @@ -8,12 +8,14 @@ namespace CarpentryWorkshopListImplement public List Components { get; set; } public List Orders { get; set; } public List Woods { get; set; } - private DataListSingleton() + public List Clients { get; set; } + private DataListSingleton() { Components = new List(); Orders = new List(); Woods = new List(); - } + Clients = new List(); + } public static DataListSingleton GetInstance() { if (_instance == null) diff --git a/CarpentryWorkshopListImplement/Implements/ClientStorage.cs b/CarpentryWorkshopListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..6a7cf6c --- /dev/null +++ b/CarpentryWorkshopListImplement/Implements/ClientStorage.cs @@ -0,0 +1,99 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataListSingleton _source; + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + public List GetFilteredList(ClientSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ClientFIO)) + { + return result; + } + foreach (var client in _source.Clients) + { + if (client.ClientFIO.Contains(model.ClientFIO)) + { + result.Add(client.GetViewModel); + } + } + return result; + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + foreach (var client in _source.Clients) + { + if (model.Id.HasValue && model.Id == client.Id) + return client.GetViewModel; + if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) && + client.Email.Equals(model.Email) && client.Password.Equals(model.Password)) + return client.GetViewModel; + if (!string.IsNullOrEmpty(model.Email) && client.Email.Equals(model.Email)) + return client.GetViewModel; + } + return null; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = 1; + foreach (var client in _source.Clients) + { + if (model.Id <= client.Id) + { + model.Id = client.Id + 1; + } + } + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + _source.Clients.Add(newClient); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + return client.GetViewModel; + } + } + return null; + } + public ClientViewModel? Delete(ClientBindingModel model) + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/CarpentryWorkshopListImplement/Implements/OrderStorage.cs b/CarpentryWorkshopListImplement/Implements/OrderStorage.cs index 937e23b..fe81c7c 100644 --- a/CarpentryWorkshopListImplement/Implements/OrderStorage.cs +++ b/CarpentryWorkshopListImplement/Implements/OrderStorage.cs @@ -18,89 +18,129 @@ namespace CarpentryWorkshopListImplement.Implements { _source = DataListSingleton.GetInstance(); } - public List GetFullList() - { - var result = new List(); - foreach (var order in _source.Orders) - { - result.Add(order.GetViewModel); - } - return result; - } - public List GetFilteredList(OrderSearchModel model) - { - var result = new List(); - if (model.Id == null) - { - return result; - } - foreach (var order in _source.Orders) - { - if (order.Id.Equals(model.Id)) - { - result.Add(order.GetViewModel); - } - } - return result; - } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (model.Id == null && !model.Id.HasValue) - { - return null; - } - foreach (var order in _source.Orders) - { - if ((model.Id != null && order.Id == model.Id) || - (model.Id.HasValue && order.Id == model.Id)) - { - return order.GetViewModel; - } - } - return null; - } - public OrderViewModel? Insert(OrderBindingModel model) - { - model.Id = 1; - foreach (var order in _source.Orders) - { - if (model.Id <= order.Id) - { - model.Id = order.Id + 1; - } - } - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } - _source.Orders.Add(newOrder); - return newOrder.GetViewModel; - } - public OrderViewModel? Update(OrderBindingModel model) - { - foreach (var order in _source.Orders) - { - if (order.Id == model.Id) - { - order.Update(model); - return order.GetViewModel; - } - } - return null; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - for (int i = 0; i < _source.Orders.Count; ++i) - { - if (_source.Orders[i].Id == model.Id) - { - var element = _source.Orders[i]; - _source.Orders.RemoveAt(i); - return element.GetViewModel; - } - } - return null; - } - } + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(GetViewModel(order)); + } + return result; + } + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + + if (model.DateFrom.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) + { + result.Add(GetViewModel(order)); + } + } + } + else if (model.ClientId.HasValue && !model.Id.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.ClientId == model.ClientId) + { + result.Add(GetViewModel(order)); + } + } + } + else if (model.Id.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(GetViewModel(order)); + } + } + } + return result; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + return GetViewModel(order); + } + } + return null; + } + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + foreach (var wood in _source.Woods) + { + if (wood.Id == order.WoodId) + { + viewModel.WoodName = wood.WoodName; + break; + } + } + foreach (var client in _source.Clients) + { + if (client.Id == order.ClientId) + { + viewModel.ClientFIO = client.ClientFIO; + break; + } + } + return viewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return GetViewModel(newOrder); + } + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return GetViewModel(order); + } + } + return null; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return GetViewModel(element); + } + } + return null; + } + } } diff --git a/CarpentryWorkshopListImplement/Models/Client.cs b/CarpentryWorkshopListImplement/Models/Client.cs new file mode 100644 index 0000000..64bb757 --- /dev/null +++ b/CarpentryWorkshopListImplement/Models/Client.cs @@ -0,0 +1,45 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopListImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + }; + } +} diff --git a/CarpentryWorkshopListImplement/Models/Order.cs b/CarpentryWorkshopListImplement/Models/Order.cs index f48279e..2dceec7 100644 --- a/CarpentryWorkshopListImplement/Models/Order.cs +++ b/CarpentryWorkshopListImplement/Models/Order.cs @@ -13,56 +13,51 @@ namespace CarpentryWorkshopListImplement.Models { public class Order : IOrderModel { - public int Id { get; set; } - public int WoodId { get; set; } - public string WoodName { get; set; } = string.Empty; - public int Count { get; set; } - public double Sum { get; set; } - public OrderStatus Status { get; set; } - public DateTime DateCreate { get; set; } - public DateTime? DateImplement { get; set; } - public static Order? Create(OrderBindingModel? model) - { - if (model == null) - { - return null; - } - return new Order() - { - Id = model.Id, - WoodId = model.WoodId, - WoodName = model.WoodName, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; - } - public void Update(OrderBindingModel? model) - { - if (model == null) - { - return; - } - WoodId = model.WoodId; - WoodName = model.WoodName; - Count = model.Count; - Sum = model.Sum; - Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; - } - public OrderViewModel GetViewModel => new() - { - Id = Id, - WoodId = WoodId, - WoodName = WoodName, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement - }; - } + public int WoodId { get; private set; } + public int ClientId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public int Id { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + WoodId = model.WoodId, + ClientId = model.ClientId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + WoodId = WoodId, + ClientId = ClientId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + } } diff --git a/CarpentryWorkshopRestApi/Program.cs b/CarpentryWorkshopRestApi/Program.cs index c27b935..630c40b 100644 --- a/CarpentryWorkshopRestApi/Program.cs +++ b/CarpentryWorkshopRestApi/Program.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; var builder = WebApplication.CreateBuilder(args); builder.Logging.SetMinimumLevel(LogLevel.Trace); +builder.Logging.AddLog4Net("log4net.config"); // Add services to the container. builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/CarpentryWorkshopRestApi/log4net.config b/CarpentryWorkshopRestApi/log4net.config index 4e8a817..f16d6e5 100644 --- a/CarpentryWorkshopRestApi/log4net.config +++ b/CarpentryWorkshopRestApi/log4net.config @@ -1,7 +1,7 @@  - +