доп классы

This commit is contained in:
Андрей Байгулов 2024-05-22 22:23:36 +04:00
parent 86067ecb57
commit b0f39f1fef
12 changed files with 651 additions and 272 deletions

View File

@ -16,151 +16,141 @@ namespace SushiBarBusinessLogic.BusinessLogics
public class OrderLogic : IOrderLogic public class OrderLogic : IOrderLogic
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
static readonly object _locker = new object(); static readonly object _locker = new object();
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
public OrderLogic(ILogger<SushiLogic> Logger, IOrderStorage OrderStorage)
{ {
_logger = Logger; _logger = logger;
_orderStorage = OrderStorage; _orderStorage = orderStorage;
} }
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
public List<OrderViewModel>? ReadList(OrderSearchModel? Model)
{ {
_logger.LogInformation("ReadList. ClientId: {ClientId}. Status: {Status}. ImplementerId: {ImplementerId}. DateFrom: {DateFrom}. DateTo: {DateTo}. OrderId: {Id}", _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); model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
var List = Model is null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(Model); if (list == null)
if (List is null)
{ {
_logger.LogWarning("ReadList return null list"); _logger.LogWarning("ReadList return null list");
return null; return null;
} }
_logger.LogInformation("ReadList. Count: {Count}", List.Count); _logger.LogInformation("ReadList. Count:{Count}", list.Count);
return List; return list;
} }
public bool CreateOrder(OrderBindingModel Model) public bool CreateOrder(OrderBindingModel model)
{ {
CheckModel(Model); CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
if (Model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Invalid order status");
return false; return false;
} model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
Model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(Model) is null)
{ {
_logger.LogWarning("Insert operation failed"); _logger.LogWarning("Insert operation failed");
return false; return false;
} }
return true; return true;
} }
private bool ChangeOrderStatus(OrderBindingModel Model, OrderStatus NewStatus) public bool TakeOrderInWork(OrderBindingModel model)
{
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)
{ {
lock (_locker) 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) 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)
{ {
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}", private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus)
Model.ClientId, Model.Status, Model.ImplementerId, Model.DateFrom, Model.DateTo, Model.Id); {
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"); _logger.LogWarning("ReadElement element not found");
return null; return null;
} }
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
_logger.LogInformation("ReadElement find. Id: {Id}", Order.Id); return element;
return Order;
} }
} }
} }

View File

@ -8,17 +8,17 @@ using System.Threading.Tasks;
namespace SushiBarContracts.BindingModels namespace SushiBarContracts.BindingModels
{ {
public class OrderBindingModel : IOrderModel public class OrderBindingModel : IOrderModel
{ {
public int Id { get; set; } public int Id { get; set; }
public int SushiId { get; set; } public int SushiId { get; set; }
public int ClientId { get; set; } public int ClientId { get; set; }
public int? ImplementerId { get; set; } public int? ImplementerId { get; set; }
public int Count { get; set; } public int Count { get; set; }
public double Sum { get; set; } public double Sum { get; set; }
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; } public DateTime? DateImplement { get; set; }
} }
} }

View File

@ -9,28 +9,35 @@ using System.Threading.Tasks;
namespace SushiBarContracts.ViewModels namespace SushiBarContracts.ViewModels
{ {
public class OrderViewModel : IOrderModel public class OrderViewModel : IOrderModel
{ {
[DisplayName("Номер")] [DisplayName("Номер")]
public int Id { get; set; } public int Id { get; set; }
public int SushiId { get; set; }
public int ClientId { get; set; }
public int? ImplementerId { get; set; } public int? ImplementerId { get; set; }
[DisplayName("Клиент")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Изделие")]
public string SushiName { get; set; } = string.Empty;
[DisplayName("Исполнитель")] [DisplayName("Исполнитель")]
public string? ImplementerFIO { get; set; } = null; 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("Количество")] [DisplayName("Количество")]
public int Count { get; set; } public int Count { get; set; }
[DisplayName("Сумма")]
public double Sum { get; set; } [DisplayName("Сумма")]
[DisplayName("Статус")] public double Sum { get; set; }
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[DisplayName("Дата создания")] [DisplayName("Статус")]
public DateTime DateCreate { get; set; } = DateTime.Now; public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[DisplayName("Дата выполнения")]
public DateTime? DateImplement { get; set; } [DisplayName("Дата создания")]
} public DateTime DateCreate { get; set; } = DateTime.Now;
[DisplayName("Дата выполнения")]
public DateTime? DateImplement { get; set; }
}
} }

View File

