Add initial seed data

This commit is contained in:
Stedoss 2022-10-30 01:52:47 +01:00
parent f9dfe68409
commit add80a3521
5 changed files with 15047 additions and 1 deletions

View File

@ -1,4 +1,7 @@
using Microsoft.EntityFrameworkCore; using System.Globalization;
using CsvHelper;
using LeedsBeerQuest.API.Data.Models;
using Microsoft.EntityFrameworkCore;
namespace LeedsBeerQuest.API.Data.Seed; namespace LeedsBeerQuest.API.Data.Seed;
@ -15,5 +18,98 @@ public class LeedsBeerQuestSeeder
public void Seed() public void Seed()
{ {
IEnumerable<LeedsBeerQuestCSV>? lbqCSV;
using (var reader = new StreamReader("../leedsbeerquest.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
lbqCSV = csv.GetRecords<LeedsBeerQuestCSV>().ToList();
}
var tags = getTags(lbqCSV).ToList();
var categories = getCategories(lbqCSV).ToList();
_modelBuilder.Entity<Tag>().HasData(tags);
_modelBuilder.Entity<Category>().HasData(categories);
var venues = new List<Venue>();
var venueIndex = 1;
foreach (var venue in lbqCSV)
{
venues.Add(new Venue
{
Id = venueIndex,
Name = venue.name,
CategoryId = categories.Single(c => c.Name == venue.category).Id,
Url = venue.url,
DateAttended = venue.date,
Excerpt = venue.excerpt,
Thumbnail = venue.thumbnail,
Latitude = venue.lat,
Longitude = venue.lng,
Address = venue.address,
Phone = venue.phone,
Twitter = venue.twitter,
StarsBeer = venue.stars_beer,
StarsAtmosphere = venue.stars_atmosphere,
StarsAmenities = venue.stars_amenities,
StarsValue = venue.stars_value,
});
venueIndex++;
}
_modelBuilder.Entity<Venue>().HasData(venues);
}
private IEnumerable<Tag> getTags(IEnumerable<LeedsBeerQuestCSV> csvData)
{
var tags = csvData.Select(d => d.tags).Distinct();
var tagId = 0;
foreach (var tag in tags)
{
tagId++;
yield return new Tag
{
Id = tagId,
Name = tag
};
}
}
private IEnumerable<Category> getCategories(IEnumerable<LeedsBeerQuestCSV> csvData)
{
var categories = csvData.Select(d => d.category).Distinct();
var categoryId = 0;
foreach (var category in categories)
{
categoryId++;
yield return new Category
{
Id = categoryId,
Name = category
};
}
}
private class LeedsBeerQuestCSV
{
public string name { get; set; }
public string category { get; set; }
public string url { get; set; }
public DateTime date { get; set; }
public string excerpt { get; set; }
public string thumbnail { get; set; }
public decimal lat { get; set; }
public decimal lng { get; set; }
public string address { get; set; }
public string? phone { get; set; }
public string? twitter { get; set; }
public decimal stars_beer { get; set; }
public decimal stars_atmosphere { get; set; }
public decimal stars_amenities { get; set; }
public decimal stars_value { get; set; }
public string tags { get; set; }
} }
} }

View File

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CsvHelper" Version="29.0.0" />
<PackageReference Include="EntityFramework" Version="6.4.4" /> <PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.10"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.10">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>