Beispiel #1
0
        private void SeedDb(int count)
        {
            using (var context = DictContext.CreateContext(_connString, true))
            {
                var c = context.Collections.First();

                for (int i = 0; i < count; i++)
                {
                    var snd = context.Files.Add(new FileDescription {
                        Name = $"sound{i}.mp3"
                    }).Entity;
                    var img = (i % 2 == 0) ? context.Files.Add(new FileDescription {
                        Name = $"image{i}.png"
                    }).Entity : null;

                    context.Cards.Add(new Card {
                        Word         = $"word{i * 2}",
                        CollectionId = c.Id,
                        SoundName    = snd.Name,
                        ImageName    = img?.Name
                    });
                }

                context.SaveChanges();
            }
        }
Beispiel #2
0
        /*private object GetPropValue(object obj, string propName)
         * {
         *  return obj.GetType().GetProperty(propName).GetValue(obj);
         * }*/

        private ICardRepository GetRepository(DictContext context)
        {
            var logger         = Logging.ConsoleLoggerProvider.CreateLogger <CardRepository>();
            var fileRepository = new InMemoryFileRepository();
            var repository     = new CardRepository(context, fileRepository, logger);

            return(repository);
        }
Beispiel #3
0
        private Collection InitCollection(DictContext context)
        {
            var collection = context.Collections
                             .FirstOrDefault(c => c.Name == "LinguaLeo");

            if (collection == null)
            {
                collection = context.Collections.Add(new Collection {
                    Name = "LinguaLeo", Description = "Imported LinguaLeo dictionary"
                }).Entity;
            }

            return(collection);
        }
Beispiel #4
0
        public void Dispose()
        {
            Client.Dispose();
            _server.Dispose();

            // delete database
            using (var context = DictContext.CreateContext(_connString, false))
            {
                context.Database.EnsureDeleted();
            }

            // remove media directory
            System.IO.Directory.Delete(Models.FileRepository.MEDIA_DIR);
        }
Beispiel #5
0
        private int SeedDb(DbContextOptions <DictContext> options, int count, bool create2Collections)
        {
            int collectionId;

            using (var context = new DictContext(options))
            {
                var c = context.Collections.Add(new Collection {
                    Name = "collection"
                });
                collectionId = c.Entity.Id;

                for (int i = 0; i < count; i++)
                {
                    var snd = context.Files.Add(new FileDescription {
                        Name = $"sound{i}.mp3"
                    }).Entity;
                    var img = (i % 2 == 0) ? context.Files.Add(new FileDescription {
                        Name = $"image{i}.png"
                    }).Entity : null;

                    context.Cards.Add(new Card {
                        Word         = $"word{i * 2}",
                        CollectionId = collectionId,
                        SoundName    = snd.Name,
                        ImageName    = img?.Name
                    });

                    if (create2Collections)
                    {
                        context.Cards.Add(new Card {
                            Word         = $"word{count + i}",
                            CollectionId = collectionId + 1
                        });
                    }
                }

                context.SaveChanges();
            }

            return(collectionId);
        }
Beispiel #6
0
        public void ImportCsv(string fileName)
        {
            var newNames = new Dictionary <string, string>();

            using (var fs = System.IO.File.OpenText(System.IO.Path.Combine(StorePath, "newnames.csv")))
            {
                while (!fs.EndOfStream)
                {
                    var s      = fs.ReadLine();
                    var fields = s.Split(',');
                    newNames.Add(fields[1], fields[0]);
                }
            }

            md5 = MD5.Create();

            Console.WriteLine("Import from {0}", fileName);
            using (var context = DictContext.CreateContext(_connString, false))
            {
                var collection = InitCollection(context);
                var wordsDict  = context.Cards.ToDictionary(c => c.Word);
                var files      = new HashSet <string>(context.Files.Select(f => f.Name));

                using (var fs = System.IO.File.OpenText(System.IO.Path.Combine(StorePath, fileName)))
                {
                    while (!fs.EndOfStream)
                    {
                        var s      = fs.ReadLine();
                        var fields = s.Split(';');

                        if (fields.Length != 6)
                        {
                            continue;
                        }

                        var word = fields[0];

                        //TODO: make Word as primary key?
                        //Too slow!!!
                        //var card = context.Cards.FirstOrDefault(c => string.Equals(c.Word, word, StringComparison.OrdinalIgnoreCase) && c.CollectionId != collection.Id);
                        Card card;

                        bool IsNew = !wordsDict.TryGetValue(word, out card) || card.Collection != collection;

                        if (IsNew)
                        {
                            card = new Card {
                                Word = word, Collection = collection
                            };
                            context.Cards.Add(card);
                            wordsDict.Add(word, card);
                        }

                        card.Translation   = fields[1];
                        card.Transcription = fields[3];
                        card.Context       = fields[4];


                        var    name = fields[2];
                        string newName;
                        if (name != "")
                        {
                            var pos = name.LastIndexOf('/');
                            name = name.Substring(pos + 1);
                            if (newNames.TryGetValue(name, out newName))
                            {
                                var hash = System.IO.Path.GetFileNameWithoutExtension(newName);
                                // no hash dubbling check
                                if (!files.Contains(newName))
                                {
                                    context.Files.Add(new FileDescription {
                                        Name = newName, FileType = FileType.Image, Hash = hash
                                    });
                                    files.Add(newName);
                                }

                                //??? take first if we get more than one word: begin, began, begun
                                string lcWord = word.Contains(",") ? word.Split(',').First().ToLower() : word.ToLower();
                                if (!context.WordsToFiles.Any(wf => wf.Word == lcWord && wf.FileName == newName))
                                {
                                    context.WordsToFiles.Add(new WordToFile {
                                        Word = lcWord, FileName = newName
                                    });
                                }

                                card.ImageName = newName;
                            }
                        }

                        name = fields[5];
                        if (name != "")
                        {
                            var pos = name.LastIndexOf('/');
                            name = name.Substring(pos + 1);
                            if (newNames.TryGetValue(name, out newName))
                            {
                                var hash = System.IO.Path.GetFileNameWithoutExtension(newName);
                                if (!files.Contains(newName))
                                {
                                    context.Files.Add(new FileDescription {
                                        Name = newName, FileType = FileType.Sound, Hash = hash
                                    });
                                    files.Add(newName);
                                }

                                string lcWord = word.ToLower();
                                if (!context.WordsToFiles.Any(wf => wf.Word == lcWord && wf.FileName == newName))
                                {
                                    context.WordsToFiles.Add(new WordToFile {
                                        Word = lcWord, FileName = newName
                                    });
                                }

                                card.SoundName = newName;
                            }
                        }

                        //card.Image = AddMedia(card.Image, fields[2], FileType.Image, app, srcDict, cards);
                        //card.Sound = AddMedia(card.Sound, fields[5], FileType.Sound, app, srcDict, cards);
                    }
                }

                Console.WriteLine("Save");
                context.SaveChanges();
            }
            Console.WriteLine("Done");
        }
Beispiel #7
0
 public HomeController(IWordsService wordsUrlParser, DictContext dictContext)
 {
     _wordsUrlParser = wordsUrlParser;
     _dictContext    = dictContext;
 }