Пример #1
0
        public void initDatabase(int nmbrRestaurants = 2)
        {
            //geen data blijven toevoegen (MongoDB.Driver)
            try
            {
                //2. testRestaurants aanmaken
                for (var i = 0; i < nmbrRestaurants; i++)
                {
                    if (!restaurantRepo.CollectionExistsAsync("TestResto" + i).Result)
                    {
                        Guid currentId = Guid.NewGuid();
                        Lst_RestaurantGuids.Add(currentId);

                        restaurantRepo.CreateAsync(new Restaurant
                        {
                            RestaurantId   = currentId,
                            Name           = "TestResto" + i,
                            Description    = "Description TestResto" + i,
                            Long           = 51 + new Random().Next(10),
                            Lat            = 51 + new Random().Next(10),
                            Street         = "City TestResto" + i,
                            HouseNumber    = i.ToString(),
                            Main_city_name = "City TestResto" + i,
                        });
                    }
                }


                //3.Reviews toevoegen (enkel indien ook restaurants worden toegevoegd)
                if (Lst_RestaurantGuids.Count != 0)
                {
                    reviewRepo.CreateAsync(new Review
                    {
                        Id           = new MongoDB.Bson.ObjectId(),
                        RestaurantID = Lst_RestaurantGuids[new Random().Next(Lst_RestaurantGuids.Count)],
                        Subject      = "Pricing",
                        Comment      = "Too expensive",
                        Quotation    = 4.5M
                    });
                    reviewRepo.CreateAsync(new Review
                    {
                        Id           = new MongoDB.Bson.ObjectId(),
                        RestaurantID = Lst_RestaurantGuids[new Random().Next(Lst_RestaurantGuids.Count)],
                        Subject      = "Location",
                        Comment      = "Nice location in beautiful city.",
                        Quotation    = 7.2M
                    });

                    reviewRepo.CreateAsync(new Review
                    {
                        Id           = new MongoDB.Bson.ObjectId(),
                        RestaurantID = Lst_RestaurantGuids[new Random().Next(Lst_RestaurantGuids.Count)],
                        Subject      = "Service",
                        Comment      = "Excellent",
                        Quotation    = 8.0M
                    });


                    reviewRepo.CreateAsync(new Review
                    {
                        Id           = new MongoDB.Bson.ObjectId(),
                        RestaurantID = Lst_RestaurantGuids[new Random().Next(Lst_RestaurantGuids.Count)],
                        Subject      = "Location",
                        Comment      = "Difficult to find.",
                        Quotation    = 5
                    });

                    reviewRepo.CreateAsync(new Review
                    {
                        Id           = new MongoDB.Bson.ObjectId(),
                        RestaurantID = Lst_RestaurantGuids[new Random().Next(Lst_RestaurantGuids.Count)],
                        Subject      = "Location",
                        Comment      = "Beautiful garden and sunny terrace.",
                        Quotation    = 6
                    });

                    reviewRepo.CreateAsync(new Review
                    {
                        Id           = new MongoDB.Bson.ObjectId(),
                        RestaurantID = Lst_RestaurantGuids[new Random().Next(Lst_RestaurantGuids.Count)],
                        Subject      = "Food",
                        Comment      = "Excellent BBQ.",
                        Quotation    = 8
                    });

                    //zoekindexen aanmaken op Mongo
                    IndexKeysDefinition <Review> keys = "{ RestaurantID: 1 }";
                    var indexModel = new CreateIndexModel <Review>(keys);
                    context.Reviews.Indexes.CreateOneAsync(indexModel);

                    IndexKeysDefinition <Restaurant> Restaurantkeys = "{ RestaurantID: 1 }";
                    var indexModelComment = new CreateIndexModel <Restaurant>(Restaurantkeys);
                    context.Restaurants.Indexes.CreateOneAsync(indexModelComment);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("fout bij het seeden:", exc);
            }
        }