From a97d5954b5b410ed3c98dcc323d8dc491e41ac4f Mon Sep 17 00:00:00 2001 From: Jonas Anker Rasmussen Date: Wed, 28 Dec 2022 18:08:08 +0100 Subject: [PATCH] Add Foreign Key reference to Purchase-Product --- ...e-Product ForeignKey reference.Designer.cs | 611 ++++++++++++++++++ ...7_Purchase-Product ForeignKey reference.cs | 39 ++ .../CoffeeCardContextModelSnapshot.cs | 12 + .../CoffeeCard.Models/Entities/Product.cs | 2 + .../CoffeeCard.Models/Entities/Purchase.cs | 12 +- 5 files changed, 672 insertions(+), 4 deletions(-) create mode 100644 coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.Designer.cs create mode 100644 coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.cs diff --git a/coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.Designer.cs b/coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.Designer.cs new file mode 100644 index 00000000..3125efbb --- /dev/null +++ b/coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.Designer.cs @@ -0,0 +1,611 @@ +// +using System; +using CoffeeCard.Library.Persistence; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace CoffeeCard.Library.Migrations +{ + [DbContext(typeof(CoffeeCardContext))] + [Migration("20221228170637_Purchase-Product ForeignKey reference")] + partial class PurchaseProductForeignKeyreference + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("dbo") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.13") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("CoffeeCard.Models.Entities.LoginAttempt", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Time") + .HasColumnType("datetime2"); + + b.Property("User_Id") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("User_Id"); + + b.ToTable("LoginAttempts"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("ExperienceWorth") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("NumberOfTickets") + .HasColumnType("int"); + + b.Property("Price") + .HasColumnType("int"); + + b.Property("Visible") + .HasColumnType("bit"); + + b.HasKey("Id"); + + b.ToTable("Products"); + + b.HasData( + new + { + Id = 1, + Description = "Used for filter coffee brewed with fresh ground coffee", + ExperienceWorth = 10, + Name = "Filter Coffee", + NumberOfTickets = 10, + Price = 80, + Visible = true + }, + new + { + Id = 2, + Description = "Used for specialities like espresso, cappuccino, caffe latte, cortado, americano and chai latte", + ExperienceWorth = 150, + Name = "Espresso Based", + NumberOfTickets = 10, + Price = 150, + Visible = true + }); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.ProductUserGroup", b => + { + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("UserGroup") + .HasColumnType("int"); + + b.HasKey("ProductId", "UserGroup"); + + b.ToTable("ProductUserGroups"); + + b.HasData( + new + { + ProductId = 1, + UserGroup = 0 + }, + new + { + ProductId = 2, + UserGroup = 0 + }); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Programme", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("FullName") + .HasColumnType("nvarchar(max)"); + + b.Property("ShortName") + .HasColumnType("nvarchar(max)"); + + b.Property("SortPriority") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Programmes"); + + b.HasData( + new + { + Id = 1, + FullName = "BSc Software Development", + ShortName = "SWU", + SortPriority = 0 + }, + new + { + Id = 2, + FullName = "BSc Global Business Informatics", + ShortName = "GBI", + SortPriority = 0 + }, + new + { + Id = 3, + FullName = "BSc Digital Design and Interactive Technologies", + ShortName = "BDDIT", + SortPriority = 0 + }, + new + { + Id = 4, + FullName = "MSc Digital Design and Interactive Technologies", + ShortName = "KDDIT", + SortPriority = 0 + }, + new + { + Id = 5, + FullName = "MSc Digital Innovation and Management", + ShortName = "DIM", + SortPriority = 0 + }, + new + { + Id = 6, + FullName = "MSc E-Business", + ShortName = "E-BUSS", + SortPriority = 0 + }, + new + { + Id = 7, + FullName = "MSc Games - Design and Theory", + ShortName = "GAMES/DT", + SortPriority = 0 + }, + new + { + Id = 8, + FullName = "MSc Games - Technology", + ShortName = "GAMES/Tech", + SortPriority = 0 + }, + new + { + Id = 9, + FullName = "MSc Computer Science", + ShortName = "CS", + SortPriority = 0 + }, + new + { + Id = 10, + FullName = "MSc Software Development (Design)", + ShortName = "SDT", + SortPriority = 0 + }, + new + { + Id = 11, + FullName = "Employee", + ShortName = "Employee", + SortPriority = 0 + }, + new + { + Id = 12, + FullName = "Other", + ShortName = "Other", + SortPriority = 0 + }, + new + { + Id = 13, + FullName = "BSc Data Science", + ShortName = "DS", + SortPriority = 0 + }); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Purchase", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Completed") + .HasColumnType("bit"); + + b.Property("DateCreated") + .HasColumnType("datetime2"); + + b.Property("NumberOfTickets") + .HasColumnType("int"); + + b.Property("OrderId") + .HasColumnType("nvarchar(450)"); + + b.Property("PaymentType") + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("ProductName") + .HasColumnType("nvarchar(max)"); + + b.Property("PurchasedBy_Id") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("nvarchar(max)"); + + b.Property("TransactionId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ProductId"); + + b.HasIndex("PurchasedBy_Id"); + + b.HasIndex("TransactionId"); + + b.ToTable("Purchases"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Statistic", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("ExpiryDate") + .HasColumnType("datetime2"); + + b.Property("LastSwipe") + .HasColumnType("datetime2"); + + b.Property("Preset") + .HasColumnType("int"); + + b.Property("SwipeCount") + .HasColumnType("int"); + + b.Property("User_Id") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("User_Id"); + + b.ToTable("Statistics"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Ticket", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("DateCreated") + .HasColumnType("datetime2"); + + b.Property("DateUsed") + .HasColumnType("datetime2"); + + b.Property("IsUsed") + .HasColumnType("bit"); + + b.Property("Owner_Id") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("Purchase_Id") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Owner_Id"); + + b.HasIndex("Purchase_Id"); + + b.ToTable("Tickets"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Token", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("TokenHash") + .HasColumnType("nvarchar(max)"); + + b.Property("User_Id") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("User_Id"); + + b.ToTable("Tokens"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("DateCreated") + .HasColumnType("datetime2"); + + b.Property("DateUpdated") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Experience") + .HasColumnType("int"); + + b.Property("IsVerified") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PrivacyActivated") + .HasColumnType("bit"); + + b.Property("Programme_Id") + .HasColumnType("int"); + + b.Property("Salt") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserGroup") + .HasColumnType("int"); + + b.Property("UserState") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("Programme_Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Voucher", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Code") + .HasColumnType("nvarchar(max)"); + + b.Property("DateCreated") + .HasColumnType("datetime2"); + + b.Property("DateUsed") + .HasColumnType("datetime2"); + + b.Property("Product_Id") + .HasColumnType("int"); + + b.Property("User_Id") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Product_Id"); + + b.HasIndex("User_Id"); + + b.ToTable("Vouchers"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.WebhookConfiguration", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("LastUpdated") + .HasColumnType("datetime2"); + + b.Property("SignatureKey") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Url") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("WebhookConfigurations"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.LoginAttempt", b => + { + b.HasOne("CoffeeCard.Models.Entities.User", "User") + .WithMany("LoginAttempts") + .HasForeignKey("User_Id"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.ProductUserGroup", b => + { + b.HasOne("CoffeeCard.Models.Entities.Product", "Product") + .WithMany("ProductUserGroup") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Purchase", b => + { + b.HasOne("CoffeeCard.Models.Entities.Product", "Product") + .WithMany("Purchases") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CoffeeCard.Models.Entities.User", "PurchasedBy") + .WithMany("Purchases") + .HasForeignKey("PurchasedBy_Id"); + + b.Navigation("Product"); + + b.Navigation("PurchasedBy"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Statistic", b => + { + b.HasOne("CoffeeCard.Models.Entities.User", "User") + .WithMany("Statistics") + .HasForeignKey("User_Id"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Ticket", b => + { + b.HasOne("CoffeeCard.Models.Entities.User", "Owner") + .WithMany("Tickets") + .HasForeignKey("Owner_Id"); + + b.HasOne("CoffeeCard.Models.Entities.Purchase", "Purchase") + .WithMany("Tickets") + .HasForeignKey("Purchase_Id"); + + b.Navigation("Owner"); + + b.Navigation("Purchase"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Token", b => + { + b.HasOne("CoffeeCard.Models.Entities.User", "User") + .WithMany("Tokens") + .HasForeignKey("User_Id"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.User", b => + { + b.HasOne("CoffeeCard.Models.Entities.Programme", "Programme") + .WithMany("Users") + .HasForeignKey("Programme_Id"); + + b.Navigation("Programme"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Voucher", b => + { + b.HasOne("CoffeeCard.Models.Entities.Product", "Product") + .WithMany() + .HasForeignKey("Product_Id"); + + b.HasOne("CoffeeCard.Models.Entities.User", "User") + .WithMany() + .HasForeignKey("User_Id"); + + b.Navigation("Product"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Product", b => + { + b.Navigation("ProductUserGroup"); + + b.Navigation("Purchases"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Programme", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.Purchase", b => + { + b.Navigation("Tickets"); + }); + + modelBuilder.Entity("CoffeeCard.Models.Entities.User", b => + { + b.Navigation("LoginAttempts"); + + b.Navigation("Purchases"); + + b.Navigation("Statistics"); + + b.Navigation("Tickets"); + + b.Navigation("Tokens"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.cs b/coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.cs new file mode 100644 index 00000000..03881747 --- /dev/null +++ b/coffeecard/CoffeeCard.Library/Migrations/20221228170637_Purchase-Product ForeignKey reference.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace CoffeeCard.Library.Migrations +{ + public partial class PurchaseProductForeignKeyreference : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateIndex( + name: "IX_Purchases_ProductId", + schema: "dbo", + table: "Purchases", + column: "ProductId"); + + migrationBuilder.AddForeignKey( + name: "FK_Purchases_Products_ProductId", + schema: "dbo", + table: "Purchases", + column: "ProductId", + principalSchema: "dbo", + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Purchases_Products_ProductId", + schema: "dbo", + table: "Purchases"); + + migrationBuilder.DropIndex( + name: "IX_Purchases_ProductId", + schema: "dbo", + table: "Purchases"); + } + } +} diff --git a/coffeecard/CoffeeCard.Library/Migrations/CoffeeCardContextModelSnapshot.cs b/coffeecard/CoffeeCard.Library/Migrations/CoffeeCardContextModelSnapshot.cs index f74d84c7..2e7c9d82 100644 --- a/coffeecard/CoffeeCard.Library/Migrations/CoffeeCardContextModelSnapshot.cs +++ b/coffeecard/CoffeeCard.Library/Migrations/CoffeeCardContextModelSnapshot.cs @@ -276,6 +276,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("OrderId"); + b.HasIndex("ProductId"); + b.HasIndex("PurchasedBy_Id"); b.HasIndex("TransactionId"); @@ -500,10 +502,18 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("CoffeeCard.Models.Entities.Purchase", b => { + b.HasOne("CoffeeCard.Models.Entities.Product", "Product") + .WithMany("Purchases") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("CoffeeCard.Models.Entities.User", "PurchasedBy") .WithMany("Purchases") .HasForeignKey("PurchasedBy_Id"); + b.Navigation("Product"); + b.Navigation("PurchasedBy"); }); @@ -567,6 +577,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("CoffeeCard.Models.Entities.Product", b => { b.Navigation("ProductUserGroup"); + + b.Navigation("Purchases"); }); modelBuilder.Entity("CoffeeCard.Models.Entities.Programme", b => diff --git a/coffeecard/CoffeeCard.Models/Entities/Product.cs b/coffeecard/CoffeeCard.Models/Entities/Product.cs index fce894cf..9b9639cb 100644 --- a/coffeecard/CoffeeCard.Models/Entities/Product.cs +++ b/coffeecard/CoffeeCard.Models/Entities/Product.cs @@ -22,6 +22,8 @@ public sealed class Product : IEquatable [Required] [DefaultValue(true)] public bool Visible { get; set; } public ICollection ProductUserGroup { get; set; } + + public ICollection Purchases { get; set; } public bool Equals(Product other) { diff --git a/coffeecard/CoffeeCard.Models/Entities/Purchase.cs b/coffeecard/CoffeeCard.Models/Entities/Purchase.cs index 2e315710..a0a8f697 100644 --- a/coffeecard/CoffeeCard.Models/Entities/Purchase.cs +++ b/coffeecard/CoffeeCard.Models/Entities/Purchase.cs @@ -6,7 +6,7 @@ namespace CoffeeCard.Models.Entities { - // TODO Should me marked as unique + // FIXME Should me marked as unique [Index(nameof(OrderId))] [Index(nameof(TransactionId))] public class Purchase @@ -33,12 +33,16 @@ public Purchase() /// /// Id of Product purchased /// - /// + /// Product Id /// 2 - - // FIXME: Foreign reference? public int ProductId { get; set; } + /// + /// Product purchased + /// + /// Product + public Product Product { get; set; } + /// /// Price for purchase in Danish kroner (kr) ///