From cef265fdebb6ebe965d63b7bde1f84f4fa134b92 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Tue, 30 Apr 2024 22:27:52 +0400 Subject: [PATCH] AddDate --- .../BusinessLogic/DetailLogic.cs | 4 + .../BindingModels/DetailBindingModel.cs | 1 + .../SearchModels/DetailSearchModel.cs | 3 + .../Contracts/ViewModels/DetailViewModel.cs | 2 + Course/DataModels/Models/IDetailModel.cs | 1 + .../Implements/DetailStorage.cs | 7 +- .../20240430182642_time.Designer.cs | 564 ++++++++++++++++++ .../Migrations/20240430182642_time.cs | 27 + .../FactoryGoWorkDatabaseModelSnapshot.cs | 9 +- Course/DatabaseImplement/Models/Detail.cs | 11 +- Course/DatabaseImplement/Models/Workshop.cs | 1 - 11 files changed, 621 insertions(+), 9 deletions(-) create mode 100644 Course/DatabaseImplement/Migrations/20240430182642_time.Designer.cs create mode 100644 Course/DatabaseImplement/Migrations/20240430182642_time.cs diff --git a/Course/BusinessLogic/BusinessLogic/DetailLogic.cs b/Course/BusinessLogic/BusinessLogic/DetailLogic.cs index 43f11f0..dd24474 100644 --- a/Course/BusinessLogic/BusinessLogic/DetailLogic.cs +++ b/Course/BusinessLogic/BusinessLogic/DetailLogic.cs @@ -65,6 +65,10 @@ namespace BusinessLogic.BusinessLogic { throw new ArgumentNullException("Цена детали должна быть больше 0", nameof(model.Cost)); } + if (model.DateCreate > DateTime.Now) + { + throw new ArgumentNullException("Деталь не может быть создана в будущем", nameof(model.DateCreate)); + } _logger.LogInformation("Detail. DetailName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); var elem = _detailStorage.GetElement(new DetailSearchModel { diff --git a/Course/Contracts/BindingModels/DetailBindingModel.cs b/Course/Contracts/BindingModels/DetailBindingModel.cs index 2bd13f7..70195c8 100644 --- a/Course/Contracts/BindingModels/DetailBindingModel.cs +++ b/Course/Contracts/BindingModels/DetailBindingModel.cs @@ -8,5 +8,6 @@ namespace Contracts.BindingModels public int UserId { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } + public DateTime DateCreate { get; set; } } } diff --git a/Course/Contracts/SearchModels/DetailSearchModel.cs b/Course/Contracts/SearchModels/DetailSearchModel.cs index 47ff68a..b360719 100644 --- a/Course/Contracts/SearchModels/DetailSearchModel.cs +++ b/Course/Contracts/SearchModels/DetailSearchModel.cs @@ -11,5 +11,8 @@ namespace Contracts.SearchModels public int? Id { get; set; } public string? Name { get; set; } public int? UserId { get; set; } + public DateTime? DateFrom { get; set; } + + public DateTime? DateTo { get; set; } } } diff --git a/Course/Contracts/ViewModels/DetailViewModel.cs b/Course/Contracts/ViewModels/DetailViewModel.cs index 1b5810f..955a9c7 100644 --- a/Course/Contracts/ViewModels/DetailViewModel.cs +++ b/Course/Contracts/ViewModels/DetailViewModel.cs @@ -16,6 +16,8 @@ namespace Contracts.ViewModels [DisplayName("Цена детали")] public double Cost { get; set; } public int UserId { get; set; } + [DisplayName("Дата создания")] + public DateTime DateCreate { get; set; } } } diff --git a/Course/DataModels/Models/IDetailModel.cs b/Course/DataModels/Models/IDetailModel.cs index afe00d2..e41429e 100644 --- a/Course/DataModels/Models/IDetailModel.cs +++ b/Course/DataModels/Models/IDetailModel.cs @@ -4,5 +4,6 @@ { string Name { get; } double Cost { get; } + DateTime DateCreate { get; } } } diff --git a/Course/DatabaseImplement/Implements/DetailStorage.cs b/Course/DatabaseImplement/Implements/DetailStorage.cs index b93ec5b..2260591 100644 --- a/Course/DatabaseImplement/Implements/DetailStorage.cs +++ b/Course/DatabaseImplement/Implements/DetailStorage.cs @@ -27,12 +27,15 @@ namespace DatabaseImplement.Implements public List GetFilteredList(DetailSearchModel model) { - if (!model.UserId.HasValue) + if (!model.UserId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue) { return new(); } using var context = new FactoryGoWorkDatabase(); - return context.Details.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + if (model.DateFrom.HasValue) + return context.Details.Where(x => x.UserId == model.Id).Where(x => x.DateCreate < model.DateTo && x.DateCreate > model.DateFrom).Select(x => x.GetViewModel).ToList(); + else + return context.Details.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); } diff --git a/Course/DatabaseImplement/Migrations/20240430182642_time.Designer.cs b/Course/DatabaseImplement/Migrations/20240430182642_time.Designer.cs new file mode 100644 index 0000000..0adcaa4 --- /dev/null +++ b/Course/DatabaseImplement/Migrations/20240430182642_time.Designer.cs @@ -0,0 +1,564 @@ +// +using System; +using DatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + [DbContext(typeof(FactoryGoWorkDatabase))] + [Migration("20240430182642_time")] + partial class time + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.29") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("DatabaseImplement.Models.Detail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Details"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.DetailProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DetailId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DetailId"); + + b.HasIndex("ProductId"); + + b.ToTable("DetailProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.DetailProduction", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("DetailId") + .HasColumnType("int"); + + b.Property("ProductionId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DetailId"); + + b.HasIndex("ProductionId"); + + b.ToTable("DetailProductions"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Guarantor", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Login") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Guarantors"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Login") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Machine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Country") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Machines"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("MachineId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("MachineId"); + + b.HasIndex("UserId"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Production", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Productions"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Worker", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Birthday") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Salary") + .HasColumnType("float"); + + b.Property("Specialization") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Workers"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.WorkerMachine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("MachineId") + .HasColumnType("int"); + + b.Property("WorkerId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("WorkerId"); + + b.ToTable("WorkerMachines"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.WorkerWorkshop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("WorkerId") + .HasColumnType("int"); + + b.Property("WorkshopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("WorkerId"); + + b.HasIndex("WorkshopId"); + + b.ToTable("WorkerWorkshops"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Workshop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Director") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProductionId") + .HasColumnType("int"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductionId"); + + b.HasIndex("UserId"); + + b.ToTable("Workshops"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Detail", b => + { + b.HasOne("DatabaseImplement.Models.Implementer", "User") + .WithMany("Details") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.DetailProduct", b => + { + b.HasOne("DatabaseImplement.Models.Detail", "Detail") + .WithMany("DetailProducts") + .HasForeignKey("DetailId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany("Details") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Detail"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.DetailProduction", b => + { + b.HasOne("DatabaseImplement.Models.Detail", "Detail") + .WithMany("DetailProductions") + .HasForeignKey("DetailId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Production", "Production") + .WithMany("Details") + .HasForeignKey("ProductionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Detail"); + + b.Navigation("Production"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Machine", b => + { + b.HasOne("DatabaseImplement.Models.Guarantor", "Guarantor") + .WithMany("Machines") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Guarantor"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.HasOne("DatabaseImplement.Models.Machine", "Machine") + .WithMany() + .HasForeignKey("MachineId"); + + b.HasOne("DatabaseImplement.Models.Implementer", "User") + .WithMany("Products") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Machine"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Production", b => + { + b.HasOne("DatabaseImplement.Models.Implementer", "User") + .WithMany("Productions") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Worker", b => + { + b.HasOne("DatabaseImplement.Models.Guarantor", "Guarantor") + .WithMany("Workers") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Guarantor"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.WorkerMachine", b => + { + b.HasOne("DatabaseImplement.Models.Machine", "Machine") + .WithMany("Workers") + .HasForeignKey("WorkerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Worker", "Worker") + .WithMany("WorkerMachines") + .HasForeignKey("WorkerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Machine"); + + b.Navigation("Worker"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.WorkerWorkshop", b => + { + b.HasOne("DatabaseImplement.Models.Worker", "Worker") + .WithMany("WorkerWorkshops") + .HasForeignKey("WorkerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Workshop", "Workshop") + .WithMany("Workers") + .HasForeignKey("WorkshopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Worker"); + + b.Navigation("Workshop"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Workshop", b => + { + b.HasOne("DatabaseImplement.Models.Production", "Production") + .WithMany("Workshops") + .HasForeignKey("ProductionId"); + + b.HasOne("DatabaseImplement.Models.Guarantor", "User") + .WithMany("Workshops") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Production"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Detail", b => + { + b.Navigation("DetailProductions"); + + b.Navigation("DetailProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Guarantor", b => + { + b.Navigation("Machines"); + + b.Navigation("Workers"); + + b.Navigation("Workshops"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Implementer", b => + { + b.Navigation("Details"); + + b.Navigation("Productions"); + + b.Navigation("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Machine", b => + { + b.Navigation("Workers"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Navigation("Details"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Production", b => + { + b.Navigation("Details"); + + b.Navigation("Workshops"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Worker", b => + { + b.Navigation("WorkerMachines"); + + b.Navigation("WorkerWorkshops"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Workshop", b => + { + b.Navigation("Workers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Course/DatabaseImplement/Migrations/20240430182642_time.cs b/Course/DatabaseImplement/Migrations/20240430182642_time.cs new file mode 100644 index 0000000..4818ef2 --- /dev/null +++ b/Course/DatabaseImplement/Migrations/20240430182642_time.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + public partial class time : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "DateCreate", + table: "Details", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified)); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "DateCreate", + table: "Details"); + } + } +} diff --git a/Course/DatabaseImplement/Migrations/FactoryGoWorkDatabaseModelSnapshot.cs b/Course/DatabaseImplement/Migrations/FactoryGoWorkDatabaseModelSnapshot.cs index f409ead..cd7adcf 100644 --- a/Course/DatabaseImplement/Migrations/FactoryGoWorkDatabaseModelSnapshot.cs +++ b/Course/DatabaseImplement/Migrations/FactoryGoWorkDatabaseModelSnapshot.cs @@ -33,6 +33,9 @@ namespace DatabaseImplement.Migrations b.Property("Cost") .HasColumnType("float"); + b.Property("DateCreate") + .HasColumnType("datetime2"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -489,15 +492,15 @@ namespace DatabaseImplement.Migrations .WithMany("Workshops") .HasForeignKey("ProductionId"); - b.HasOne("DatabaseImplement.Models.Guarantor", "Guarantor") + b.HasOne("DatabaseImplement.Models.Guarantor", "User") .WithMany("Workshops") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Guarantor"); - b.Navigation("Production"); + + b.Navigation("User"); }); modelBuilder.Entity("DatabaseImplement.Models.Detail", b => diff --git a/Course/DatabaseImplement/Models/Detail.cs b/Course/DatabaseImplement/Models/Detail.cs index 45988de..c3a8f90 100644 --- a/Course/DatabaseImplement/Models/Detail.cs +++ b/Course/DatabaseImplement/Models/Detail.cs @@ -14,6 +14,8 @@ namespace DatabaseImplement.Models [Required] public double Cost { get; set; } [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + [Required] public int UserId { get; set; } [ForeignKey("DetailId")] public virtual List DetailProducts { get; set; } = new(); @@ -31,7 +33,8 @@ namespace DatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - UserId = model.UserId + UserId = model.UserId, + DateCreate = model.DateCreate }; } public static Detail Create(DetailViewModel model) @@ -41,7 +44,8 @@ namespace DatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - UserId = model.UserId + UserId = model.UserId, + DateCreate = model.DateCreate }; } public void Update(DetailBindingModel model) @@ -56,7 +60,8 @@ namespace DatabaseImplement.Models Id = Id, Name = Name, Cost = Cost, - UserId = UserId + UserId = UserId, + DateCreate = DateCreate }; } } diff --git a/Course/DatabaseImplement/Models/Workshop.cs b/Course/DatabaseImplement/Models/Workshop.cs index ef24b57..bd8ec1f 100644 --- a/Course/DatabaseImplement/Models/Workshop.cs +++ b/Course/DatabaseImplement/Models/Workshop.cs @@ -35,7 +35,6 @@ namespace DatabaseImplement.Models } [ForeignKey("WorkshopId")] public List Workers { get; set; } = new(); - public virtual Guarantor Guarantor { get; set; } public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model) { return new Workshop()