Ejemplo n.º 1
0
        public static void Seed(ApplicationDbContext context)
        {
            var updateFile = $"{ConfigurationService.InstalledPath}{Path.DirectorySeparatorChar}config{Path.DirectorySeparatorChar}setup.sql";

            if (File.Exists(updateFile))
            {
                var sql = File.ReadAllText(updateFile);
                context.Database.ExecuteSqlRaw(sql);
                File.Delete(updateFile);
            }

            if (!context.Tags.Any())
            {
                //load persona service and create tag for each distinct tag found there
                var s = PersonaService.LoadDefaults();

                var personaSeedValuesEnumerable = s as PersonaSeedValues[] ?? s.ToArray();
                foreach (var item in personaSeedValuesEnumerable.GroupBy(elem => elem.Name).Select(group => group.First()))
                {
                    context.Personas.Add(new Persona {
                        Name = item.Name
                    });
                }
                foreach (var item in personaSeedValuesEnumerable.GroupBy(elem => elem.Tag).Select(group => group.First()))
                {
                    context.Tags.Add(new Tag {
                        Name = item.Tag
                    });
                }
                context.SaveChanges();

                foreach (var item in personaSeedValuesEnumerable)
                {
                    var tag     = context.Tags.FirstOrDefault(o => o.Name == item.Tag);
                    var persona = context.Personas.FirstOrDefault(o => o.Name != null && o.Name == item.Name);
                    if (tag != null)
                    {
                        persona?.PersonaTags.Add(new PersonaTag {
                            TagId = tag.Id, High = item.High, Low = item.Low
                        });
                    }
                }
                context.SaveChanges();

                var agentId = Guid.NewGuid();
                context.Agents.Add(new Agent {
                    Id = agentId, MachineId = Guid.NewGuid(), Username = "******", FirstName = "Jed", LastName = "Eckert"
                });

                var random = new Random();
                var x      = context.Personas.FirstOrDefault(o => o.Name.ToLower() == "sports");
                if (x?.PersonaTags != null)
                {
                    foreach (var personaTag in x.PersonaTags)
                    {
                        var tag = context.Tags.FirstOrDefault(o => o.Id == personaTag.TagId);
                        context.AgentTags.Add(new AgentTag {
                            AgentId = agentId, Score = random.Next(personaTag.Low, personaTag.High), Tag = tag
                        });
                    }

                    agentId = Guid.NewGuid();
                    context.Agents.Add(new Agent
                    {
                        Id = agentId, MachineId = Guid.NewGuid(), Username = "******", FirstName = "Andy", LastName = "Tanner"
                    });
                    x = context.Personas.FirstOrDefault(o => o.Name.ToLower() == "news");
                    if (x?.PersonaTags != null)
                    {
                        foreach (var personaTag in x.PersonaTags)
                        {
                            var tag = context.Tags.FirstOrDefault(o => o.Id == personaTag.TagId);
                            context.AgentTags.Add(new AgentTag
                            {
                                AgentId = agentId, Score = random.Next(personaTag.Low, personaTag.High), Tag = tag
                            });
                        }
                    }
                }

                context.SaveChanges();

                Loaders.LoadSeedData();
            }
        }