я не придумал

This commit is contained in:
Николай 2023-03-25 20:38:11 +04:00
parent 2ecd2d7e9a
commit 32132b644a
22 changed files with 444 additions and 22 deletions

View File

@ -1,4 +1,5 @@
using FoodOrdersContracts.BindingModels;

using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;

View File

@ -48,6 +48,7 @@ namespace FoodOrdersView
services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<IDishLogic, DishLogic>();
services.AddTransient<IReportLogic, ReportLogic>();
services.AddTransient<IClientLogic, ClientLogic>();
services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
services.AddTransient<AbstractSaveToWord, SaveToWord>();

View File

@ -1,35 +1,117 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.BusinessLogicsContracts;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace FoodOrdersBusinessLogic.BusinessLogics
{
public class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _clienttStorage;
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage ClientStorage)
{
_logger = logger;
_clienttStorage = ClientStorage;
}
public bool Create(ClientBindingModel model)
{
throw new NotImplementedException();
CheckModel(model);
if (_clienttStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
throw new NotImplementedException();
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_clienttStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ClientViewModel? ReadElement(ClientSearchModel model)
{
throw new NotImplementedException();
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("Client. ClientFIO:{ClientFIO}. Email:{Email}. Id:{Id}", model.ClientFIO, model.Email, model.Id);
var element = _clienttStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
throw new NotImplementedException();
_logger.LogInformation("Client. ClientFIO:{ClientFIO}. Email:{Email}. Id:{Id}", model?.ClientFIO, model?.Email, model?.Id);
var list = model == null ? _clienttStorage.GetFullList() : _clienttStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(ClientBindingModel model)
{
throw new NotImplementedException();
CheckModel(model);
if (_clienttStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет логина клиента", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password));
}
_logger.LogInformation("Client. ClientFIO:{ClientFIO}. Email:{Email}. Password:{Password} Id:{Id}", model.ClientFIO, model.Email, model.Password, model.Id);
var element = _clienttStorage.GetElement(new ClientSearchModel
{
Email = model.Email
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с таким логином уже есть");
}
}
}
}

View File

@ -11,8 +11,7 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IComponentStorage _componentStorage;
public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage
ComponentStorage)
public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage ComponentStorage)
{
_logger = logger;
_componentStorage = ComponentStorage;

View File

@ -78,6 +78,10 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
{
throw new ArgumentNullException("Некорректный идентификатор у продукта", nameof(model.Id));
}
if (model.ClientId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество продуктов в заказе должно быть больше 0", nameof(model.Count));

View File

@ -13,6 +13,11 @@ namespace FoodOrdersContracts.ViewModels
[DisplayName("Набор")]
public string DishName { get; set; } = string.Empty;
public int ClientId { get; set; }
[DisplayName("Клиент")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Количество")]
public int Count { get; set; }

View File

@ -5,6 +5,7 @@ namespace FoodOrdersDataModels.Models
public interface IOrderModel : IId
{
int DishId { get; }
int ClientId { get; }
int Count { get; }
double Sum { get; }
OrderStatus Status { get; }

View File

@ -21,5 +21,7 @@ namespace FoodOrdersDatabaseImplement
public virtual DbSet<DishComponent> DishComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { set; get; }
}
}

View File

@ -2,6 +2,7 @@
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDatabaseImplement.Models;
namespace FoodOrdersDatabaseImplement.Implements
{
@ -9,32 +10,76 @@ namespace FoodOrdersDatabaseImplement.Implements
{
public ClientViewModel? Delete(ClientBindingModel model)
{
throw new NotImplementedException();
using var context = new FoodOrdersDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new FoodOrdersDatabase();
return context.Clients
.FirstOrDefault(x => (x.Email == model.Email) || (x.Id == model.Id))
?.GetViewModel;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
throw new NotImplementedException();
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new FoodOrdersDatabase();
return context.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
}
public List<ClientViewModel> GetFullList()
{
throw new NotImplementedException();
using var context = new FoodOrdersDatabase();
return context.Clients
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
throw new NotImplementedException();
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
using var context = new FoodOrdersDatabase();
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
throw new NotImplementedException();
using var context = new FoodOrdersDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
}
}

View File

@ -21,6 +21,14 @@ namespace FoodOrdersDatabaseImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new FoodOrdersDatabase();
if (model.ClientId.HasValue)
{
return context.Orders
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
return context.Orders

View File

@ -0,0 +1,204 @@
// <auto-generated />
using System;
using FoodOrdersDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FoodOrdersDatabaseImplement.Migrations
{
[DbContext(typeof(FoodOrdersDatabase))]
partial class FoodOrdersDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ClientFIO")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Clients", (string)null);
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components", (string)null);
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Dish", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("DishName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Dishes", (string)null);
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.DishComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("DishId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("DishId");
b.ToTable("DishComponents", (string)null);
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ClientId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("DishId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("DishId");
b.ToTable("Orders", (string)null);
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.DishComponent", b =>
{
b.HasOne("FoodOrdersDatabaseImplement.Models.Component", "Component")
.WithMany("DishComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FoodOrdersDatabaseImplement.Models.Dish", "Dish")
.WithMany("Components")
.HasForeignKey("DishId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Dish");
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Order", b =>
{
b.HasOne("FoodOrdersDatabaseImplement.Models.Client", "Client")
.WithMany()
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FoodOrdersDatabaseImplement.Models.Dish", "Dish")
.WithMany()
.HasForeignKey("DishId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Dish");
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Component", b =>
{
b.Navigation("DishComponents");
});
modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Dish", b =>
{
b.Navigation("Components");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -12,6 +12,8 @@ namespace FoodOrdersDatabaseImplement.Models
[Required]
public int DishId { get; set; }
[Required]
public int ClientId { get; set; }
[Required]
public int Count { get; set; }
@ -27,6 +29,8 @@ namespace FoodOrdersDatabaseImplement.Models
public virtual Dish Dish { get; set; }
public virtual Client Client { get; set; }
public DateTime? DateImplement { get; set; }
public static Order? Create(OrderBindingModel? model)
@ -39,6 +43,7 @@ namespace FoodOrdersDatabaseImplement.Models
{
Id = model.Id,
DishId = model.DishId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -61,6 +66,8 @@ namespace FoodOrdersDatabaseImplement.Models
{
Id = Id,
DishId = DishId,
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
Count = Count,
Sum = Sum,
Status = Status,

View File

@ -8,9 +8,12 @@ namespace FoodOrdersFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DishFileName = "Dish.xml";
private readonly string ClientFileName = "Clients.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Dish> Dishes { get; private set; }
public List<Client> Clients { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -22,11 +25,13 @@ namespace FoodOrdersFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveDishes() => SaveData(Dishes, DishFileName, "Dishes", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Dishes = LoadData(DishFileName, "Dish", x => Dish.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{

View File

@ -21,6 +21,13 @@ namespace FoodOrdersFileImplement.Implements
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (model.ClientId.HasValue)
{
return _source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => GetViewModel(x))
.ToList();
}
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
return _source.Orders
@ -44,6 +51,7 @@ namespace FoodOrdersFileImplement.Implements
{
var viewModel = order.GetViewModel;
viewModel.DishName = _source.Dishes.FirstOrDefault(x => (x.Id == order.DishId))?.DishName;
viewModel.ClientFIO = _source.Clients.FirstOrDefault(x => (x.Id == order.ClientId))?.ClientFIO;
return viewModel;
}

View File

@ -1,10 +1,9 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using System.Reflection;
using System.Xml.Linq;
namespace FoodOrdersFileImplement.ViewModels
namespace FoodOrdersFileImplement.Models
{
public class Client : IClientModel
{

View File

@ -2,6 +2,7 @@
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Enums;
using FoodOrdersDataModels.Models;
using System.Reflection;
using System.Reflection.Metadata;
using System.Xml.Linq;
@ -11,6 +12,7 @@ namespace FoodOrdersFileImplement.Models
{
public int Id { get; private set; }
public int DishId { 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; }
@ -26,6 +28,7 @@ namespace FoodOrdersFileImplement.Models
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
DishId = Convert.ToInt32(element.Element("DishId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
@ -45,6 +48,7 @@ namespace FoodOrdersFileImplement.Models
{
Id = model.Id,
DishId = model.DishId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -65,6 +69,7 @@ namespace FoodOrdersFileImplement.Models
{
Id = Id,
DishId = DishId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
@ -76,6 +81,7 @@ namespace FoodOrdersFileImplement.Models
"Order",
new XAttribute("Id", Id),
new XElement("DishId", DishId.ToString()),
new XElement("ClientId", ClientId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),

View File

@ -8,11 +8,13 @@ namespace FoodOrdersListImplement
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Dish> Dishes { get; set; }
public List<Client> Clients { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Dishes = new List<Dish>();
Clients = new List<Client>();
}
public static DataListSingleton GetInstance()
{

View File

@ -7,6 +7,11 @@ namespace FoodOrdersListImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataListSingleton _source;
public ClientStorage()
{
_source = DataListSingleton.GetInstance();
}
public ClientViewModel? Delete(ClientBindingModel model)
{
throw new NotImplementedException();
@ -19,12 +24,29 @@ namespace FoodOrdersListImplement.Implements
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
throw new NotImplementedException();
var result = new List<ClientViewModel>();
if (string.IsNullOrEmpty(model.Email))
{
return result;
}
foreach (var component in _source.Clients)
{
if (component.Email.Contains(model.Email))
{
result.Add(component.GetViewModel);
}
}
return result;
}
public List<ClientViewModel> GetFullList()
{
throw new NotImplementedException();
var result = new List<ClientViewModel>();
foreach (var component in _source.Clients)
{
result.Add(component.GetViewModel);
}
return result;
}
public ClientViewModel? Insert(ClientBindingModel model)

View File

@ -22,8 +22,7 @@ namespace FoodOrdersListImplement.Implements
}
return result;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
var result = new List<ComponentViewModel>();
if (string.IsNullOrEmpty(model.ComponentName))

View File

@ -30,6 +30,17 @@ namespace FoodOrdersListImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (model.ClientId.HasValue)
{
foreach (var order in _source.Orders)
{
if (order.ClientId == model.ClientId)
{
result.Add(GetViewModel(order));
}
}
return result;
}
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
foreach (var order in _source.Orders)
@ -78,6 +89,14 @@ namespace FoodOrdersListImplement.Implements
break;
}
}
foreach (var client in _source.Clients)
{
if (client.Id == order.ClientId)
{
viewModel.ClientFIO = client.ClientFIO;
break;
}
}
return viewModel;
}

View File

@ -3,7 +3,7 @@ using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using System.Reflection;
namespace FoodOrdersListImplement.ViewModels
namespace FoodOrdersListImplement.Models
{
public class Client : IClientModel
{

View File

@ -9,6 +9,7 @@ namespace FoodOrdersListImplement.Models
public class Order : IOrderModel
{
public int Id { get; private set; }
public int ClientId { get; private set; }
public int DishId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
@ -24,8 +25,9 @@ namespace FoodOrdersListImplement.Models
}
return new Order()
{
Id = model.Id,
Id = model.Id,
DishId = model.DishId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -46,6 +48,7 @@ namespace FoodOrdersListImplement.Models
{
Id = Id,
DishId = DishId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,