Models Update

This commit is contained in:
batgo6o
2021-03-17 22:02:52 +02:00
parent 3b673f153e
commit 9f7dcd71d5
11 changed files with 201 additions and 59 deletions

View File

@@ -49,8 +49,8 @@ namespace Business.Business.UserManagment
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
var AdminRole = context.Permission.Where(p => p.Name == "Admin").FirstOrDefault(); var AdminPermission = context.Permission.FirstOrDefault(p => p.Name == "Admin");
userctl.CreateRole(this.RoleName , new List<Permission> { AdminRole }); userctl.CreateRole(this.RoleName , AdminPermission);
} }
} }
public void CreateFirstUser() public void CreateFirstUser()

View File

@@ -1,7 +1,9 @@
using Models; using Data.Models;
using Models;
using Models.Models; using Models.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -10,12 +12,18 @@ namespace Business.Business.UserManagment
public class UserController public class UserController
{ {
private LuminousContext context; private LuminousContext context;
public void CreateRole(string RoleName, ICollection<Permission> Permissions) public void CreateRole(string RoleName, Permission Permission)
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
var firstRole = new Role(RoleName, Permissions); var role = new Role(RoleName);
context.Role.Add(firstRole); var relationship = new RolePermission();
relationship.Roles = role;
relationship.Permission = Permission;
role.Permissions.Add(relationship);
Permission.Role.Add(relationship);
context.RolePermission.Add(relationship);
context.Role.Add(role);
context.SaveChanges(); context.SaveChanges();
} }
} }
@@ -23,8 +31,8 @@ namespace Business.Business.UserManagment
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
var firstUser = new User(Username, Password, Role); var user = new User(Username, Password, Role);
context.User.Add(firstUser); context.User.Add(user);
context.SaveChanges(); context.SaveChanges();
} }
} }

View File

@@ -0,0 +1,36 @@
using Models;
using Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Business.Business.UserManagment
{
public class UserValidator
{
private LuminousContext context;
public bool CheckIfUserIsCreated()
{
using (context = new LuminousContext())
{
if (context.User.ToList().Any())
{
return true;
}
return false;
}
}
public bool CheckPassword(string Password)
{
using (context = new LuminousContext())
{
if (context.User.ToList().Exists(user => user.Password == Password))
{
return true;
}
return false;
}
}
}
}

View File

@@ -1,5 +1,8 @@
using System; using Business.Business.UserManagment;
using Models;
using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Display namespace Display
{ {
@@ -7,10 +10,16 @@ namespace Display
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var val = new Business.Business.UserManagment.Validator(); var val = new UserValidator();
if () if (!val.CheckIfUserIsCreated())
{ {
var InitialCreation = new CreateInitialUser("Admin", "Admin", "pass123");
InitialCreation.CreateFirstRole();
InitialCreation.CreateFirstUser();
}
else
{
Console.WriteLine("Already created");
} }
} }
} }

View File

@@ -1,4 +1,5 @@
using Data; using Data;
using Data.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Models.Models; using Models.Models;
@@ -22,6 +23,7 @@ namespace Models
public DbSet<Product> Product { get; set; } public DbSet<Product> Product { get; set; }
public DbSet<Deal> Deal { get; set; } public DbSet<Deal> Deal { get; set; }
public DbSet<Stock> Stock { get; set; } public DbSet<Stock> Stock { get; set; }
public DbSet<RolePermission> RolePermission { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
@@ -44,6 +46,16 @@ namespace Models
modelBuilder.Entity<Product>() modelBuilder.Entity<Product>()
.HasIndex(product => new { product.Name }) .HasIndex(product => new { product.Name })
.IsUnique(true); .IsUnique(true);
modelBuilder.Entity<RolePermission>()
.HasKey(rp => new { rp.RoleId, rp.PermisionId });
modelBuilder.Entity<RolePermission>()
.HasOne(rp => rp.Roles)
.WithMany(rp => rp.Permissions)
.HasForeignKey(rp => rp.RoleId);
modelBuilder.Entity<RolePermission>()
.HasOne(rp => rp.Permission)
.WithMany(rp => rp.Role)
.HasForeignKey(rp => rp.PermisionId);
} }
} }
} }

