From 9c2288c7665de01faf698afb37eae54d041895dc Mon Sep 17 00:00:00 2001
From: ElEgEv <112943269+ElEgEv@users.noreply.github.com>
Date: Mon, 23 Oct 2023 16:47:58 +0400
Subject: [PATCH] Congratulates! DB is created :)
---
.../BindingModel/LabWorkBindingModel.cs | 2 +-
.../Contracts/ViewModel/LabWorkViewModel.cs | 2 +-
.../DataModels/Models/ILabWork.cs | 2 +-
.../DatabaseImplement.csproj | 4 -
.../Implements/DisciplineStorage.cs | 92 ++++++++++++++++
.../Implements/LabWorkStorage.cs | 102 ++++++++++++++++++
.../20231023124245_InitialCreate.Designer.cs | 75 +++++++++++++
.../20231023124245_InitialCreate.cs | 53 +++++++++
.../Migrations/ElegevContextModelSnapshot.cs | 72 +++++++++++++
.../DatabaseImplement/Models/LabWork.cs | 2 +-
.../VisualComponentsForm/Program.cs | 23 +++-
.../VisualComponentsForm.csproj | 7 ++
12 files changed, 427 insertions(+), 9 deletions(-)
create mode 100644 VisualComponentsForm/DatabaseImplement/Implements/DisciplineStorage.cs
create mode 100644 VisualComponentsForm/DatabaseImplement/Implements/LabWorkStorage.cs
create mode 100644 VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.Designer.cs
create mode 100644 VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.cs
create mode 100644 VisualComponentsForm/DatabaseImplement/Migrations/ElegevContextModelSnapshot.cs
diff --git a/VisualComponentsForm/Contracts/BindingModel/LabWorkBindingModel.cs b/VisualComponentsForm/Contracts/BindingModel/LabWorkBindingModel.cs
index bb4358d..c92dcc4 100644
--- a/VisualComponentsForm/Contracts/BindingModel/LabWorkBindingModel.cs
+++ b/VisualComponentsForm/Contracts/BindingModel/LabWorkBindingModel.cs
@@ -17,7 +17,7 @@ namespace Contracts.BindingModel
public string Discipline { get; set; }
- public string[] Questions { get; set; }
+ public string Questions { get; set; }
public LabWorkBindingModel() { }
diff --git a/VisualComponentsForm/Contracts/ViewModel/LabWorkViewModel.cs b/VisualComponentsForm/Contracts/ViewModel/LabWorkViewModel.cs
index a9a615e..f949112 100644
--- a/VisualComponentsForm/Contracts/ViewModel/LabWorkViewModel.cs
+++ b/VisualComponentsForm/Contracts/ViewModel/LabWorkViewModel.cs
@@ -17,7 +17,7 @@ namespace Contracts.ViewModel
public string Discipline { get; set; }
- public string[] Questions { get; set; }
+ public string Questions { get; set; }
public LabWorkViewModel() { }
diff --git a/VisualComponentsForm/DataModels/Models/ILabWork.cs b/VisualComponentsForm/DataModels/Models/ILabWork.cs
index a1a3b7a..eb61a89 100644
--- a/VisualComponentsForm/DataModels/Models/ILabWork.cs
+++ b/VisualComponentsForm/DataModels/Models/ILabWork.cs
@@ -16,6 +16,6 @@ namespace DataModels.Models
string Discipline { get; }
//Вопросы по лабораторной
- string[] Questions { get; }
+ string Questions { get; }
}
}
diff --git a/VisualComponentsForm/DatabaseImplement/DatabaseImplement.csproj b/VisualComponentsForm/DatabaseImplement/DatabaseImplement.csproj
index 7b209b9..417bed2 100644
--- a/VisualComponentsForm/DatabaseImplement/DatabaseImplement.csproj
+++ b/VisualComponentsForm/DatabaseImplement/DatabaseImplement.csproj
@@ -21,8 +21,4 @@
-
-
-
-
diff --git a/VisualComponentsForm/DatabaseImplement/Implements/DisciplineStorage.cs b/VisualComponentsForm/DatabaseImplement/Implements/DisciplineStorage.cs
new file mode 100644
index 0000000..493bc48
--- /dev/null
+++ b/VisualComponentsForm/DatabaseImplement/Implements/DisciplineStorage.cs
@@ -0,0 +1,92 @@
+using Contracts.BindingModel;
+using Contracts.SearchModel;
+using Contracts.StorageContracts;
+using Contracts.ViewModel;
+using DatabaseImplement.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DatabaseImplement.Implements
+{
+ public class DisciplineStorage : IDisciplineStorage
+ {
+ public List GetFullList()
+ {
+ using var context = new ElegevContext();
+
+ return context.Disciplines
+ .Select(x => x.GetViewModel)
+ .ToList();
+ }
+
+ public DisciplineViewModel? GetElement(DisciplineSearchModel model)
+ {
+ if (!model.Id.HasValue)
+ {
+ return null;
+ }
+
+ using var context = new ElegevContext();
+
+ return context.Disciplines
+ .FirstOrDefault(x =>(model.Id.HasValue && x.Id == model.Id))
+ ?.GetViewModel;
+ }
+
+ public DisciplineViewModel? Insert(DisciplineBindingModel model)
+ {
+ var newDiscipline = Discipline.Create(model);
+
+ if (newDiscipline == null)
+ {
+ return null;
+ }
+
+ using var context = new ElegevContext();
+
+ context.Disciplines.Add(newDiscipline);
+ context.SaveChanges();
+
+ return newDiscipline.GetViewModel;
+ }
+
+ public DisciplineViewModel? Update(DisciplineBindingModel model)
+ {
+ using var context = new ElegevContext();
+
+ var discipline = context.Disciplines
+ .FirstOrDefault(x => x.Id == model.Id);
+
+ if (discipline == null)
+ {
+ return null;
+ }
+
+ discipline.Update(model);
+ context.SaveChanges();
+
+ return discipline.GetViewModel;
+ }
+
+ public DisciplineViewModel? Delete(DisciplineBindingModel model)
+ {
+ using var context = new ElegevContext();
+
+ var element = context.Disciplines
+ .FirstOrDefault(rec => rec.Id == model.Id);
+
+ if (element != null)
+ {
+ context.Disciplines.Remove(element);
+ context.SaveChanges();
+
+ return element.GetViewModel;
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/VisualComponentsForm/DatabaseImplement/Implements/LabWorkStorage.cs b/VisualComponentsForm/DatabaseImplement/Implements/LabWorkStorage.cs
new file mode 100644
index 0000000..77038b3
--- /dev/null
+++ b/VisualComponentsForm/DatabaseImplement/Implements/LabWorkStorage.cs
@@ -0,0 +1,102 @@
+using Contracts.BindingModel;
+using Contracts.SearchModel;
+using Contracts.StorageContracts;
+using Contracts.ViewModel;
+using DatabaseImplement.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DatabaseImplement.Implements
+{
+ public class LabWorkStorage : ILabWorkStorage
+ {
+ public List GetFullList()
+ {
+ using var context = new ElegevContext();
+
+ return context.LabWorks
+ .Select(x => x.GetViewModel)
+ .ToList();
+ }
+
+ public List GetFilteredList(LabWorkSearchModel model)
+ {
+ using var context = new ElegevContext();
+
+ return context.LabWorks
+ .Where(x => x.Theme == model.Theme)
+ .Select(x => x.GetViewModel)
+ .ToList();
+ }
+
+ public LabWorkViewModel? GetElement(LabWorkSearchModel model)
+ {
+ if (!model.Id.HasValue)
+ {
+ return null;
+ }
+
+ using var context = new ElegevContext();
+
+ return context.LabWorks
+ .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
+ ?.GetViewModel;
+ }
+
+ public LabWorkViewModel? Insert(LabWorkBindingModel model)
+ {
+ var newLabWork = LabWork.Create(model);
+
+ if (newLabWork == null)
+ {
+ return null;
+ }
+
+ using var context = new ElegevContext();
+
+ context.LabWorks.Add(newLabWork);
+ context.SaveChanges();
+
+ return newLabWork.GetViewModel;
+ }
+
+ public LabWorkViewModel? Update(LabWorkBindingModel model)
+ {
+ using var context = new ElegevContext();
+
+ var labWork = context.LabWorks
+ .FirstOrDefault(x => x.Id == model.Id);
+
+ if (labWork == null)
+ {
+ return null;
+ }
+
+ labWork.Update(model);
+ context.SaveChanges();
+
+ return labWork.GetViewModel;
+ }
+
+ public LabWorkViewModel? Delete(LabWorkBindingModel model)
+ {
+ using var context = new ElegevContext();
+
+ var element = context.LabWorks
+ .FirstOrDefault(rec => rec.Id == model.Id);
+
+ if (element != null)
+ {
+ context.LabWorks.Remove(element);
+ context.SaveChanges();
+
+ return element.GetViewModel;
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.Designer.cs b/VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.Designer.cs
new file mode 100644
index 0000000..57b86dc
--- /dev/null
+++ b/VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.Designer.cs
@@ -0,0 +1,75 @@
+//
+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(ElegevContext))]
+ [Migration("20231023124245_InitialCreate")]
+ partial class InitialCreate
+ {
+ ///
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "7.0.12")
+ .HasAnnotation("Relational:MaxIdentifierLength", 128);
+
+ SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+
+ modelBuilder.Entity("DatabaseImplement.Models.Discipline", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("Name")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Disciplines");
+ });
+
+ modelBuilder.Entity("DatabaseImplement.Models.LabWork", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("Discipline")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("FCs")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Questions")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Theme")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("LabWorks");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.cs b/VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.cs
new file mode 100644
index 0000000..b64c388
--- /dev/null
+++ b/VisualComponentsForm/DatabaseImplement/Migrations/20231023124245_InitialCreate.cs
@@ -0,0 +1,53 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace DatabaseImplement.Migrations
+{
+ ///
+ public partial class InitialCreate : Migration
+ {
+ ///
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "Disciplines",
+ columns: table => new
+ {
+ Id = table.Column(type: "int", nullable: false)
+ .Annotation("SqlServer:Identity", "1, 1"),
+ Name = table.Column(type: "nvarchar(max)", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Disciplines", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "LabWorks",
+ columns: table => new
+ {
+ Id = table.Column(type: "int", nullable: false)
+ .Annotation("SqlServer:Identity", "1, 1"),
+ Theme = table.Column(type: "nvarchar(max)", nullable: false),
+ FCs = table.Column(type: "nvarchar(max)", nullable: false),
+ Discipline = table.Column(type: "nvarchar(max)", nullable: false),
+ Questions = table.Column(type: "nvarchar(max)", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_LabWorks", x => x.Id);
+ });
+ }
+
+ ///
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "Disciplines");
+
+ migrationBuilder.DropTable(
+ name: "LabWorks");
+ }
+ }
+}
diff --git a/VisualComponentsForm/DatabaseImplement/Migrations/ElegevContextModelSnapshot.cs b/VisualComponentsForm/DatabaseImplement/Migrations/ElegevContextModelSnapshot.cs
new file mode 100644
index 0000000..b28391d
--- /dev/null
+++ b/VisualComponentsForm/DatabaseImplement/Migrations/ElegevContextModelSnapshot.cs
@@ -0,0 +1,72 @@
+//
+using DatabaseImplement;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace DatabaseImplement.Migrations
+{
+ [DbContext(typeof(ElegevContext))]
+ partial class ElegevContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "7.0.12")
+ .HasAnnotation("Relational:MaxIdentifierLength", 128);
+
+ SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+
+ modelBuilder.Entity("DatabaseImplement.Models.Discipline", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("Name")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Disciplines");
+ });
+
+ modelBuilder.Entity("DatabaseImplement.Models.LabWork", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("Discipline")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("FCs")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Questions")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Theme")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("LabWorks");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/VisualComponentsForm/DatabaseImplement/Models/LabWork.cs b/VisualComponentsForm/DatabaseImplement/Models/LabWork.cs
index 3d383d7..2d2dcb2 100644
--- a/VisualComponentsForm/DatabaseImplement/Models/LabWork.cs
+++ b/VisualComponentsForm/DatabaseImplement/Models/LabWork.cs
@@ -26,7 +26,7 @@ namespace DatabaseImplement.Models
public string Discipline { get; set; }
[Required]
- public string[] Questions { get; set; }
+ public string Questions { get; set; }
public static LabWork? Create(LabWorkBindingModel model)
{
diff --git a/VisualComponentsForm/VisualComponentsForm/Program.cs b/VisualComponentsForm/VisualComponentsForm/Program.cs
index 865a38b..d482b61 100644
--- a/VisualComponentsForm/VisualComponentsForm/Program.cs
+++ b/VisualComponentsForm/VisualComponentsForm/Program.cs
@@ -1,7 +1,15 @@
+using Contracts.StorageContracts;
+using DatabaseImplement.Implements;
+using Microsoft.Extensions.DependencyInjection;
+
namespace VisualComponentsForm
{
internal static class Program
{
+ private static ServiceProvider? _serviceProvider;
+
+ public static ServiceProvider? ServiceProvider => _serviceProvider;
+
///
/// The main entry point for the application.
///
@@ -11,7 +19,20 @@ namespace VisualComponentsForm
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
- Application.Run(new FormWord());
+
+ 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/VisualComponentsForm/VisualComponentsForm/VisualComponentsForm.csproj b/VisualComponentsForm/VisualComponentsForm/VisualComponentsForm.csproj
index c13f9b5..a19beff 100644
--- a/VisualComponentsForm/VisualComponentsForm/VisualComponentsForm.csproj
+++ b/VisualComponentsForm/VisualComponentsForm/VisualComponentsForm.csproj
@@ -10,6 +10,10 @@
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
@@ -18,6 +22,9 @@
+
+
+
\ No newline at end of file