diff --git a/PlumbingRepair/App.config b/PlumbingRepair/App.config index 70a9fd5..3666028 100644 --- a/PlumbingRepair/App.config +++ b/PlumbingRepair/App.config @@ -5,7 +5,7 @@ - - + + \ No newline at end of file diff --git a/PlumbingRepair/PlumbingRepair.csproj b/PlumbingRepair/PlumbingRepair.csproj index e1d3bc5..24f8e5d 100644 --- a/PlumbingRepair/PlumbingRepair.csproj +++ b/PlumbingRepair/PlumbingRepair.csproj @@ -28,6 +28,9 @@ + + Always + Always diff --git a/PlumbingRepair/Program.cs b/PlumbingRepair/Program.cs index 5fa1c75..1fcfff1 100644 --- a/PlumbingRepair/Program.cs +++ b/PlumbingRepair/Program.cs @@ -7,6 +7,8 @@ using NLog.Extensions.Logging; using PlumbingRepairBusinessLogic.BusinessLogic; using PlumbingRepairBusinessLogic.OfficePackage; using PlumbingRepairBusinessLogic.OfficePackage.Implements; +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairBusinessLogic.MailWorker; namespace PlumbingRepair { @@ -26,7 +28,27 @@ namespace PlumbingRepair var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); + try + { + var mailSender = _serviceProvider.GetService(); + mailSender?.MailConfig(new MailConfigBindingModel + { + MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty, + MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty, + SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty, + SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]), + PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty, + PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"]) + }); + + var timer = new System.Threading.Timer(new TimerCallback(MailCheck!), null, 0, 100000); + } + catch (Exception ex) + { + var logger = _serviceProvider.GetService(); + logger?.LogError(ex, "Error"); + } + Application.Run(_serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) { @@ -35,33 +57,39 @@ namespace PlumbingRepair 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.AddSingleton(); 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(); } - } + + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + } } \ No newline at end of file diff --git a/PlumbingRepairBusinessLogic/BusinessLogic/ClientLogic.cs b/PlumbingRepairBusinessLogic/BusinessLogic/ClientLogic.cs index 3d606d5..44b45e2 100644 --- a/PlumbingRepairBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/PlumbingRepairBusinessLogic/BusinessLogic/ClientLogic.cs @@ -4,6 +4,7 @@ using PlumbingRepairContracts.BusinessLogicsContracts; using PlumbingRepairContracts.SearchModels; using PlumbingRepairContracts.StoragesContracts; using PlumbingRepairContracts.ViewModels; +using System.Text.RegularExpressions; namespace PlumbingRepairBusinessLogic.BusinessLogic { @@ -97,7 +98,16 @@ namespace PlumbingRepairBusinessLogic.BusinessLogic { throw new ArgumentNullException("Нет пароля учетной записи клиента", nameof(model.ClientFIO)); } - _logger.LogInformation("Client. ClientFIO:{ClientFIO}. Email:{Email}. Password:{Password}. Id:{Id}", model.ClientFIO, model.Email, model.Password, model.Id); + if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный email", nameof(model.Email)); + } + + if (!Regex.IsMatch(model.Password, @"^^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный пароль", nameof(model.Password)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}. Email:{Email}. Password:{Password}. Id:{Id}", model.ClientFIO, model.Email, model.Password, model.Id); var element = _clientStorage.GetElement(new ClientSearchModel { Email = model.Email diff --git a/PlumbingRepairBusinessLogic/BusinessLogic/OrderLogic.cs b/PlumbingRepairBusinessLogic/BusinessLogic/OrderLogic.cs index 513eba8..109c40c 100644 --- a/PlumbingRepairBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/PlumbingRepairBusinessLogic/BusinessLogic/OrderLogic.cs @@ -5,41 +5,49 @@ using PlumbingRepairContracts.StoragesContracts; using PlumbingRepairContracts.ViewModels; using Microsoft.Extensions.Logging; using PlumbingRepairDataModels.Enums; +using PlumbingRepairBusinessLogic.MailWorker; namespace PlumbingRepairBusinessLogic.BusinessLogic { - public class OrderLogic : IOrderLogic - { - private readonly ILogger _logger; - private readonly IOrderStorage _orderStorage; + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) - { - _logger = logger; - _orderStorage = orderStorage; - } + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) + { + _logger = logger; + _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; + } - public bool CreateOrder(OrderBindingModel model) - { - CheckModel(model); + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); - if (model.Status != OrderStatus.Неизвестен) - { - _logger.LogWarning("Insert operation failed. Order status incorrect."); - return false; - } + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } - model.Status = OrderStatus.Принят; + model.Status = OrderStatus.Принят; + var result = _orderStorage.Insert(model); - if (_orderStorage.Insert(model) == null) - { - model.Status = OrderStatus.Неизвестен; - _logger.LogWarning("Insert operation failed"); - return false; - } + if (result == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } - return true; - } + SendOrderMessage(result.ClientId, $"Ремонт сантехники, Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); + + return true; + } public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); @@ -68,75 +76,97 @@ namespace PlumbingRepairBusinessLogic.BusinessLogic model.DateImplement = viewModel.DateImplement; } CheckModel(model); - if (_orderStorage.Update(model) == null) + + var result = _orderStorage.Update(model); + if (result == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } + SendOrderMessage(result.ClientId, $"Ремонт сантехники, Заказ №{result.Id}", $"Заказ №{model.Id} изменен статус на {result.Status}"); + return true; + } + + private bool SendOrderMessage(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + + if (client == null) + { + return false; + } + + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; } public bool TakeOrderInWork(OrderBindingModel model) - { - return StatusUpdate(model, OrderStatus.Выполняется); - } + { + return StatusUpdate(model, OrderStatus.Выполняется); + } - public bool DeliveryOrder(OrderBindingModel model) - { + public bool DeliveryOrder(OrderBindingModel model) + { return StatusUpdate(model, OrderStatus.Выдан); } - public bool FinishOrder(OrderBindingModel model) - { + public bool FinishOrder(OrderBindingModel model) + { return StatusUpdate(model, OrderStatus.Готов); - } + } - public List? ReadList(OrderSearchModel? model) - { - _logger.LogInformation("Order. OrderId:{Id}", model?.Id); + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. OrderId:{Id}", model?.Id); - var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } + 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; + } - private void CheckModel(OrderBindingModel model, bool withParams = true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } - if (!withParams) - { - return; - } + if (!withParams) + { + return; + } - if (model.WorkId < 0) - { - throw new ArgumentNullException("Некорректный идентификатор Работы", nameof(model.WorkId)); - } + if (model.WorkId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.WorkId)); + } - if (model.Count <= 0) - { - throw new ArgumentNullException("Количество Работ в заказе должно быть больше 0", nameof(model.Count)); - } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); + } - if (model.Sum <= 0) - { - throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); - } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } - _logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. WorkId: { WorkId}", model.Id, model.Sum, model.WorkId); - } + _logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. WorkId: { WorkId}", model.Id, model.Sum, model.WorkId); + } public OrderViewModel? ReadElement(OrderSearchModel model) { @@ -155,4 +185,4 @@ namespace PlumbingRepairBusinessLogic.BusinessLogic return element; } } -} +} \ No newline at end of file diff --git a/PlumbingRepairBusinessLogic/PlumbingRepairBusinessLogic.csproj b/PlumbingRepairBusinessLogic/PlumbingRepairBusinessLogic.csproj index 475b74f..fcd0a62 100644 --- a/PlumbingRepairBusinessLogic/PlumbingRepairBusinessLogic.csproj +++ b/PlumbingRepairBusinessLogic/PlumbingRepairBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/PlumbingRepairDatabaseImplement/Implements/ClientStorage.cs b/PlumbingRepairDatabaseImplement/Implements/ClientStorage.cs index b2a6b8e..ebd3dfd 100644 --- a/PlumbingRepairDatabaseImplement/Implements/ClientStorage.cs +++ b/PlumbingRepairDatabaseImplement/Implements/ClientStorage.cs @@ -13,7 +13,6 @@ namespace PlumbingRepairDatabaseImplement.Implements { using var context = new PlumbingRepairDataBase(); return context.Clients - .Include(x => x.Orders) .Select(x => x.GetViewModel) .ToList(); } @@ -23,11 +22,15 @@ namespace PlumbingRepairDatabaseImplement.Implements { return new(); } - if (!string.IsNullOrEmpty(model.Email)) + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + if (!string.IsNullOrEmpty(model.Email)) { using var context = new PlumbingRepairDataBase(); return context.Clients - .Include(x => x.Orders) .Where(x => x.Email.Contains(model.Email)) .Select(x => x.GetViewModel) .ToList(); @@ -36,19 +39,22 @@ namespace PlumbingRepairDatabaseImplement.Implements } public ClientViewModel? GetElement(ClientSearchModel model) { - using var context = new PlumbingRepairDataBase(); - if (model.Id.HasValue) - { - return context.Clients - .FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel; - } - else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) - { - return context.Clients - .FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))?.GetViewModel; - } - return new(); - } + if (string.IsNullOrEmpty(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password) && + !model.Id.HasValue) + { + return null; + } + using var context = new PlumbingRepairDataBase(); + var temp = context.Clients + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && + (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && + (!model.Id.HasValue || x.Id == model.Id)) + ?.GetViewModel; + return temp; + } public ClientViewModel? Insert(ClientBindingModel model) { var newClient = Client.Create(model); @@ -77,7 +83,6 @@ namespace PlumbingRepairDatabaseImplement.Implements { using var context = new PlumbingRepairDataBase(); var element = context.Clients - .Include(x => x.Orders) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { diff --git a/PlumbingRepairDatabaseImplement/Migrations/20231108203013_init.Designer.cs b/PlumbingRepairDatabaseImplement/Migrations/20231108203013_init.Designer.cs new file mode 100644 index 0000000..4fd65fc --- /dev/null +++ b/PlumbingRepairDatabaseImplement/Migrations/20231108203013_init.Designer.cs @@ -0,0 +1,300 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PlumbingRepairDatabaseImplement; + +#nullable disable + +namespace PlumbingRepairDatabaseImplement.Migrations +{ + [DbContext(typeof(PlumbingRepairDataBase))] + [Migration("20231108203013_init")] + partial class init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Message", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ImplementerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("WorkId") + .HasColumnType("int"); + + b.Property("WorkName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("WorkId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("WorkName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Works"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.WorkComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("WorkId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("WorkId"); + + b.ToTable("WorkComponents"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Message", b => + { + b.HasOne("PlumbingRepairDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Order", b => + { + b.HasOne("PlumbingRepairDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PlumbingRepairDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work") + .WithMany("Orders") + .HasForeignKey("WorkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Work"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.WorkComponent", b => + { + b.HasOne("PlumbingRepairDatabaseImplement.Models.Component", "Component") + .WithMany("WorkComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work") + .WithMany("Components") + .HasForeignKey("WorkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Work"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Component", b => + { + b.Navigation("WorkComponents"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PlumbingRepairDatabaseImplement/Migrations/20231108203013_init.cs b/PlumbingRepairDatabaseImplement/Migrations/20231108203013_init.cs new file mode 100644 index 0000000..b3e6616 --- /dev/null +++ b/PlumbingRepairDatabaseImplement/Migrations/20231108203013_init.cs @@ -0,0 +1,215 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PlumbingRepairDatabaseImplement.Migrations +{ + /// + public partial class init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ClientFIO = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Implementers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ImplementerFIO = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + WorkExperience = table.Column(type: "int", nullable: false), + Qualification = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Implementers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Works", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WorkName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Works", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + MessageId = table.Column(type: "nvarchar(450)", nullable: false), + ClientId = table.Column(type: "int", nullable: true), + SenderName = table.Column(type: "nvarchar(max)", nullable: false), + DateDelivery = table.Column(type: "datetime2", nullable: false), + Subject = table.Column(type: "nvarchar(max)", nullable: false), + Body = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.MessageId); + table.ForeignKey( + name: "FK_Messages_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WorkId = table.Column(type: "int", nullable: false), + WorkName = table.Column(type: "nvarchar(max)", nullable: false), + ClientId = table.Column(type: "int", nullable: false), + ImplementerId = table.Column(type: "int", nullable: true), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + column: x => x.ImplementerId, + principalTable: "Implementers", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_Orders_Works_WorkId", + column: x => x.WorkId, + principalTable: "Works", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "WorkComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WorkId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WorkComponents", x => x.Id); + table.ForeignKey( + name: "FK_WorkComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_WorkComponents_Works_WorkId", + column: x => x.WorkId, + principalTable: "Works", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Messages_ClientId", + table: "Messages", + column: "ClientId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ImplementerId", + table: "Orders", + column: "ImplementerId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_WorkId", + table: "Orders", + column: "WorkId"); + + migrationBuilder.CreateIndex( + name: "IX_WorkComponents_ComponentId", + table: "WorkComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_WorkComponents_WorkId", + table: "WorkComponents", + column: "WorkId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Messages"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "WorkComponents"); + + migrationBuilder.DropTable( + name: "Clients"); + + migrationBuilder.DropTable( + name: "Implementers"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Works"); + } + } +} diff --git a/PlumbingRepairDatabaseImplement/Migrations/PlumbingRepairDataBaseModelSnapshot.cs b/PlumbingRepairDatabaseImplement/Migrations/PlumbingRepairDataBaseModelSnapshot.cs new file mode 100644 index 0000000..1a22350 --- /dev/null +++ b/PlumbingRepairDatabaseImplement/Migrations/PlumbingRepairDataBaseModelSnapshot.cs @@ -0,0 +1,297 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PlumbingRepairDatabaseImplement; + +#nullable disable + +namespace PlumbingRepairDatabaseImplement.Migrations +{ + [DbContext(typeof(PlumbingRepairDataBase))] + partial class PlumbingRepairDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Message", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ImplementerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("WorkId") + .HasColumnType("int"); + + b.Property("WorkName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("WorkId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("WorkName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Works"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.WorkComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("WorkId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("WorkId"); + + b.ToTable("WorkComponents"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Message", b => + { + b.HasOne("PlumbingRepairDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Order", b => + { + b.HasOne("PlumbingRepairDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PlumbingRepairDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work") + .WithMany("Orders") + .HasForeignKey("WorkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Work"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.WorkComponent", b => + { + b.HasOne("PlumbingRepairDatabaseImplement.Models.Component", "Component") + .WithMany("WorkComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work") + .WithMany("Components") + .HasForeignKey("WorkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Work"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Component", b => + { + b.Navigation("WorkComponents"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PlumbingRepairDatabaseImplement/Models/Client.cs b/PlumbingRepairDatabaseImplement/Models/Client.cs index 79e7c73..c96c681 100644 --- a/PlumbingRepairDatabaseImplement/Models/Client.cs +++ b/PlumbingRepairDatabaseImplement/Models/Client.cs @@ -17,7 +17,9 @@ namespace PlumbingRepairDatabaseImplement.Models public string Password { get; set; } = string.Empty; [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); - public static Client? Create(ClientBindingModel? model) + [ForeignKey("ClientId")] + public virtual List Messages { get; set; } = new(); + public static Client? Create(ClientBindingModel? model) { if (model == null) { diff --git a/PlumbingRepairDatabaseImplement/PlumbingRepairDataBase.cs b/PlumbingRepairDatabaseImplement/PlumbingRepairDataBase.cs index 7e7c5b9..77174d5 100644 --- a/PlumbingRepairDatabaseImplement/PlumbingRepairDataBase.cs +++ b/PlumbingRepairDatabaseImplement/PlumbingRepairDataBase.cs @@ -9,7 +9,7 @@ namespace PlumbingRepairDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=IS-424-2-01\SQLEXPRESS;Initial Catalog=PlumbingRepairDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-94MIQ0M\SQLEXPRESS;Initial Catalog=PlumbingRepairDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/PlumbingRepairDatabaseImplement/PlumbingRepairDatabaseImplement.csproj b/PlumbingRepairDatabaseImplement/PlumbingRepairDatabaseImplement.csproj index ba8924d..5588848 100644 --- a/PlumbingRepairDatabaseImplement/PlumbingRepairDatabaseImplement.csproj +++ b/PlumbingRepairDatabaseImplement/PlumbingRepairDatabaseImplement.csproj @@ -7,9 +7,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive