diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index dfbd4c1..48c3bbc 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -43,7 +43,7 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics Count = model.Count, Sum = model.Sum, Status = OrderStatus.Принят, - DateCreate = DateTime.Now + DateCreate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc) }); ; return true; } @@ -96,7 +96,7 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics Count = order.Count, Sum = order.Sum, DateCreate = order.DateCreate, - DateImplement = DateTime.Now, + DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), Status = OrderStatus.Готов }); return true; diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs index c44a424..e6d0d44 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs @@ -16,7 +16,7 @@ namespace CarpentryWorkshopContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs index 7276b02..ebc1d47 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs @@ -10,11 +10,7 @@ namespace CarpentryWorkshopDataModels.Models public interface IOrderModel : IId { int WoodId { get; } - string WoodName - { - get; - } - + string WoodName{ get;} int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index 75ee4da..51d04d5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -1,15 +1,20 @@ -using CarpentryWorkshopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using CarpentryWorkshopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + namespace CarpentryWorkshopDatabaseImplement { public class CarpentryWorkshopDatabase : DbContext { - protected override void OnConfiguring(DbContextOptionsBuilder - optionsBuilder) + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=KOMPUTER\SQLEXPRESS;Initial Catalog=CarpentryWorkshopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=CarpentryWorkshopDatabaseFull;Username=postgres;Password=postgres"); } base.OnConfiguring(optionsBuilder); } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj index d9f920a..39785eb 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj @@ -13,6 +13,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs index f6d7a2d..3bfc675 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -3,41 +3,65 @@ using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopContracts.StoragesContracts; using CarpentryWorkshopContracts.ViewModels; using CarpentryWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace CarpentryWorkshopDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { - public List GetFullList() + public OrderViewModel? Delete(OrderBindingModel model) { using var context = new CarpentryWorkshopDatabase(); - return context.Orders - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(OrderSearchModel model) - { - if (model.Id == null) + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) { - return new(); + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); } - using var context = new CarpentryWorkshopDatabase(); - return context.Orders - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + + return null; } + public OrderViewModel? GetElement(OrderSearchModel model) { - if (model.Id == null && !model.Id.HasValue) + if (!model.Id.HasValue) { return null; } + using var context = new CarpentryWorkshopDatabase(); - return context.Orders - .FirstOrDefault(x => (model.Id != null && x.Id == model.Id) || (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new CarpentryWorkshopDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -45,37 +69,38 @@ namespace CarpentryWorkshopDatabaseImplement.Implements { return null; } + using var context = new CarpentryWorkshopDatabase(); + context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new CarpentryWorkshopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Orders.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return GetViewModel(newOrder); } - - // public OrderViewModel? Update(OrderBindingModel model) { using var context = new CarpentryWorkshopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { return null; } + order.Update(model); context.SaveChanges(); - return order.GetViewModel; + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new CarpentryWorkshopDatabase(); + var element = context.Woods.FirstOrDefault(x => x.Id == order.WoodId); + viewModel.WoodName = element.WoodName; + return viewModel; } } -} \ No newline at end of file +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs similarity index 74% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs index c31a556..055a999 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs @@ -3,16 +3,16 @@ using System; using CarpentryWorkshopDatabaseImplement; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace CarpentryWorkshopDatabaseImplement.Migrations { [DbContext(typeof(CarpentryWorkshopDatabase))] - [Migration("20240318160111_InitialCreate")] + [Migration("20240319095810_InitialCreate")] partial class InitialCreate { /// @@ -21,24 +21,24 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "8.0.3") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Component", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.Property("Cost") - .HasColumnType("float"); + .HasColumnType("double precision"); b.HasKey("Id"); @@ -49,31 +49,31 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("DateCreate") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("DateImplement") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("Status") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Sum") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -86,16 +86,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Price") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -106,18 +106,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.HasKey("Id"); diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs similarity index 64% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs index 4ccc59c..6d91226 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs @@ -1,5 +1,6 @@ using System; using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable @@ -15,10 +16,10 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations 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) + 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 => { @@ -29,10 +30,10 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations name: "Woods", columns: table => new { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - WoodName = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "float", nullable: false) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) }, constraints: table => { @@ -43,15 +44,15 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations name: "Orders", columns: table => new { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - WoodId = table.Column(type: "int", nullable: false), - WoodName = table.Column(type: "nvarchar(max)", nullable: false), - 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) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodId = table.Column(type: "integer", nullable: false), + WoodName = table.Column(type: "text", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { @@ -68,11 +69,11 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations name: "WoodComponents", columns: table => new { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - WoodId = table.Column(type: "int", nullable: false), - ComponentId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) }, constraints: table => { diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs index 2d1d4e2..7375e3b 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs @@ -3,8 +3,8 @@ using System; using CarpentryWorkshopDatabaseImplement; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable @@ -18,24 +18,24 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "8.0.3") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Component", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.Property("Cost") - .HasColumnType("float"); + .HasColumnType("double precision"); b.HasKey("Id"); @@ -46,31 +46,31 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("DateCreate") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("DateImplement") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("Status") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Sum") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -83,16 +83,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Price") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -103,18 +103,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.HasKey("Id"); diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs index dd7746f..84bf173 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs @@ -47,13 +47,9 @@ namespace CarpentryWorkshopDatabaseImplement.Models { return; } - Id = model.Id; - WoodId = model.WoodId; - WoodName = model.WoodName; - Count = model.Count; - Sum = model.Sum; + Status = model.Status; - DateCreate = model.DateCreate; + DateImplement = model.DateImplement; }