コード例 #1
0
        /// <summary>
        /// Load a batch of restaurants.
        /// </summary>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <returns></returns>
        private static IReadOnlyCollection <Restaurant> GetRestaurants(int skip, int take)
        {
            using (var ctx = new RestoContext())
            {
                ctx.Configuration.AutoDetectChangesEnabled = false;
                ctx.Configuration.LazyLoadingEnabled       = false;
                ctx.Configuration.ProxyCreationEnabled     = false;

                var restaurants = ctx.Restaurants
                                  .Include(r => r.Translations)
                                  .Include(r => r.Region.Translations)
                                  .Include(r => r.Cuisines.Select(a => a.Cuisine.Translations))
                                  .Include(r => r.Accommodations.Select(a => a.Accommodation.Translations))
                                  .Include(r => r.PaymentFacilities.Select(a => a.PaymentFacility.Translations))
                                  .OrderBy(r => r.Name)
                                  .Skip(skip)
                                  .Take(take)
                                  .ToList();
                if (restaurants.Count == 0)
                {
                    return(null);
                }
                return(restaurants.ToList());
            }
        }
コード例 #2
0
        private static Guid PersistToDatabase(RegisterRestoCommand command, Coordinates coordinates)
        {
            using (var ctx = new RestoContext())
            {
                var restaurant = ctx.Restaurants.Add(new Restaurant()
                {
                    Id            = Guid.NewGuid(),
                    Locality      = command.City,
                    StreetAddress = command.Street,
                    Name          = command.Name
                });

                if (coordinates != null)
                {
                    restaurant.Latitude  = coordinates.Latitude;
                    restaurant.Longitude = coordinates.Longitude;
                }

                if (!String.IsNullOrEmpty(command.Region))
                {
                    restaurant.RegionId = int.Parse(command.Region);
                }

                if (!String.IsNullOrEmpty(command.Cuisine))
                {
                    restaurant.Cuisines = new Collection <RestaurantCuisine>
                    {
                        new RestaurantCuisine
                        {
                            Id        = Guid.NewGuid(),
                            CuisineId = int.Parse(command.Cuisine)
                        }
                    };
                }

                if (command.SelectedAccommodationIds != null)
                {
                    restaurant.Accommodations = new Collection <RestaurantAccommodation>();

                    foreach (var accommodation in command.SelectedAccommodationIds)
                    {
                        restaurant.Accommodations.Add(new RestaurantAccommodation
                        {
                            Id = Guid.NewGuid(),
                            AccommodationId = accommodation
                        });
                    }
                }

                ctx.SaveChanges();

                Console.WriteLine("Persisted to DB.");

                return(restaurant.Id);
            }
        }
コード例 #3
0
        private static void PersistToSearch(Guid id, RegisterRestoCommand command, Coordinates coordinates)
        {
            using (var ctx = new RestoContext())
            {
                var connection = ApiConnection.Create(CloudConfigurationManager.GetSetting("Azure.Search.ServiceName"),
                                                      CloudConfigurationManager.GetSetting("Azure.Search.ApiKey"));
                var indexClient = new IndexManagementClient(connection);
                var indexName   = CloudConfigurationManager.GetSetting("Azure.Search.IndexName");

                var restaurant = ctx.Restaurants
                                 .Include(r => r.Accommodations.Select(a => a.Accommodation.Translations))
                                 .FirstOrDefault(r => r.Id == id);


                var operation = new IndexOperation(IndexOperationType.MergeOrUpload, "id", id.ToString())
                                .WithProperty("name", command.Name)
                                .WithProperty("locality", command.City)
                                .WithProperty("budget", command.Budget)
                                .WithProperty("rating", command.Rating)
                                .WithProperty("street", command.Street)
                                .WithProperty("accommodations", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("en"))
                                .WithProperty("accommodations_fr", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("fr"))
                                .WithProperty("accommodations_nl", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("nl"))
                                .WithProperty("cuisine", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("en"))
                                .WithProperty("cuisine_fr", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("fr"))
                                .WithProperty("cuisine_nl", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("nl"));

                if (coordinates != null)
                {
                    operation.WithGeographyPoint("location", coordinates.Longitude, coordinates.Latitude);
                }

                var response = indexClient.PopulateAsync(indexName, operation).Result;

                // Error handling!
                if (!response.IsSuccess)
                {
                    Console.WriteLine("Error: " + response.StatusCode);
                    return;
                }
                else
                {
                    var failed = response.Body.Where(r => !r.Status);
                    foreach (var item in failed)
                    {
                        Console.WriteLine("Failed: {0} ({1})", item.Key, item.ErrorMessage);
                    }
                }

                Console.WriteLine("Persisted to Search.");
            }
        }
コード例 #4
0
        public ActionResult Index(string name, int skip = 0)
        {
            using (var ctx = new RestoContext())
            {
                ViewBag.Name    = name;
                ViewBag.Count   = ctx.Restaurants.Count();
                ViewBag.Showing = String.Format("Showing {0} to {1} of {2}.", skip + 1, skip + 50, ViewBag.Count);
                if (skip > 0)
                {
                    ViewBag.PreviousPage = Url.Action("Index", new { name, skip = skip - 50 });
                }
                ViewBag.NextPage = Url.Action("Index", new { name, skip = skip + 50 });

                if (!String.IsNullOrEmpty(name))
                {
                    return(View(ctx.Restaurants.Where(r => r.Name.Contains(name)).OrderByDescending(r => r.Budget).Skip(skip).Take(50).ToList()));
                }
                return(View(ctx.Restaurants.OrderByDescending(r => r.Budget).Skip(skip).Take(50).ToList()));
            }
        }
コード例 #5
0
 public ActionResult Index()
 {
     using (var ctx = new RestoContext())
     {
         var vm = new RegisterViewModel();
         vm.Regions = ctx.RegionTranslations
                      .Where(r => r.Language == "en").ToList()
                      .OrderBy(r => r.Title)
                      .Select(r => new SelectListItem {
             Text = r.Title, Value = r.ParentId.ToString(CultureInfo.InvariantCulture)
         });
         vm.Cuisines = ctx.CuisineTranslations
                       .Where(c => c.Language == "en").ToList()
                       .OrderBy(c => c.Title)
                       .Select(c => new SelectListItem {
             Text = c.Title, Value = c.ParentId.ToString(CultureInfo.InvariantCulture)
         });
         vm.Accomodations = ctx.AccommodationTranslations
                            .Where(a => a.Language == "en")
                            .OrderBy(a => a.Title)
                            .ToList();
         return(View(vm));
     }
 }
コード例 #6
0
 public RepositoryIngredient(RestoContext context) : base(context)
 {
 }
コード例 #7
0
 public RepositoryRestaurant(RestoContext context) : base(context)
 {
 }
コード例 #8
0
 public UnitOfWork(RestoContext context)
 {
     _restoContext = context;
     Restaurants   = new RepositoryRestaurant(_restoContext);
     Ingredients   = new RepositoryIngredient(_restoContext);
 }