From 3f9ba1700369452abc5cb80655fe50a66fd6783c Mon Sep 17 00:00:00 2001 From: tellsense Date: Tue, 7 May 2024 21:09:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2?= =?UTF-8?q?.=20=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=91?= =?UTF-8?q?=D0=94.=20=D0=9C=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SUBD/SUBD/ComponentEntity/Component.cs | 56 +++++++++++ SUBD/SUBD/ComponentEntity/ComponentLogic.cs | 94 +++++++++++++++++++ SUBD/SUBD/ComponentEntity/ComponentStorage.cs | 80 ++++++++++++++++ .../ComponentEntity/ComponentsDatabase.cs | 28 ++++++ .../SUBD/ComponentEntity/IComponentStorage.cs | 18 ++++ .../20240507170902_Initial.Designer.cs | 49 ++++++++++ .../SUBD/Migrations/20240507170902_Initial.cs | 36 +++++++ .../ComponentsDatabaseModelSnapshot.cs | 46 +++++++++ SUBD/SUBD/Program.cs | 20 +++- SUBD/SUBD/SUBD.csproj | 14 +++ 10 files changed, 438 insertions(+), 3 deletions(-) create mode 100644 SUBD/SUBD/ComponentEntity/Component.cs create mode 100644 SUBD/SUBD/ComponentEntity/ComponentLogic.cs create mode 100644 SUBD/SUBD/ComponentEntity/ComponentStorage.cs create mode 100644 SUBD/SUBD/ComponentEntity/ComponentsDatabase.cs create mode 100644 SUBD/SUBD/ComponentEntity/IComponentStorage.cs create mode 100644 SUBD/SUBD/Migrations/20240507170902_Initial.Designer.cs create mode 100644 SUBD/SUBD/Migrations/20240507170902_Initial.cs create mode 100644 SUBD/SUBD/Migrations/ComponentsDatabaseModelSnapshot.cs diff --git a/SUBD/SUBD/ComponentEntity/Component.cs b/SUBD/SUBD/ComponentEntity/Component.cs new file mode 100644 index 0000000..7713b79 --- /dev/null +++ b/SUBD/SUBD/ComponentEntity/Component.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SUBD.ComponentEntity +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/SUBD/SUBD/ComponentEntity/ComponentLogic.cs b/SUBD/SUBD/ComponentEntity/ComponentLogic.cs new file mode 100644 index 0000000..09f40bd --- /dev/null +++ b/SUBD/SUBD/ComponentEntity/ComponentLogic.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SUBD.ComponentEntity +{ + public class ComponentLogic : IComponentLogic + { + private readonly IComponentStorage _componentStorage; + public ComponentLogic(IComponentStorage componentStorage) + { + _componentStorage = componentStorage; + } + public List? ReadList(ComponentSearchModel? model) + { + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _componentStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + return false; + } + return true; + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + return false; + } + return true; + } + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + if (_componentStorage.Delete(model) == null) + { + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия компонента", + nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/SUBD/SUBD/ComponentEntity/ComponentStorage.cs b/SUBD/SUBD/ComponentEntity/ComponentStorage.cs new file mode 100644 index 0000000..f0fa657 --- /dev/null +++ b/SUBD/SUBD/ComponentEntity/ComponentStorage.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SUBD.ComponentEntity +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComponentsDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ComponentsDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new ComponentsDatabase(); + return context.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ComponentsDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComponentsDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ComponentsDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SUBD/SUBD/ComponentEntity/ComponentsDatabase.cs b/SUBD/SUBD/ComponentEntity/ComponentsDatabase.cs new file mode 100644 index 0000000..ccc5434 --- /dev/null +++ b/SUBD/SUBD/ComponentEntity/ComponentsDatabase.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace SUBD.ComponentEntity +{ + public class ComponentsDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder) + { + if (OptionsBuilder.IsConfigured == false) + { + OptionsBuilder.UseNpgsql(@"Host=localhost;Database=Components;Username=postgres;Password=postgres"); + } + + base.OnConfiguring(OptionsBuilder); + + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + + public virtual DbSet Components { set; get; } + } +} diff --git a/SUBD/SUBD/ComponentEntity/IComponentStorage.cs b/SUBD/SUBD/ComponentEntity/IComponentStorage.cs new file mode 100644 index 0000000..ed80cea --- /dev/null +++ b/SUBD/SUBD/ComponentEntity/IComponentStorage.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SUBD.ComponentEntity +{ + public interface IComponentStorage + { + List GetFullList(); + List GetFilteredList(ComponentSearchModel model); + ComponentViewModel? GetElement(ComponentSearchModel model); + ComponentViewModel? Insert(ComponentBindingModel model); + ComponentViewModel? Update(ComponentBindingModel model); + ComponentViewModel? Delete(ComponentBindingModel model); + } +} diff --git a/SUBD/SUBD/Migrations/20240507170902_Initial.Designer.cs b/SUBD/SUBD/Migrations/20240507170902_Initial.Designer.cs new file mode 100644 index 0000000..9b06344 --- /dev/null +++ b/SUBD/SUBD/Migrations/20240507170902_Initial.Designer.cs @@ -0,0 +1,49 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using SUBD.ComponentEntity; + +#nullable disable + +namespace SUBD.Migrations +{ + [DbContext(typeof(ComponentsDatabase))] + [Migration("20240507170902_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("SUBD.ComponentEntity.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SUBD/SUBD/Migrations/20240507170902_Initial.cs b/SUBD/SUBD/Migrations/20240507170902_Initial.cs new file mode 100644 index 0000000..9abecb5 --- /dev/null +++ b/SUBD/SUBD/Migrations/20240507170902_Initial.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace SUBD.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Components"); + } + } +} diff --git a/SUBD/SUBD/Migrations/ComponentsDatabaseModelSnapshot.cs b/SUBD/SUBD/Migrations/ComponentsDatabaseModelSnapshot.cs new file mode 100644 index 0000000..8b8d697 --- /dev/null +++ b/SUBD/SUBD/Migrations/ComponentsDatabaseModelSnapshot.cs @@ -0,0 +1,46 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using SUBD.ComponentEntity; + +#nullable disable + +namespace SUBD.Migrations +{ + [DbContext(typeof(ComponentsDatabase))] + partial class ComponentsDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("SUBD.ComponentEntity.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SUBD/SUBD/Program.cs b/SUBD/SUBD/Program.cs index dcba0ec..4736f12 100644 --- a/SUBD/SUBD/Program.cs +++ b/SUBD/SUBD/Program.cs @@ -1,17 +1,31 @@ +using SUBD.ComponentEntity; +using Microsoft.Extensions.DependencyInjection; +using System; + namespace SUBD { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// [STAThread] static void Main() { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormSushi()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + Application.Run(_serviceProvider.GetRequiredService()); + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/SUBD/SUBD/SUBD.csproj b/SUBD/SUBD/SUBD.csproj index 663fdb8..a4a5d31 100644 --- a/SUBD/SUBD/SUBD.csproj +++ b/SUBD/SUBD/SUBD.csproj @@ -8,4 +8,18 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + \ No newline at end of file