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
{
private LuminousContext context;
private string RoleName;
private string Username;
private string Password;
private UserController userctl;
public CreateInitialUser(string RoleName, string Username, string Password)
public CreateInitialUser(string Username, string Password)
{
userctl = new UserController();
this.RoleName = RoleName;
this.Username = Username;
this.Password = Password;
}
public void CreatePermissions()
public void CreateRoles()
{
using (context = new LuminousContext())
{
var admin = new Permission("Admin");
var roleChanger = new Permission("Role Creator");
var userCreation = new Permission("User Creator");
var report = new Permission("Report");
var stock = new Permission("Stock");
var sell = new Permission("Sell");
context.Permission.AddRange
(
admin,
roleChanger,
userCreation,
report,
stock,
sell
);
var Admin = new Role("Admin");
var Manager = new Role("Manager");
var Cashier = new Role("Cashier");
context.Role.AddRange(Admin, Manager, Cashier);
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()
{
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);
}
}

View File

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

View File

@@ -10,26 +10,24 @@ namespace Business.Business.UserManagment
public class UserValidator
{
private LuminousContext context;
public bool CheckIfUserIsCreated()
public void CheckIfUserEverCreated()
{
using (context = new LuminousContext())
{
if (context.User.ToList().Any())
{
return true;
}
return false;
throw new ArgumentException("First user is already created!");
}
}
public bool CheckPassword(string Password)
}
public void CheckPassword(string Password)
{
using (context = new LuminousContext())
{
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)
{
var val = new UserValidator();
if (!val.CheckIfUserIsCreated())
try
{
var InitialCreation = new CreateInitialUser("Admin", "Admin", "pass123");
InitialCreation.CreateFirstRole();
val.CheckIfUserEverCreated();
var InitialCreation = new CreateInitialUser("Admin", "pass123");
InitialCreation.CreateRoles();
InitialCreation.CreateFirstUser();
}
else
catch (Exception e)
{
Console.WriteLine("Already created");
Console.WriteLine(e.Message);
}
}
}

View File

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

View File

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

View File

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

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
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.Products = Products;
this.UserId = UserId;
this.ProductId = ProductId;
this.Amount = Amount;
}
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("User")]
public int UserId { get; set; }
[Required]
public virtual User User { get; set; }
[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]
[Required]
public byte[] Time { get; set; }

View File

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

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