From d1b46637e28b1a7a1459ea6440aeeff0573e6119 Mon Sep 17 00:00:00 2001 From: 1eG0ist Date: Sat, 16 Nov 2024 15:34:29 +0300 Subject: [PATCH] add dao --- data/DAO/Group.cs | 2 + data/DAO/Presence.cs | 12 +++ data/DAO/User.cs | 2 +- .../20241115163726_InitialCreate.Designer.cs | 83 +++++++++++++++++++ .../20241115163726_InitialCreate.cs | 63 ++++++++++++++ .../RemoteDatabseContextModelSnapshot.cs | 80 ++++++++++++++++++ data/RemoteDatabseContext.cs | 32 +++++++ data/data.csproj | 13 ++- 8 files changed, 284 insertions(+), 3 deletions(-) create mode 100644 data/DAO/Presence.cs create mode 100644 data/Migrations/20241115163726_InitialCreate.Designer.cs create mode 100644 data/Migrations/20241115163726_InitialCreate.cs create mode 100644 data/Migrations/RemoteDatabseContextModelSnapshot.cs create mode 100644 data/RemoteDatabseContext.cs diff --git a/data/DAO/Group.cs b/data/DAO/Group.cs index 97a3ae4..b7e3dab 100644 --- a/data/DAO/Group.cs +++ b/data/DAO/Group.cs @@ -10,5 +10,7 @@ namespace data.DAO { public int Id { get; set; } public String Name { get; set; } + public virtual IEnumerable Users { get; set; } + } } diff --git a/data/DAO/Presence.cs b/data/DAO/Presence.cs new file mode 100644 index 0000000..2fb92e5 --- /dev/null +++ b/data/DAO/Presence.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace data.DAO +{ + internal class Presence + { + } +} diff --git a/data/DAO/User.cs b/data/DAO/User.cs index db965ca..cf41fb9 100644 --- a/data/DAO/User.cs +++ b/data/DAO/User.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace data.DAO { - internal class User + internal class UserDAO { public Guid Guid { get; set; } public string Name { get; set; } diff --git a/data/Migrations/20241115163726_InitialCreate.Designer.cs b/data/Migrations/20241115163726_InitialCreate.Designer.cs new file mode 100644 index 0000000..a5fc96b --- /dev/null +++ b/data/Migrations/20241115163726_InitialCreate.Designer.cs @@ -0,0 +1,83 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using data; + +#nullable disable + +namespace data.Migrations +{ + [DbContext(typeof(RemoteDatabseContext))] + [Migration("20241115163726_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("data.DAO.GroupDAO", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("groups"); + }); + + modelBuilder.Entity("data.DAO.UserDAO", b => + { + b.Property("Guid") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("GroupId") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Guid"); + + b.HasIndex("GroupId"); + + b.ToTable("users"); + }); + + modelBuilder.Entity("data.DAO.UserDAO", b => + { + b.HasOne("data.DAO.GroupDAO", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("data.DAO.GroupDAO", b => + { + b.Navigation("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/data/Migrations/20241115163726_InitialCreate.cs b/data/Migrations/20241115163726_InitialCreate.cs new file mode 100644 index 0000000..d14854e --- /dev/null +++ b/data/Migrations/20241115163726_InitialCreate.cs @@ -0,0 +1,63 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace data.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "groups", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_groups", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "users", + columns: table => new + { + Guid = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: false), + GroupId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_users", x => x.Guid); + table.ForeignKey( + name: "FK_users_groups_GroupId", + column: x => x.GroupId, + principalTable: "groups", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_users_GroupId", + table: "users", + column: "GroupId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "users"); + + migrationBuilder.DropTable( + name: "groups"); + } + } +} diff --git a/data/Migrations/RemoteDatabseContextModelSnapshot.cs b/data/Migrations/RemoteDatabseContextModelSnapshot.cs new file mode 100644 index 0000000..948cdc9 --- /dev/null +++ b/data/Migrations/RemoteDatabseContextModelSnapshot.cs @@ -0,0 +1,80 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using data; + +#nullable disable + +namespace data.Migrations +{ + [DbContext(typeof(RemoteDatabseContext))] + partial class RemoteDatabseContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("data.DAO.GroupDAO", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("groups"); + }); + + modelBuilder.Entity("data.DAO.UserDAO", b => + { + b.Property("Guid") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("GroupId") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Guid"); + + b.HasIndex("GroupId"); + + b.ToTable("users"); + }); + + modelBuilder.Entity("data.DAO.UserDAO", b => + { + b.HasOne("data.DAO.GroupDAO", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("data.DAO.GroupDAO", b => + { + b.Navigation("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/data/RemoteDatabseContext.cs b/data/RemoteDatabseContext.cs new file mode 100644 index 0000000..e338471 --- /dev/null +++ b/data/RemoteDatabseContext.cs @@ -0,0 +1,32 @@ +using data.DAO; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace data +{ + internal class RemoteDatabseContext: DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseNpgsql("Host=localhost;port=5432;Password=admin;Username=postgres;Database=semesterWork"); + } + + DbSet groups { get; set; } + + DbSet users { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(it => it.Guid); + modelBuilder.Entity().HasKey(it => it.Id); + + modelBuilder.Entity().Property(it => it.Guid).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(it => it.Id).ValueGeneratedOnAdd(); + + } + } +} diff --git a/data/data.csproj b/data/data.csproj index 132c02c..b3a10f4 100644 --- a/data/data.csproj +++ b/data/data.csproj @@ -1,9 +1,18 @@ - + - net6.0 + net8.0 enable enable + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + +