Make tags a many-to-many relationship with venues

This commit is contained in:
Stedoss 2022-10-30 19:57:08 +00:00
parent c9814a2142
commit 431532e69f
6 changed files with 285 additions and 18 deletions

View File

@ -0,0 +1,158 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Categories");
});
modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Tag", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Tags");
});
modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Address")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("CategoryId")
.HasColumnType("INTEGER");
b.Property<DateTime>("DateAttended")
.HasColumnType("TEXT");
b.Property<string>("Excerpt")
.IsRequired()
.HasColumnType("TEXT");
b.Property<decimal>("Latitude")
.HasColumnType("TEXT");
b.Property<decimal>("Longitude")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Phone")
.HasColumnType("TEXT");
b.Property<decimal>("StarsAmenities")
.HasColumnType("TEXT");
b.Property<decimal>("StarsAtmosphere")
.HasColumnType("TEXT");
b.Property<decimal>("StarsBeer")
.HasColumnType("TEXT");
b.Property<decimal>("StarsValue")
.HasColumnType("TEXT");
b.Property<string>("Thumbnail")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Twitter")
.HasColumnType("TEXT");
b.Property<string>("Url")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("CategoryId");
b.ToTable("Venues");
});
modelBuilder.Entity("TagVenue", b =>
{
b.Property<int>("TagsId")
.HasColumnType("INTEGER");
b.Property<int>("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
}
}
}

View File

@ -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<int>(type: "INTEGER", nullable: false),
VenuesId = table.Column<int>(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<int>(
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");
}
}
}

View File

@ -29,7 +29,7 @@ namespace LeedsBeerQuest.API.Data.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Categories", (string)null); b.ToTable("Categories");
}); });
modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Tag", b => modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Tag", b =>
@ -42,14 +42,9 @@ namespace LeedsBeerQuest.API.Data.Migrations
.IsRequired() .IsRequired()
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.Property<int?>("VenueId")
.HasColumnType("INTEGER");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("VenueId"); b.ToTable("Tags");
b.ToTable("Tags", (string)null);
}); });
modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b =>
@ -112,14 +107,22 @@ namespace LeedsBeerQuest.API.Data.Migrations
b.HasIndex("CategoryId"); 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) b.Property<int>("TagsId")
.WithMany("Tags") .HasColumnType("INTEGER");
.HasForeignKey("VenueId");
b.Property<int>("VenuesId")
.HasColumnType("INTEGER");
b.HasKey("TagsId", "VenuesId");
b.HasIndex("VenuesId");
b.ToTable("TagVenue");
}); });
modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b => modelBuilder.Entity("LeedsBeerQuest.API.Data.Models.Venue", b =>
@ -133,9 +136,19 @@ namespace LeedsBeerQuest.API.Data.Migrations
b.Navigation("Category"); 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 #pragma warning restore 612, 618
} }

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
namespace LeedsBeerQuest.API.Data.Models; namespace LeedsBeerQuest.API.Data.Models;
@ -7,4 +8,7 @@ public class Tag
[Key] [Key]
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
[JsonIgnore]
public virtual ICollection<Venue> Venues { get; set; }
} }

View File

@ -70,14 +70,29 @@ public class LeedsBeerQuestSeeder
_dbContext.Venues.AddRange(venues); _dbContext.Venues.AddRange(venues);
_dbContext.SaveChangesAsync(); _dbContext.SaveChanges();
} }
private IEnumerable<Tag> getTags(IEnumerable<LeedsBeerQuestCSV> csvData) private IEnumerable<Tag> getTags(IEnumerable<LeedsBeerQuestCSV> csvData)
{ {
var tags = csvData.Select(d => d.tags).Distinct(); var tagEntries = csvData.Select(d => d.tags);
var tagId = 0; var uniqueTags = new List<string>();
foreach (var tagEntry in tagEntries)
{
var tags = tagEntry.Split(',');
foreach (var tag in tags) foreach (var tag in tags)
{
if (!uniqueTags.Contains(tag))
{
uniqueTags.Add(tag);
}
}
}
var tagId = 0;
foreach (var tag in uniqueTags)
{ {
tagId++; tagId++;
yield return new Tag yield return new Tag

View File

@ -7,7 +7,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE;LBQ_SEED_DATA</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>