Models Updated

This commit is contained in:
thermalthrottle
2021-03-18 13:08:18 +02:00
parent 9f7dcd71d5
commit 7aac7f999f
15 changed files with 130 additions and 345 deletions

View File

@@ -9,55 +9,31 @@ namespace Business.Business.UserManagment
public class CreateInitialUser public class CreateInitialUser
{ {
private LuminousContext context; private LuminousContext context;
private string RoleName;
private string Username; private string Username;
private string Password; private string Password;
private UserController userctl; private UserController userctl;
public CreateInitialUser(string RoleName, string Username, string Password) public CreateInitialUser(string Username, string Password)
{ {
userctl = new UserController(); userctl = new UserController();
this.RoleName = RoleName;
this.Username = Username; this.Username = Username;
this.Password = Password; this.Password = Password;
} }
public void CreatePermissions() public void CreateRoles()
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
var admin = new Permission("Admin"); var Admin = new Role("Admin");
var roleChanger = new Permission("Role Creator"); var Manager = new Role("Manager");
var userCreation = new Permission("User Creator"); var Cashier = new Role("Cashier");
var report = new Permission("Report"); context.Role.AddRange(Admin, Manager, Cashier);
var stock = new Permission("Stock");
var sell = new Permission("Sell");
context.Permission.AddRange
(
admin,
roleChanger,
userCreation,
report,
stock,
sell
);
context.SaveChanges(); context.SaveChanges();
Console.WriteLine("Permissions were intialized");
}
}
public void CreateFirstRole()
{
using (context = new LuminousContext())
{
var AdminPermission = context.Permission.FirstOrDefault(p => p.Name == "Admin");
userctl.CreateRole(this.RoleName , AdminPermission);
} }
} }
public void CreateFirstUser() public void CreateFirstUser()
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
var roleToAttach = context.Role.Where(r => r.Name == this.RoleName).FirstOrDefault(); int roleToAttach = context.Role.FirstOrDefault(r => r.Name == "Admin").Id;
userctl.CreateUser(this.Username, this.Password, roleToAttach); userctl.CreateUser(this.Username, this.Password, roleToAttach);
} }
} }

View File

@@ -1,5 +1,4 @@
using Data.Models; using Models;
using Models;
using Models.Models; using Models.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -12,26 +11,11 @@ namespace Business.Business.UserManagment
public class UserController public class UserController
{ {
private LuminousContext context; private LuminousContext context;
public void CreateRole(string RoleName, Permission Permission) public void CreateUser(string Username, string Password, int RoleId)
{
using (context = new LuminousContext())
{
var role = new Role(RoleName);
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();
}
}
public void CreateUser(string Username, string Password, Role Role)
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
var user = new User(Username, Password, Role); var user = new User(Username, Password, RoleId);
context.User.Add(user); context.User.Add(user);
context.SaveChanges(); context.SaveChanges();
} }

View File

@@ -10,26 +10,24 @@ namespace Business.Business.UserManagment
public class UserValidator public class UserValidator
{ {
private LuminousContext context; private LuminousContext context;
public bool CheckIfUserIsCreated() public void CheckIfUserEverCreated()
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
if (context.User.ToList().Any()) if (context.User.ToList().Any())
{ {
return true; throw new ArgumentException("First user is already created!");
} }
return false;
} }
} }
public bool CheckPassword(string Password) public void CheckPassword(string Password)
{ {
using (context = new LuminousContext()) using (context = new LuminousContext())
{ {
if (context.User.ToList().Exists(user => user.Password == Password)) if (context.User.ToList().Exists(user => user.Password == Password))
{ {
return true; throw new ArgumentException("Invalid User!");
} }
return false;
} }
} }
} }

View File

