From 431532e69f78a023fe6e9dbbd4aa199d0266406d Mon Sep 17 00:00:00 2001 From: Stedoss <29103029+Stedoss@users.noreply.github.com> Date: Sun, 30 Oct 2022 19:57:08 +0000 Subject: [PATCH] Make tags a many-to-many relationship with venues --- ...221030181404_Tags_Many_To_Many.Designer.cs | 158 ++++++++++++++++++ .../20221030181404_Tags_Many_To_Many.cs | 77 +++++++++ .../LeedsBeerQuestDbContextModelSnapshot.cs | 41 +++-- .../LeedsBeerQuest.API/Data/Models/Tag.cs | 4 + .../Data/Seed/LeedsBeerQuestSeeder.cs | 21 ++- .../LeedsBeerQuest.API.csproj | 2 +- 6 files changed, 285 insertions(+), 18 deletions(-) create mode 100644 backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.Designer.cs create mode 100644 backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.cs diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.Designer.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.Designer.cs new file mode 100644 index 0000000..78ea45f --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.Designer.cs @@ -0,0 +1,158 @@ +// +using System; +using LeedsBeerQuest.API.Data.Contexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LeedsBeerQuest.API.Data.Migrations +{ + [DbContext(typeof(LeedsBeerQuestDbContext))] + [Migration("20221030181404_Tags_Many_To_Many")] + partial class Tags_Many_To_Many + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "6.0.10"); + + modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Tag", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Tags"); + }); + + modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Address") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CategoryId") + .HasColumnType("INTEGER"); + + b.Property("DateAttended") + .HasColumnType("TEXT"); + + b.Property("Excerpt") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Latitude") + .HasColumnType("TEXT"); + + b.Property("Longitude") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Phone") + .HasColumnType("TEXT"); + + b.Property("StarsAmenities") + .HasColumnType("TEXT"); + + b.Property("StarsAtmosphere") + .HasColumnType("TEXT"); + + b.Property("StarsBeer") + .HasColumnType("TEXT"); + + b.Property("StarsValue") + .HasColumnType("TEXT"); + + b.Property("Thumbnail") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Twitter") + .HasColumnType("TEXT"); + + b.Property("Url") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Venues"); + }); + + modelBuilder.Entity("TagVenue", b => + { + b.Property("TagsId") + .HasColumnType("INTEGER"); + + b.Property("VenuesId") + .HasColumnType("INTEGER"); + + b.HasKey("TagsId", "VenuesId"); + + b.HasIndex("VenuesId"); + + b.ToTable("TagVenue"); + }); + + modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => + { + b.HasOne("LeedsBeerQuest.API.Data.Models.Category", "Category") + .WithMany() + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("TagVenue", b => + { + b.HasOne("LeedsBeerQuest.API.Data.Models.Tag", null) + .WithMany() + .HasForeignKey("TagsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LeedsBeerQuest.API.Data.Models.Venue", null) + .WithMany() + .HasForeignKey("VenuesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.cs new file mode 100644 index 0000000..4012a4b --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/20221030181404_Tags_Many_To_Many.cs @@ -0,0 +1,77 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LeedsBeerQuest.API.Data.Migrations +{ + public partial class Tags_Many_To_Many : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Tags_Venues_VenueId", + table: "Tags"); + + migrationBuilder.DropIndex( + name: "IX_Tags_VenueId", + table: "Tags"); + + migrationBuilder.DropColumn( + name: "VenueId", + table: "Tags"); + + migrationBuilder.CreateTable( + name: "TagVenue", + columns: table => new + { + TagsId = table.Column(type: "INTEGER", nullable: false), + VenuesId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TagVenue", x => new { x.TagsId, x.VenuesId }); + table.ForeignKey( + name: "FK_TagVenue_Tags_TagsId", + column: x => x.TagsId, + principalTable: "Tags", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TagVenue_Venues_VenuesId", + column: x => x.VenuesId, + principalTable: "Venues", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_TagVenue_VenuesId", + table: "TagVenue", + column: "VenuesId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "TagVenue"); + + migrationBuilder.AddColumn( + name: "VenueId", + table: "Tags", + type: "INTEGER", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Tags_VenueId", + table: "Tags", + column: "VenueId"); + + migrationBuilder.AddForeignKey( + name: "FK_Tags_Venues_VenueId", + table: "Tags", + column: "VenueId", + principalTable: "Venues", + principalColumn: "Id"); + } + } +} diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/LeedsBeerQuestDbContextModelSnapshot.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/LeedsBeerQuestDbContextModelSnapshot.cs index 6d81ca4..5c415f2 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/LeedsBeerQuestDbContextModelSnapshot.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Migrations/LeedsBeerQuestDbContextModelSnapshot.cs @@ -29,7 +29,7 @@ namespace LeedsBeerQuest.API.Data.Migrations b.HasKey("Id"); - b.ToTable("Categories", (string)null); + b.ToTable("Categories"); }); modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Tag", b => @@ -42,14 +42,9 @@ namespace LeedsBeerQuest.API.Data.Migrations .IsRequired() .HasColumnType("TEXT"); - b.Property("VenueId") - .HasColumnType("INTEGER"); - b.HasKey("Id"); - b.HasIndex("VenueId"); - - b.ToTable("Tags", (string)null); + b.ToTable("Tags"); }); modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => @@ -112,14 +107,22 @@ namespace LeedsBeerQuest.API.Data.Migrations b.HasIndex("CategoryId"); - b.ToTable("Venues", (string)null); + b.ToTable("Venues"); }); - modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Tag", b => + modelBuilder.Entity("TagVenue", b => { - b.HasOne("LeedsBeerQuest.API.Data.Models.Venue", null) - .WithMany("Tags") - .HasForeignKey("VenueId"); + b.Property("TagsId") + .HasColumnType("INTEGER"); + + b.Property("VenuesId") + .HasColumnType("INTEGER"); + + b.HasKey("TagsId", "VenuesId"); + + b.HasIndex("VenuesId"); + + b.ToTable("TagVenue"); }); modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => @@ -133,9 +136,19 @@ namespace LeedsBeerQuest.API.Data.Migrations b.Navigation("Category"); }); - modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => + modelBuilder.Entity("TagVenue", b => { - b.Navigation("Tags"); + b.HasOne("LeedsBeerQuest.API.Data.Models.Tag", null) + .WithMany() + .HasForeignKey("TagsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LeedsBeerQuest.API.Data.Models.Venue", null) + .WithMany() + .HasForeignKey("VenuesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); #pragma warning restore 612, 618 } diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Models/Tag.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Models/Tag.cs index 425c822..e874b84 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Models/Tag.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Models/Tag.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; namespace LeedsBeerQuest.API.Data.Models; @@ -7,4 +8,7 @@ public class Tag [Key] public int Id { get; set; } public string Name { get; set; } + + [JsonIgnore] + public virtual ICollection Venues { get; set; } } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs index be5c904..0e71200 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Seed/LeedsBeerQuestSeeder.cs @@ -70,14 +70,29 @@ public class LeedsBeerQuestSeeder _dbContext.Venues.AddRange(venues); - _dbContext.SaveChangesAsync(); + _dbContext.SaveChanges(); } private IEnumerable getTags(IEnumerable csvData) { - var tags = csvData.Select(d => d.tags).Distinct(); + var tagEntries = csvData.Select(d => d.tags); + var uniqueTags = new List(); + + foreach (var tagEntry in tagEntries) + { + var tags = tagEntry.Split(','); + + foreach (var tag in tags) + { + if (!uniqueTags.Contains(tag)) + { + uniqueTags.Add(tag); + } + } + } + var tagId = 0; - foreach (var tag in tags) + foreach (var tag in uniqueTags) { tagId++; yield return new Tag diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj index eaa913b..ac55e87 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/LeedsBeerQuest.API.csproj @@ -7,7 +7,7 @@ - TRACE + TRACE;LBQ_SEED_DATA