@ -6,74 +6,80 @@ using System.ComponentModel.DataAnnotations;
namespace SushiBarDatabaseImplement.Models namespace SushiBarDatabaseImplement.Models
{ {
public class Order : IOrderModel public class Order : IOrderModel
{ {
public int Id { get; private set; } public int Id { get; private set; }
[Required] [Required]
public int ClientId { get; private set; } public int ClientId { get; private set; }
public virtual Client Client { get; private set; } = new(); public virtual Client Client { get; private set; } = new();
[Required]
public int SushiId { get; private set; }
public virtual Sushi Sushi { get; set; } = new();
[Required] [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] [Required]
public int Count { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required] [Required]
public double Sum { get; private set; } public DateTime DateCreate { get; private set; } = DateTime.Now;
[Required] public DateTime? DateImplement { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int? ImplementerId { get; private set; } public int? ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; set; } = new(); public virtual Implementer? Implementer { get; set; } = new();
public static Order Create(SushiBarDatabase context, OrderBindingModel model) public static Order Create(SushiBarDatabase context, OrderBindingModel model)
{ {
return new Order() return new Order()
{ {
Id = model.Id, Id = model.Id,
ClientId = model.ClientId, ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId), Client = context.Clients.First(x => x.Id == model.ClientId),
SushiId = model.SushiId, SushiId = model.SushiId,
Sushi = context.Sushis.First(x => x.Id == model.SushiId), Sushi = context.Sushis.First(x => x.Id == model.SushiId),
Count = model.Count, ImplementerId = model.ImplementerId,
Sum = model.Sum, Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
Status = model.Status, Count = model.Count,
DateCreate = model.DateCreate, Sum = model.Sum,
DateImplement = model.DateImplement, Status = model.Status,
}; DateCreate = model.DateCreate,
} DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model) public void Update(SushiBarDatabase context, OrderBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
Status = model.Status; Status = model.Status;
DateImplement = model.DateImplement; DateImplement = model.DateImplement;
} ImplementerId = model.ImplementerId;
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null;
}
public OrderViewModel GetViewModel => new() public OrderViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,
ClientId = ClientId, ClientId = ClientId,
ClientFIO = Client.ClientFIO, ClientFIO = Client.ClientFIO,
SushiId = SushiId, SushiId = SushiId,
SushiName = Sushi.SushiName, SushiName = Sushi.SushiName,
Count = Count, ImplementerId = ImplementerId,
Sum = Sum, ImplementerFIO = Implementer != null ? Implementer.ImplementerFIO : null,
Status = Status, Count = Count,
DateCreate = DateCreate, Sum = Sum,
DateImplement = DateImplement, Status = Status,
}; DateCreate = DateCreate,
} DateImplement = DateImplement,
};
}
} }

View File

@ -12,7 +12,7 @@ namespace SushiBarDatabaseImplement.Implements
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
using var context = new SushiBarDatabase(); 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<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -65,7 +65,7 @@ namespace SushiBarDatabaseImplement.Implements
{ {
return null; return null;
} }
order.Update(model); order.Update(context, model);
context.SaveChanges(); context.SaveChanges();
return order.GetViewModel; return order.GetViewModel;
} }

View File

@ -8,55 +8,59 @@ using System.Xml.Linq;
namespace SushiBarFileImplement namespace SushiBarFileImplement
{ {
internal class DataFileSingleton internal class DataFileSingleton
{ {
private static DataFileSingleton? instance; private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml"; private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml"; private readonly string OrderFileName = "Order.xml";
private readonly string SushiFileName = "Sushi.xml"; private readonly string SushiFileName = "Sushi.xml";
private readonly string ClientFileName = "Client.xml"; private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; } private readonly string ImplementerFileName = "Implementer.xml";
public List<Order> Orders { get; private set; } public List<Component> Components { get; private set; }
public List<Sushi> Sushis { get; private set; } public List<Order> Orders { get; private set; }
public List<Client> Clients { get; private set; } public List<Sushi> Sushis { get; private set; }
public List<Client> Clients { get; private set; }
public List<Implementer> Implementers { get; private set; }
public static DataFileSingleton GetInstance() public static DataFileSingleton GetInstance()
{ {
if (instance == null) if (instance == null)
{ {
instance = new DataFileSingleton(); instance = new DataFileSingleton();
} }
return instance; return instance;
} }
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", 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 SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", 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 SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement);
private DataFileSingleton() private DataFileSingleton()
{ {
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
} Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction) private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{ {
if (File.Exists(filename)) if (File.Exists(filename))
{ {
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
} }
return new List<T>(); return new List<T>();
} }
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction) private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
{ {
if (data != null) if (data != null)
{ {
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
} }
} }
} }
} }

View File

@ -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)
);
}
}

View File

@ -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<ImplementerViewModel> GetFullList()
{
return _source.Implementers.Select(x => x.GetViewModel).ToList();
}
public List<ImplementerViewModel> 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;
}
}
}

