コード例 #1
0
        private static void CreateHotel()
        {
            Console.WriteLine("Please enter hotel name:");
            var name = Console.ReadLine();

            Console.WriteLine("Please enter hotel address:");
            var address = Console.ReadLine();

            var hotel = new Hotel {
                Name = name, Address = address
            };

            using (var context = new HotelAdvisorContext())
            {
                context.Hotels.Add(hotel);
                context.SaveChanges();

                Console.WriteLine("Hotels in our database:");

                var query = from h in context.Hotels
                            orderby h.Name
                            select h;

                foreach (var h in query)
                {
                    Console.WriteLine(string.Format("Hotel id: {0}, name: {1}, address:{2}", h.Id, h.Name, h.Address));
                }

                Console.WriteLine("Press any key to exit application.");
                Console.ReadKey();
            }
        }
コード例 #2
0
        // GET: Hotel

        public ActionResult Index()
        {
            HotelAdvisorContext context = new HotelAdvisorContext();
            var model = context.Hotels.ToList();

            return(View(model));
        }
コード例 #3
0
        public HotelDetailsViewModel GetHotelDetails(int hotelId)
        {
            HotelDetailsViewModel hotelDetails = new HotelDetailsViewModel();

            using (var dbContext = new HotelAdvisorContext())
            {
                Hotel hotel = dbContext.Hotels.Include(h => h.Comments)
                              .FirstOrDefault(h => h.Id == hotelId);


                hotelDetails =
                    new HotelDetailsViewModel()
                {
                    HotelName     = hotel.Name,
                    HotelId       = hotel.Id,
                    AverageRating = hotel.Comments.Count() == 0 ? 0 : hotel.Comments.Average(c => c.Rating),
                    TotalReviews  = hotel.Comments.Count(),
                    Image         = hotel.Image,
                    Comments      = hotel.Comments.Select(co => new CommentViewModel()
                    {
                        UserName  = co.User.UserName,
                        Text      = co.Text,
                        Title     = co.Title,
                        DateAdded = co.DateAdded,
                        Rating    = co.Rating,
                        Id        = co.Id
                    }).ToList()
                };
            }
            return(hotelDetails);
        }
コード例 #4
0
 public void CreateHotel(Hotel hotel)
 {
     using (var context = new HotelAdvisorContext())
     {
         context.Hotels.Add(hotel);
         context.SaveChanges();
     }
 }
コード例 #5
0
 public void CreateUser(User user)
 {
     using (var context = new HotelAdvisorContext())
     {
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
コード例 #6
0
 public void CreateComment(Comment comment)
 {
     using (var context = new HotelAdvisorContext())
     {
         context.Comments.Add(comment);
         context.SaveChanges();
     }
 }
コード例 #7
0
        public Hotel Find(int id)
        {
            using (var dbContext = new HotelAdvisorContext())
            {
                this.hotel = dbContext.Hotels.Find(id);
            }

            return(hotel);
        }
コード例 #8
0
        public Hotel FindHotelById(int id)
        {
            Hotel hotel;

            using (var context = new HotelAdvisorContext())
            {
                hotel = context.Hotels.Find(id);
            }
            return(hotel);
        }
コード例 #9
0
        public User FindUserById(int id)
        {
            User user;

            using (var context = new HotelAdvisorContext())
            {
                user = context.Users.Find(id);
            }
            return(user);
        }
コード例 #10
0
        public Comment FindCommentById(int id)
        {
            Comment comment;

            using (var context = new HotelAdvisorContext())
            {
                comment = context.Comments.Find(id);
            }
            return(comment);
        }
コード例 #11
0
 public void Edit(Hotel newHotel)
 {
     using (var dbContext = new HotelAdvisorContext())
     {
         this.hotel = dbContext.Hotels.Find(newHotel.Id);
         this.Populate(newHotel);
         dbContext.Entry(hotel).State = System.Data.Entity.EntityState.Modified;
         dbContext.SaveChanges();
     }
 }
コード例 #12
0
        public decimal FindHotelAverageRating(int id)
        {
            decimal averageRating;

            using (var context = new HotelAdvisorContext())
            {
                context.Hotels.Include("Comments");
                var hotel = context.Hotels.Find(id);
                averageRating = hotel.Comments.Count == 0 ? 0 : hotel.Comments.Average(c => c.Rating);
            }
            return(averageRating);
        }
コード例 #13
0
        public HotelDetailsViewModel GetHotelDetails(int hotelId)
        {
            HotelDetailsViewModel hotelDetails = new HotelDetailsViewModel();

            using (var context = new HotelAdvisorContext())
            {
                hotelDetails = context.Hotels.Where(h => h.Id == hotelId).Include(h => h.Comments).Select(
                    s => new HotelDetailsViewModel()
                {
                    HotelId       = s.Id,
                    HotelName     = s.Name,
                    City          = s.City,
                    Description   = s.Description,
                    Address       = s.Address,
                    Image         = s.Image,
                    TotalReveiws  = s.Comments.Count(),
                    AverageRating = (s.Comments.Count() == 0) ? 0 : s.Comments.Average(c => c.Rating),
                    Comments      = s.Comments.ToList()
                }).FirstOrDefault();
            }

            return(hotelDetails);
        }
コード例 #14
0
        public List <HotelDetailsViewModel> GetHotels()
        {
            List <HotelDetailsViewModel> hotels = new List <HotelDetailsViewModel>();

            using (var dbContext = new HotelAdvisorContext())
            {
                List <Hotel> hotelList = dbContext.Hotels.ToList();

                foreach (var hotel in hotelList)
                {
                    hotels.Add(
                        new HotelDetailsViewModel()
                    {
                        HotelName     = hotel.Name,
                        HotelId       = hotel.Id,
                        Description   = hotel.Description,
                        Address       = hotel.Address,
                        City          = hotel.City,
                        HouseNumber   = hotel.HouseNumber,
                        AverageRating = hotel.Comments.Count() == 0 ? 0 : hotel.Comments.Average(c => c.Rating),
                        Image         = hotel.Image
                                        //TotalReviews = hotel.Comments.Count(),
                                        //Comments = hotel.Comments.Select(co => new CommentViewModel()
                                        //{
                                        //    UserName = co.User.UserName,
                                        //    Text = co.Text,
                                        //    DateAdded = co.DateAdded,
                                        //    Rating = co.Rating,
                                        //    Id = co.Id
                                        //}).ToList()
                    }
                        );
                }
            }

            return(hotels);
        }