@@ -11,15 +11,16 @@ namespace Display
static void Main(string[] args) static void Main(string[] args)
{ {
var val = new UserValidator(); var val = new UserValidator();
if (!val.CheckIfUserIsCreated()) try
{ {
var InitialCreation = new CreateInitialUser("Admin", "Admin", "pass123"); val.CheckIfUserEverCreated();
InitialCreation.CreateFirstRole(); var InitialCreation = new CreateInitialUser("Admin", "pass123");
InitialCreation.CreateRoles();
InitialCreation.CreateFirstUser(); InitialCreation.CreateFirstUser();
} }
else catch (Exception e)
{ {
Console.WriteLine("Already created"); Console.WriteLine(e.Message);
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using Data; using Data;
using Data.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Models.Models; using Models.Models;
@@ -19,11 +18,9 @@ namespace Models
public DbSet<User> User { get; set; } public DbSet<User> User { get; set; }
public DbSet<Role> Role { get; set; } public DbSet<Role> Role { get; set; }
public DbSet<Permission> Permission { get; set; }
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)
{ {
@@ -40,22 +37,9 @@ namespace Models
modelBuilder.Entity<Role>() modelBuilder.Entity<Role>()
.HasIndex(role => new { role.Name }) .HasIndex(role => new { role.Name })
.IsUnique(true); .IsUnique(true);
modelBuilder.Entity<Permission>()
.HasIndex(permission => new { permission.Name })
.IsUnique(true);
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("20210317183331_InitialMigration")] [Migration("20210318105825_init")]
partial class InitialMigration partial class init
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
{ {
@@ -21,21 +21,6 @@ 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")
@@ -43,6 +28,12 @@ namespace Data.Migrations
.HasColumnType("int") .HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<double>("Amount")
.HasColumnType("float");
b.Property<int>("ProductId")
.HasColumnType("int");
b.Property<byte[]>("Time") b.Property<byte[]>("Time")
.IsConcurrencyToken() .IsConcurrencyToken()
.IsRequired() .IsRequired()
@@ -54,30 +45,13 @@ namespace Data.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ProductId");
b.HasIndex("UserId"); b.HasIndex("UserId");
b.ToTable("Deal"); b.ToTable("Deal");
}); });
modelBuilder.Entity("Models.Models.Permission", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Permission");
});
modelBuilder.Entity("Models.Models.Product", b => modelBuilder.Entity("Models.Models.Product", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@@ -88,9 +62,6 @@ namespace Data.Migrations
b.Property<double>("AmountInStock") b.Property<double>("AmountInStock")
.HasColumnType("float"); .HasColumnType("float");
b.Property<int?>("DealId")
.HasColumnType("int");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(450)"); .HasColumnType("nvarchar(450)");
@@ -98,18 +69,11 @@ namespace Data.Migrations
b.Property<double>("Price") b.Property<double>("Price")
.HasColumnType("float"); .HasColumnType("float");
b.Property<int?>("StockId")
.HasColumnType("int");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("DealId");
b.HasIndex("Name") b.HasIndex("Name")
.IsUnique(); .IsUnique();
b.HasIndex("StockId");
b.ToTable("Product"); b.ToTable("Product");
}); });
@@ -139,6 +103,12 @@ namespace Data.Migrations
.HasColumnType("int") .HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<double>("Amount")
.HasColumnType("float");
b.Property<int>("ProductId")
.HasColumnType("int");
b.Property<byte[]>("Time") b.Property<byte[]>("Time")
.IsConcurrencyToken() .IsConcurrencyToken()
.IsRequired() .IsRequired()
@@ -150,6 +120,8 @@ namespace Data.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ProductId");
b.HasIndex("UserId"); b.HasIndex("UserId");
b.ToTable("Stock"); b.ToTable("Stock");
@@ -183,23 +155,14 @@ 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.Product", "Products")
.WithMany()
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Models.Models.User", "User") b.HasOne("Models.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserId") .HasForeignKey("UserId")
@@ -207,19 +170,14 @@ namespace Data.Migrations
.IsRequired(); .IsRequired();
}); });
modelBuilder.Entity("Models.Models.Product", b =>
{
b.HasOne("Models.Models.Deal", null)
.WithMany("Products")
.HasForeignKey("DealId");
b.HasOne("Models.Models.Stock", null)
.WithMany("Products")
.HasForeignKey("StockId");
});
modelBuilder.Entity("Models.Models.Stock", b => modelBuilder.Entity("Models.Models.Stock", b =>
{ {
b.HasOne("Models.Models.Product", "Products")
.WithMany()
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Models.Models.User", "User") b.HasOne("Models.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserId") .HasForeignKey("UserId")

View File

@@ -3,21 +3,23 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Data.Migrations namespace Data.Migrations
{ {
public partial class InitialMigration : Migration public partial class init : Migration
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Permission", name: "Product",
columns: table => new columns: table => new
{ {
Id = table.Column<int>(nullable: false) Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"), .Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: false) Name = table.Column<string>(nullable: false),
Price = table.Column<double>(nullable: false),
AmountInStock = table.Column<double>(nullable: false)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Permission", x => x.Id); table.PrimaryKey("PK_Product", x => x.Id);
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
@@ -33,30 +35,6 @@ namespace Data.Migrations
table.PrimaryKey("PK_Role", x => x.Id); table.PrimaryKey("PK_Role", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "RolePermission",
columns: table => new
{
RoleId = table.Column<int>(nullable: false),
PermisionId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_RolePermission", x => new { x.RoleId, x.PermisionId });
table.ForeignKey(
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,
principalTable: "Role",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "User", name: "User",
columns: table => new columns: table => new
@@ -85,11 +63,19 @@ namespace Data.Migrations
Id = table.Column<int>(nullable: false) Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"), .Annotation("SqlServer:Identity", "1, 1"),
UserId = table.Column<int>(nullable: false), UserId = table.Column<int>(nullable: false),
ProductId = table.Column<int>(nullable: false),
Amount = table.Column<double>(nullable: false),
Time = table.Column<byte[]>(rowVersion: true, nullable: false) Time = table.Column<byte[]>(rowVersion: true, nullable: false)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Deal", x => x.Id); table.PrimaryKey("PK_Deal", x => x.Id);
table.ForeignKey(
name: "FK_Deal_Product_ProductId",
column: x => x.ProductId,
principalTable: "Product",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey( table.ForeignKey(
name: "FK_Deal_User_UserId", name: "FK_Deal_User_UserId",
column: x => x.UserId, column: x => x.UserId,
@@ -105,11 +91,19 @@ namespace Data.Migrations
Id = table.Column<int>(nullable: false) Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"), .Annotation("SqlServer:Identity", "1, 1"),
UserId = table.Column<int>(nullable: false), UserId = table.Column<int>(nullable: false),
ProductId = table.Column<int>(nullable: false),
Amount = table.Column<double>(nullable: false),
Time = table.Column<byte[]>(rowVersion: true, nullable: false) Time = table.Column<byte[]>(rowVersion: true, nullable: false)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Stock", x => x.Id); table.PrimaryKey("PK_Stock", x => x.Id);
table.ForeignKey(
name: "FK_Stock_Product_ProductId",
column: x => x.ProductId,
principalTable: "Product",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey( table.ForeignKey(
name: "FK_Stock_User_UserId", name: "FK_Stock_User_UserId",
column: x => x.UserId, column: x => x.UserId,
@@ -118,62 +112,22 @@ namespace Data.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateIndex(
name: "Product", name: "IX_Deal_ProductId",
columns: table => new table: "Deal",
{ column: "ProductId");
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: false),
Price = table.Column<double>(nullable: false),
AmountInStock = table.Column<double>(nullable: false),
DealId = table.Column<int>(nullable: true),
StockId = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Product", x => x.Id);
table.ForeignKey(
name: "FK_Product_Deal_DealId",
column: x => x.DealId,
principalTable: "Deal",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Product_Stock_StockId",
column: x => x.StockId,
principalTable: "Stock",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Deal_UserId", name: "IX_Deal_UserId",
table: "Deal", table: "Deal",
column: "UserId"); column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_Permission_Name",
table: "Permission",
column: "Name",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Product_DealId",
table: "Product",
column: "DealId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Product_Name", name: "IX_Product_Name",
table: "Product", table: "Product",
column: "Name", column: "Name",
unique: true); unique: true);
migrationBuilder.CreateIndex(
name: "IX_Product_StockId",
table: "Product",
column: "StockId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Role_Name", name: "IX_Role_Name",
table: "Role", table: "Role",
@@ -181,9 +135,9 @@ namespace Data.Migrations
unique: true); unique: true);
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_RolePermission_PermisionId", name: "IX_Stock_ProductId",
table: "RolePermission", table: "Stock",
column: "PermisionId"); column: "ProductId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Stock_UserId", name: "IX_Stock_UserId",
@@ -204,12 +158,6 @@ namespace Data.Migrations
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable(
name: "Product");
migrationBuilder.DropTable(
name: "RolePermission");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Deal"); name: "Deal");
@@ -217,7 +165,7 @@ namespace Data.Migrations
name: "Stock"); name: "Stock");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Permission"); name: "Product");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "User"); name: "User");

View File

@@ -19,21 +19,6 @@ 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")
@@ -41,6 +26,12 @@ namespace Data.Migrations
.HasColumnType("int") .HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<double>("Amount")
.HasColumnType("float");
b.Property<int>("ProductId")
.HasColumnType("int");
b.Property<byte[]>("Time") b.Property<byte[]>("Time")
.IsConcurrencyToken() .IsConcurrencyToken()
.IsRequired() .IsRequired()
@@ -52,30 +43,13 @@ namespace Data.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ProductId");
b.HasIndex("UserId"); b.HasIndex("UserId");
b.ToTable("Deal"); b.ToTable("Deal");
}); });
modelBuilder.Entity("Models.Models.Permission", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Permission");
});
modelBuilder.Entity("Models.Models.Product", b => modelBuilder.Entity("Models.Models.Product", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@@ -86,9 +60,6 @@ namespace Data.Migrations
b.Property<double>("AmountInStock") b.Property<double>("AmountInStock")
.HasColumnType("float"); .HasColumnType("float");
b.Property<int?>("DealId")
.HasColumnType("int");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(450)"); .HasColumnType("nvarchar(450)");
@@ -96,18 +67,11 @@ namespace Data.Migrations
b.Property<double>("Price") b.Property<double>("Price")
.HasColumnType("float"); .HasColumnType("float");
b.Property<int?>("StockId")
.HasColumnType("int");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("DealId");
b.HasIndex("Name") b.HasIndex("Name")
.IsUnique(); .IsUnique();
b.HasIndex("StockId");
b.ToTable("Product"); b.ToTable("Product");
}); });
@@ -137,6 +101,12 @@ namespace Data.Migrations
.HasColumnType("int") .HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<double>("Amount")
.HasColumnType("float");
b.Property<int>("ProductId")
.HasColumnType("int");
b.Property<byte[]>("Time") b.Property<byte[]>("Time")
.IsConcurrencyToken() .IsConcurrencyToken()
.IsRequired() .IsRequired()
@@ -148,6 +118,8 @@ namespace Data.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ProductId");
b.HasIndex("UserId"); b.HasIndex("UserId");
b.ToTable("Stock"); b.ToTable("Stock");
@@ -181,23 +153,14 @@ 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.Product", "Products")
.WithMany()
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Models.Models.User", "User") b.HasOne("Models.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserId") .HasForeignKey("UserId")
@@ -205,19 +168,14 @@ namespace Data.Migrations
.IsRequired(); .IsRequired();
}); });
modelBuilder.Entity("Models.Models.Product", b =>
{
b.HasOne("Models.Models.Deal", null)
.WithMany("Products")
.HasForeignKey("DealId");
b.HasOne("Models.Models.Stock", null)
.WithMany("Products")
.HasForeignKey("StockId");
});
modelBuilder.Entity("Models.Models.Stock", b => modelBuilder.Entity("Models.Models.Stock", b =>
{ {
b.HasOne("Models.Models.Product", "Products")
.WithMany()
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Models.Models.User", "User") b.HasOne("Models.Models.User", "User")
.WithMany() .WithMany()
.HasForeignKey("UserId") .HasForeignKey("UserId")

View File

@@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text; using System.Text;
namespace Data.Base namespace Data.Base
@@ -12,17 +13,26 @@ namespace Data.Base
{ {
} }
protected BaseSales(User User, ICollection<Product> Products) protected BaseSales(int UserId, int ProductId, double Amount)
{ {
this.User = User; this.UserId = UserId;
this.Products = Products; this.ProductId = ProductId;
this.Amount = Amount;
} }
[Key] [Key]
public int Id { get; set; } public int Id { get; set; }
[Required] [Required]
[ForeignKey("User")]
public int UserId { get; set; }
[Required]
public virtual User User { get; set; } public virtual User User { get; set; }
[Required] [Required]
public virtual ICollection<Product> Products { get; set; } [ForeignKey("Product")]
public int ProductId { get; set; }
[Required]
public virtual Product Products { get; set; }
[Required]
public double Amount { get; set; }
[Timestamp] [Timestamp]
[Required] [Required]
public byte[] Time { get; set; } public byte[] Time { get; set; }

View File

@@ -6,6 +6,6 @@ namespace Models.Models
public class Deal : BaseSales public class Deal : BaseSales
{ {
public Deal() : base(){} public Deal() : base(){}
public Deal(User User, ICollection<Product> Products) : base(User, Products){} public Deal(int UserId, int ProductId, double Amount) : base(UserId, ProductId, Amount) { }
} }
} }

View File

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

View File

@@ -1,5 +1,4 @@
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; using System.ComponentModel.DataAnnotations.Schema;
@@ -9,9 +8,6 @@ namespace Models.Models
public class Role : BaseUserManagmentEntity public class Role : BaseUserManagmentEntity
{ {
public Role() : base(){} public Role() : base(){}
public Role(string Name) : base(Name) public Role(string Name) : base(Name){}
{
}
public virtual ICollection<RolePermission> Permissions { get; set; } = new List<>
} }
} }

View File

@@ -1,15 +0,0 @@
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; }
}
}

View File

@@ -9,6 +9,6 @@ namespace Models.Models
public class Stock : BaseSales public class Stock : BaseSales
{ {
public Stock() : base(){} public Stock() : base(){}
public Stock(User User, ICollection<Product> Products) : base(User, Products){} public Stock(int UserId, int ProductId, double Amount) : base(UserId, ProductId, Amount){}
} }
} }

View File

@@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text; using System.Text;
namespace Models.Models namespace Models.Models
@@ -9,14 +10,16 @@ namespace Models.Models
public class User : BaseUserManagmentEntity public class User : BaseUserManagmentEntity
{ {
public User() : base() { } public User() : base() { }
public User(string Name, string Password, Role Role) : base(Name) public User(string Name, string Password, int RoleId) : base(Name)
{ {
this.Password = Password; this.Password = Password;
this.Role = Role; this.RoleId = RoleId;
} }
[Required] [Required]
public string Password { get; set; } public string Password { get; set; }
[Required] [Required]
[ForeignKey("Role")]
public int RoleId { get; set; }
public virtual Role Role { get; set; } public virtual Role Role { get; set; }
} }
} }