View File

@ -7,27 +7,29 @@ using System.Threading.Tasks;
namespace SushiBarListImplement namespace SushiBarListImplement
{ {
public class DataListSingleton public class DataListSingleton
{ {
private static DataListSingleton? _instance; private static DataListSingleton? _instance;
public List<Component> Components { get; set; } public List<Component> Components { get; set; }
public List<Order> Orders { get; set; } public List<Order> Orders { get; set; }
public List<Sushi> Sushis { get; set; } public List<Sushi> Sushis { get; set; }
public List<Client> Clients { get; set; } public List<Client> Clients { get; set; }
private DataListSingleton() public List<Implementer> Implementers { get; set; }
{ private DataListSingleton()
Components = new List<Component>(); {
Orders = new List<Order>(); Components = new List<Component>();
Sushis = new List<Sushi>(); Orders = new List<Order>();
Clients = new List<Client>(); Sushis = new List<Sushi>();
} Clients = new List<Client>();
public static DataListSingleton GetInstance() Implementers = new List<Implementer>();
{ }
if (_instance == null) public static DataListSingleton GetInstance()
{ {
_instance = new DataListSingleton(); if (_instance == null)
} {
return _instance; _instance = new DataListSingleton();
} }
} return _instance;
}
}
} }

View File

@ -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,
};
}
}

View File

@ -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<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (model == null)
{
return new();
}
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
List<ImplementerViewModel> 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<ImplementerViewModel> GetFullList()
{
var result = new List<ImplementerViewModel>();
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;
}
}
}

View File

@ -37,39 +37,40 @@ namespace SushiBarView
option.SetMinimumLevel(LogLevel.Information); option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config"); option.AddNLog("nlog.config");
}); });
services.AddTransient<IClientStorage, ClientStorage>();
services.AddTransient<IImplementerStorage, ImplementerStorage>();
services.AddTransient<IComponentStorage, ComponentStorage>(); services.AddTransient<IComponentStorage, ComponentStorage>();
services.AddTransient<IOrderStorage, OrderStorage>(); services.AddTransient<IOrderStorage, OrderStorage>();
services.AddTransient<ISushiStorage, SushiStorage>(); services.AddTransient<ISushiStorage, SushiStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>(); services.AddTransient<IClientStorage, ClientStorage>();
services.AddTransient<IImplementerStorage, ImplementerStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>();
services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<ISushiLogic, SushiLogic>();
services.AddTransient<IClientLogic, ClientLogic>(); services.AddTransient<IClientLogic, ClientLogic>();
services.AddTransient<IComponentLogic, ComponentLogic>(); services.AddTransient<IComponentLogic, ComponentLogic>();
services.AddTransient<IImplementerLogic, ImplementerLogic>(); services.AddTransient<IImplementerLogic, ImplementerLogic>();
services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<ISushiLogic, SushiLogic>();
services.AddTransient<IReportLogic, ReportLogic>();
services.AddTransient<IWorkProcess, WorkModeling>(); services.AddTransient<IWorkProcess, WorkModeling>();
services.AddTransient<FormMain>();
services.AddTransient<FormComponent>();
services.AddTransient<FormComponents>();
services.AddTransient<FormCreateOrder>();
services.AddTransient<FormSushis>();
services.AddTransient<FormSushiComponent>();
services.AddTransient<FormSushi>();
services.AddTransient<FormReportOrders>();
services.AddTransient<FormReportSushiComponents>();
services.AddTransient<FormImplementers>();
services.AddTransient<FormImplementer>();
services.AddTransient<IReportLogic, ReportLogic>();
services.AddTransient<FormClients>();
services.AddTransient<EntityFrameworkDesignServicesBuilder>();
services.AddTransient<AbstractSaveToWord, SaveToWord>(); services.AddTransient<AbstractSaveToWord, SaveToWord>();
services.AddTransient<AbstractSaveToExcel, SaveToExcel>(); services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
services.AddTransient<AbstractSaveToPdf, SaveToPdf>(); services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
services.AddTransient<FormMain>(); services.AddTransient<FormMain>();
services.AddTransient<FormComponent>();
services.AddTransient<FormComponents>();
services.AddTransient<FormCreateOrder>();
services.AddTransient<FormSushi>();
services.AddTransient<FormSushiComponent>();
services.AddTransient<FormSushis>();
services.AddTransient<FormReportSushiComponents>();
services.AddTransient<FormReportOrders>();
services.AddTransient<FormClients>();
services.AddTransient<EntityFrameworkDesignServicesBuilder>();
services.AddTransient<FormImplementers>();
services.AddTransient<FormImplementer>();
} }
} }
} }