<commit_before>using dwCheckApi.DatabaseContexts;
using dwCheckApi.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace dwCheckApi.DatabaseTools
{
    public class DatabaseSeeder
    {
        private DwContext _context;

        public DatabaseSeeder(DwContext context)
        {
            _context = context;
        }

        public void SeedBookEntitiesFromJson()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookSeedData.json");
            if (File.Exists(filePath))
            {
                var dataSet = File.ReadAllText(filePath);
                var seedData = JsonConvert.DeserializeObject<List<Book>>(dataSet);
                _context.Books.AddRange(seedData);
                _context.SaveChanges();
            }
        }

        public void SeedCharacterEntitiesFromJson()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "CharacterSeedData.json");
            if (File.Exists(filePath))
            {
                var dataSet = File.ReadAllText(filePath);
                var seedData = JsonConvert.DeserializeObject<List<Character>>(dataSet);
                _context.Characters.AddRange(seedData);
                _context.SaveChanges();
            }
        }

        public void SeedBookCharacterEntriesFromJson()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookCharacterSeedData.json");
            if (File.Exists(filePath))
            {
                var dataSet = File.ReadAllText(filePath);
                var seedData = JsonConvert.DeserializeObject<List<BookCharacterSeedData>>(dataSet);

                foreach(var bc in seedData)
                {
                    var book = _context.Books.Single(b => b.BookName == bc.BookName);
                    var characters = _context.Characters.Where(c => bc.CharacterNames.Contains(c.CharacterName));

                    foreach (var character in characters)
                    {
                        _context.BookCharacters.Add(new BookCharacter
                        {
                            Book = book,
                            Character = character
                        });
                    }
                }
                _context.SaveChanges();
                }
        }
    }
}<commit_msg>Ensured that all seeded data is distinct at seed time<commit_after>using dwCheckApi.DatabaseContexts;
using dwCheckApi.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace dwCheckApi.DatabaseTools
{
    public class DatabaseSeeder
    {
        private DwContext _context;

        public DatabaseSeeder(DwContext context)
        {
            _context = context;
        }

        public void SeedBookEntitiesFromJson()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookSeedData.json");
            if (File.Exists(filePath))
            {
                var dataSet = File.ReadAllText(filePath);
                var seedData = JsonConvert.DeserializeObject<List<Book>>(dataSet);

                // ensure that we only get the distinct books (based on their name)
                var distinctSeedData = seedData.GroupBy(b => b.BookName).Select(b => b.First());

                _context.Books.AddRange(seedData);
                _context.SaveChanges();
            }
        }

        public void SeedCharacterEntitiesFromJson()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "CharacterSeedData.json");
            if (File.Exists(filePath))
            {
                var dataSet = File.ReadAllText(filePath);
                var seedData = JsonConvert.DeserializeObject<IEnumerable<Character>>(dataSet);

                // ensure that we only get the distinct characters (based on their name)
                var distinctSeedData = seedData.GroupBy(c => c.CharacterName).Select(c => c.First());

                _context.Characters.AddRange(distinctSeedData);
                _context.SaveChanges();
            }
        }

        public void SeedBookCharacterEntriesFromJson()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookCharacterSeedData.json");
            if (File.Exists(filePath))
            {
                var dataSet = File.ReadAllText(filePath);
                var seedData = JsonConvert.DeserializeObject<List<BookCharacterSeedData>>(dataSet);

                foreach(var seedBook in seedData)
                {
                    var dbBook = _context.Books.Single(b => b.BookName == seedBook.BookName);

                    foreach (var seedChar in seedBook.CharacterNames)
                    {
                        var dbChar = _context.Characters.FirstOrDefault(c => c.CharacterName == seedChar);
                        if (dbChar != null)
                        {
                            _context.BookCharacters.Add(new BookCharacter
                            {
                                Book = dbBook,
                                Character = dbChar
                            }); 
                        }
                    }
                }
                _context.SaveChanges();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08690036900369004, 'fraction_numerical': 0.0, 'mean_word_length': 1.6137897782063646, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3293460', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1299, 'n_words': 251}