Exemple #1
0
        public async Task <ActionResult <Review> > PostReview([FromBody] Review review)
        {
            //  Location location = mapper.Map<Location>(destinationDTO.LocationDTO);
            // Location createdLocation = await genericLocationRepo.Create(location);
            if (review == null)
            {
                return(BadRequest(new { Message = "No price input" }));
            }

            try
            {
                var result = await repo.CreateAsync(review);

                if (result == null)
                {
                    return(BadRequest(new { Message = $"Review about {review.Subject} could not be saved" }));
                }
                return(Created("api/reviews", review));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("HandleErrorCode", "Error", new
                {
                    statusCode = 400,
                    errorMessage = $"Creating review about {review.Subject} failed : {ex}"
                }));
            }
        }
Exemple #2
0
        public async Task initDatabase(int nmbrFlights = 2)
        {
            //geen data blijven toevoegen (MongoDB.Driver)
            context.Database.DropCollection("reviews");
            context.Database.DropCollection("flights");
            context.Database.DropCollection("authors");
            //  context.Database.DropCollection("subjectCategories");
            if (await context.Reviews.EstimatedDocumentCountAsync() == 0)
            {
                try
                {
                    //await context.SubjectCategories.InsertManyAsync(subjects);
                    await context.Flights.InsertManyAsync(Flights);

                    await context.Authors.InsertOneAsync(Author);

                    //3.Reviews toevoegen
                    await reviewRepo.CreateAsync(new Review
                    {
                        Id = new MongoDB.Bson.ObjectId(),
                        //FlightId = Lst_FlightGuids[new Random().Next(Lst_FlightGuids.Count)],
                        FlightId = Lst_FlightGuids[0],
                        Flight   = Flights[0],
                        // UserId = Guid.NewGuid(),
                        UserId = Author.UserId,
                        //Subject =
                        //new SubjectCategorie
                        //{
                        //    Subject = "Pricing"
                        //},
                        Subject = "Pricing",
                        Comment = "Too expensive",
                        Rating  = 2.5M,
                        Author  = Author
                    });;
                    await reviewRepo.CreateAsync(new Review
                    {
                        Id = new MongoDB.Bson.ObjectId(),
                        // FlightId = Lst_FlightGuids[new Random().Next(Lst_FlightGuids.Count)],
                        FlightId = Lst_FlightGuids[1],
                        Flight   = Flights[1],
                        UserId   = Author.UserId,
                        //Subject = new SubjectCategorie
                        //{
                        //    Subject = "Pricing"
                        //},

                        Subject = "Pricing",
                        Comment = "Cheap enough for me.",
                        Rating  = 3.2M,
                        Author  = Author
                    });

                    await reviewRepo.CreateAsync(new Review
                    {
                        Id = new MongoDB.Bson.ObjectId(),
                        // FlightId = Lst_FlightGuids[new Random().Next(Lst_FlightGuids.Count)],
                        FlightId = Lst_FlightGuids[0],
                        Flight   = Flights[0],
                        UserId   = Author.UserId,
                        // Subject = new SubjectCategorie { Subject = "Service" },
                        Subject = "Service",
                        Comment = "Excellent",
                        Rating  = 4.5M,
                        Author  = Author
                    });


                    await reviewRepo.CreateAsync(new Review
                    {
                        Id = new MongoDB.Bson.ObjectId(),
                        // FlightId = Lst_FlightGuids[new Random().Next(Lst_FlightGuids.Count)],
                        FlightId = Lst_FlightGuids[1],
                        Flight   = Flights[1],
                        UserId   = Author.UserId,
                        //Subject = new SubjectCategorie
                        //{
                        //    Subject = "Service"
                        //},
                        Subject = "Service",
                        Comment = "Abysmal.",
                        Rating  = 1,
                        Author  = Author
                    });

                    await reviewRepo.CreateAsync(new Review
                    {
                        Id = new MongoDB.Bson.ObjectId(),
                        //  FlightId = Lst_FlightGuids[new Random().Next(Lst_FlightGuids.Count)],
                        FlightId = Lst_FlightGuids[0],
                        Flight   = Flights[0],
                        UserId   = Author.UserId,
                        //Subject = new SubjectCategorie { Subject= "Food" },
                        Subject = "Food",
                        Comment = "They gave my grandma rice, against her will.",
                        Rating  = 2,
                        Author  = Author
                    });

                    await reviewRepo.CreateAsync(new Review
                    {
                        Id = new MongoDB.Bson.ObjectId(),
                        //  FlightId = Lst_FlightGuids[new Random().Next(Lst_FlightGuids.Count)],
                        FlightId = Lst_FlightGuids[1],
                        Flight   = Flights[1],
                        UserId   = Author.UserId,
                        //Subject = new SubjectCategorie
                        //{
                        //    Subject = "Food"
                        //},
                        Subject = "Food",
                        Comment = "Excellent cheesecrackers.",
                        Rating  = 4,
                        Author  = Author
                    });

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

                    //IndexKeysDefinition<Flight>Flightkeys = "{FlightId: 1 }";
                    //var indexModelComment = new CreateIndexModel<Flight>(Flightkeys);
                    //context.Flights.Indexes.CreateOneAsync(indexModelComment);
                }
                catch (Exception exc)
                {
                    Console.WriteLine("fout bij het seeden:", exc);
                }
            }
        }
        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);
            }
        }