View File

@@ -10,8 +10,8 @@ using Models;
namespace Data.Migrations namespace Data.Migrations
{ {
[DbContext(typeof(LuminousContext))] [DbContext(typeof(LuminousContext))]
[Migration("20210314081427_IntialMigration")] [Migration("20210317183331_InitialMigration")]
partial class IntialMigration partial class InitialMigration
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
{ {
@@ -21,6 +21,21 @@ namespace Data.Migrations
.HasAnnotation("Relational:MaxIdentifierLength", 128) .HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Data.Models.RolePermission", b =>
{
b.Property<int>("RoleId")
.HasColumnType("int");
b.Property<int>("PermisionId")
.HasColumnType("int");
b.HasKey("RoleId", "PermisionId");
b.HasIndex("PermisionId");
b.ToTable("RolePermission");
});
modelBuilder.Entity("Models.Models.Deal", b => modelBuilder.Entity("Models.Models.Deal", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@@ -55,16 +70,11 @@ namespace Data.Migrations
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(450)"); .HasColumnType("nvarchar(450)");
b.Property<int?>("RoleId")
.HasColumnType("int");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Name") b.HasIndex("Name")
.IsUnique(); .IsUnique();
b.HasIndex("RoleId");
b.ToTable("Permission"); b.ToTable("Permission");
}); });
@@ -173,6 +183,21 @@ namespace Data.Migrations
b.ToTable("User"); b.ToTable("User");
}); });
modelBuilder.Entity("Data.Models.RolePermission", b =>
{
b.HasOne("Models.Models.Permission", "Permission")
.WithMany("Role")
.HasForeignKey("PermisionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Models.Models.Role", "Roles")
.WithMany("Permissions")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Models.Models.Deal", b => modelBuilder.Entity("Models.Models.Deal", b =>
{ {
b.HasOne("Models.Models.User", "User") b.HasOne("Models.Models.User", "User")
@@ -182,13 +207,6 @@ namespace Data.Migrations
.IsRequired(); .IsRequired();
}); });
modelBuilder.Entity("Models.Models.Permission", b =>
{
b.HasOne("Models.Models.Role", null)
.WithMany("Permissions")
.HasForeignKey("RoleId");
});
modelBuilder.Entity("Models.Models.Product", b => modelBuilder.Entity("Models.Models.Product", b =>
{ {
b.HasOne("Models.Models.Deal", null) b.HasOne("Models.Models.Deal", null)

View File

@@ -3,10 +3,23 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Data.Migrations namespace Data.Migrations
{ {
public partial class IntialMigration : Migration public partial class InitialMigration : Migration
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.CreateTable(
name: "Permission",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Permission", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Role", name: "Role",
columns: table => new columns: table => new
@@ -21,23 +34,27 @@ namespace Data.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Permission", name: "RolePermission",
columns: table => new columns: table => new
{ {
Id = table.Column<int>(nullable: false) RoleId = table.Column<int>(nullable: false),
.Annotation("SqlServer:Identity", "1, 1"), PermisionId = table.Column<int>(nullable: false)
Name = table.Column<string>(nullable: false),
RoleId = table.Column<int>(nullable: true)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Permission", x => x.Id); table.PrimaryKey("PK_RolePermission", x => new { x.RoleId, x.PermisionId });
table.ForeignKey( table.ForeignKey(
name: "FK_Permission_Role_RoleId", name: "FK_RolePermission_Permission_PermisionId",
column: x => x.PermisionId,
principalTable: "Permission",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_RolePermission_Role_RoleId",
column: x => x.RoleId, column: x => x.RoleId,
principalTable: "Role", principalTable: "Role",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
@@ -141,11 +158,6 @@ namespace Data.Migrations
column: "Name", column: "Name",
unique: true); unique: true);
migrationBuilder.CreateIndex(
name: "IX_Permission_RoleId",
table: "Permission",
column: "RoleId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Product_DealId", name: "IX_Product_DealId",
table: "Product", table: "Product",
@@ -168,6 +180,11 @@ namespace Data.Migrations
column: "Name", column: "Name",
unique: true); unique: true);
migrationBuilder.CreateIndex(
name: "IX_RolePermission_PermisionId",
table: "RolePermission",
column: "PermisionId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Stock_UserId", name: "IX_Stock_UserId",
table: "Stock", table: "Stock",
@@ -188,10 +205,10 @@ namespace Data.Migrations
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Permission"); name: "Product");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Product"); name: "RolePermission");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Deal"); name: "Deal");
@@ -199,6 +216,9 @@ namespace Data.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Stock"); name: "Stock");
migrationBuilder.DropTable(
name: "Permission");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "User"); name: "User");

View File

@@ -19,6 +19,21 @@ namespace Data.Migrations
.HasAnnotation("Relational:MaxIdentifierLength", 128) .HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Data.Models.RolePermission", b =>
{
b.Property<int>("RoleId")
.HasColumnType("int");
b.Property<int>("PermisionId")
.HasColumnType("int");
b.HasKey("RoleId", "PermisionId");
b.HasIndex("PermisionId");
b.ToTable("RolePermission");
});
modelBuilder.Entity("Models.Models.Deal", b => modelBuilder.Entity("Models.Models.Deal", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@@ -53,16 +68,11 @@ namespace Data.Migrations
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(450)"); .HasColumnType("nvarchar(450)");
b.Property<int?>("RoleId")
.HasColumnType("int");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Name") b.HasIndex("Name")
.IsUnique(); .IsUnique();
b.HasIndex("RoleId");
b.ToTable("Permission"); b.ToTable("Permission");
}); });
@@ -171,6 +181,21 @@ namespace Data.Migrations
b.ToTable("User"); b.ToTable("User");
}); });
modelBuilder.Entity("Data.Models.RolePermission", b =>
{
b.HasOne("Models.Models.Permission", "Permission")
.WithMany("Role")
.HasForeignKey("PermisionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Models.Models.Role", "Roles")
.WithMany("Permissions")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Models.Models.Deal", b => modelBuilder.Entity("Models.Models.Deal", b =>
{ {
b.HasOne("Models.Models.User", "User") b.HasOne("Models.Models.User", "User")
@@ -180,13 +205,6 @@ namespace Data.Migrations
.IsRequired(); .IsRequired();
}); });
modelBuilder.Entity("Models.Models.Permission", b =>
{
b.HasOne("Models.Models.Role", null)
.WithMany("Permissions")
.HasForeignKey("RoleId");
});
modelBuilder.Entity("Models.Models.Product", b => modelBuilder.Entity("Models.Models.Product", b =>
{ {
b.HasOne("Models.Models.Deal", null) b.HasOne("Models.Models.Deal", null)

View File

@@ -1,4 +1,8 @@
using Data.Base; using Data.Base;
using Data.Models;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Models.Models namespace Models.Models
{ {
@@ -6,5 +10,7 @@ namespace Models.Models
{ {
public Permission() : base(){} public Permission() : base(){}
public Permission(string Name) : base(Name){} public Permission(string Name) : base(Name){}
[Required]
public virtual ICollection<RolePermission> Role { get; set; }
} }
} }

View File

@@ -1,17 +1,17 @@
using Data.Base; using Data.Base;
using Data.Models;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Models.Models namespace Models.Models
{ {
public class Role : BaseUserManagmentEntity public class Role : BaseUserManagmentEntity
{ {
public Role() : base(){} public Role() : base(){}
public Role(string Name, ICollection<Permission> Permissions) : base(Name) public Role(string Name) : base(Name)
{ {
this.Permissions = Permissions;
} }
[Required] public virtual ICollection<RolePermission> Permissions { get; set; } = new List<>
public virtual ICollection<Permission> Permissions { get; set; }
} }
} }

View File

@@ -0,0 +1,15 @@
using Models.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace Data.Models
{
public class RolePermission
{
public int RoleId { get; set; }
public virtual Role Roles { get; set; }
public int PermisionId { get; set; }
public virtual Permission Permission { get; set; }
}
}