public IHttpActionResult PostHotel(Hotel hotel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Hotel.Add(hotel);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (HotelExists(hotel.Hotel_No))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = hotel.Hotel_No }, hotel);
        }
        public IHttpActionResult PutHotel(int id, Hotel hotel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != hotel.Hotel_No)
            {
                return BadRequest();
            }

            db.Entry(hotel).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HotelExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
示例#3
0
        private static void Main(string[] args)
        {
            const string serverUrl = "http://localhost.fiddler:5000";

            var hotellist = new List<Hotel>();
            var roomlist = new List<Room>();
             //   Console.ReadLine( );

            Hotel aNewHotel = new Hotel()
            {
                Hotel_No = 345,Name = "Big Hotel",Address = "MyWay 1",Rating = "*"
            };

            string jsonaNewHotel = aNewHotel.SerializerJson();
            Console.WriteLine(jsonaNewHotel);

            Hotel deserializeHotel = Hotel.DeserializeJson(jsonaNewHotel);

            Console.WriteLine(deserializeHotel.ToString());

            Console.ReadLine();
            ////GetAHotel
            //Hotel selectedHotel = GetAHotel(serverUrl,3);
            //Console.WriteLine("After WebserviceCall");
            //Console.WriteLine(selectedHotel);

            //Exercise1
            //int selectHotel = 3;
            //Excercise1(serverUrl, selectHotel);

            //Exercise 2
            //Exercise2(serverUrl, hotellist);

            //Exercise 3
            //3.Select(HTTP Get) all hotels and all of them located in roskilde should be put into a list of hotels using LINQ
            //Exercise3(serverUrl, hotellist, roomlist);

            //Excercise4
            //Exercise4(serverUrl);

            //Exercise5
            //Exercise5(serverUrl);

            //Exercise6
            //int deleteHotelNo = 1002;
            //DeleteHotel(serverUrl, deleteHotelNo);

            //Exercise7
            //int hotelNo = 4;
            //int roomNo = 300;
            // exercise7(hotelNo, roomNo, serverUrl);

            //Exercise 8
            //8) Update all hotels in roskilde increase the price of a single
            //room with 20 %, show the prices before and after the update
              //  Exercise8(serverUrl, hotellist, roomlist);

            Console.ReadLine();

            //using (var client = new HttpClient())
            //{
            //    client.BaseAddress = new Uri(serverUrl);
            //    client.DefaultRequestHeaders.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //    try
            //    {
            //        var response =  client.GetAsync("Api/Hotels").Result;

            //        if (response.IsSuccessStatusCode)
            //        {
            //var hotels = response.Content.ReadAsAsync<IEnumerable<Hotel>>().Result;

            //            foreach (var hotel in hotels)
            //            {
            //                Console.WriteLine(hotel.ToString());
            //            }
            //            Console.ReadLine();
            //        }
            //    }
            //    catch (Exception)
            //    {
            //        throw;
            //    }
            //}

            //var MyNewHotel = new Hotel()
            //{
            //    Hotel_No = 1005,
            //    Address = "Fiddlerhotel 1",
            //    Name = "Fiddler hotel",
            //    Rating = "",
            //    HotelUrl = "fiddlerhotel.com",
            //    Room = new List<Room>()
            //};

            //string json = JsonConvert.SerializeObject(MyNewHotel);
            //Console.WriteLine("json file : " + json);
            //StringContent MyContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            //using (var client = new HttpClient())
            //{
            //    client.BaseAddress = new Uri(serverUrl);
            //    client.DefaultRequestHeaders.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //    try
            //    {
            //        var response = client.PostAsync("Api/Hotels", MyContent).Result;
            //        var r = response.Content;
            //        Console.WriteLine("response : " + response.StatusCode);
            //    }
            //    catch (Exception)
            //    {
            //        throw;
            //    }
            //}

            //int changeHotelNo = 1005;

            //var ChangeMyNewHotel = new Hotel()
            //{
            //    Hotel_No = changeHotelNo,
            //    Address = "Fiddlerhotel update",
            //    Name = "Fiddler hotel",
            //    Rating = "*",
            //    HotelUrl = "fiddlerhotel.com",
            //    Room = new List<Room>()
            //};

            //string jsonC = JsonConvert.SerializeObject(ChangeMyNewHotel);

            //StringContent content = new StringContent(jsonC, System.Text.Encoding.UTF8, "application/json");

            //using (var client = new HttpClient())
            //{
            //    client.BaseAddress = new Uri(serverUrl);
            //    client.DefaultRequestHeaders.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //    try
            //    {
            //        //var response = client.PostAsJsonAsync("Api/Hotels", MyContent).Result;
            //        var response = client.PutAsync("Api/Hotels/" + changeHotelNo, content).Result;

            //        var r = response.Content;
            //        Console.WriteLine("response : " + response.StatusCode);
            //    }
            //    catch (Exception)
            //    {
            //        throw;
            //    }
            //}

            //using (var client = new HttpClient())
            //{
            //    client.BaseAddress = new Uri(serverUrl);
            //    client.DefaultRequestHeaders.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //    try
            //    {
            //        //var response = client.PostAsJsonAsync("Api/Hotels", MyContent).Result;
            //        var response = client.DeleteAsync("Api/Hotels/" + 1005).Result;

            //        var r = response.Content;
            //        Console.WriteLine("response : " + response.StatusCode);
            //    }
            //    catch (Exception)
            //    {
            //        throw;
            //    }
            //}

            //Console.ReadLine();
        }
示例#4
0
        /// <summary>
        /// method to get a specific hotel
        /// </summary>
        /// <param name="serverUrl"></param>
        /// <param name="selectHotelNo"></param>
        /// <returns></returns>
        /// <summary>
        /// method to get a specific hotel
        /// </summary>
        /// <param name="serverUrl"></param>
        /// <param name="selectHotelNo"></param>
        /// <returns></returns>
        private static Hotel GetHotel(string serverUrl, int selectHotelNo)
        {
            var hotel = new Hotel();
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                string urlString = "api/hotels/" + selectHotelNo;
                var response = client.GetAsync(urlString).Result;
                Console.WriteLine("GetAsync : " + urlString);
                Console.WriteLine("Status code : " + response.StatusCode);
                if (response.IsSuccessStatusCode)
                {
                     hotel = response.Content.ReadAsAsync<Hotel>().Result;
                    Console.WriteLine("hotel : " + hotel.ToString());
                }
            }
            return hotel;
        }
示例#5
0
        private static void Exercise5(string serverUrl)
        {
            Console.WriteLine("Exercise 5");
            Console.WriteLine("Insert (HTTP Post) a new hotel fx number 200");

            int myNewHotelNo = 2000;
            //First we create the new hotel object
            var myNewHotel = new Hotel()
            {
                Hotel_No = myNewHotelNo,
                Address = "Fiddlerhotel 1",
                Name = "Fiddler hotel",
                Rating = "***",
                HotelUrl = "fiddlerhotel.com",
                Room = new List<Room>()
            };

            //The we need to serialize it
            string newHoteljson = JsonConvert.SerializeObject(myNewHotel);

            //Create the content we will send in the Http post request
            var content = new StringContent(newHoteljson, Encoding.UTF8, "application/json");

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = client.PostAsync("api/hotels", content).Result;
                Console.WriteLine("PostAsync");
                Console.WriteLine("Status code " + response.StatusCode);

                if (response.IsSuccessStatusCode)
                {
                    //Success , Now we can get the hotel by a Http post
                    var responseHotel = client.GetAsync("api/hotels/" + myNewHotelNo).Result;
                    Console.WriteLine("GetAsync");
                    Console.WriteLine("Status code " + response.StatusCode);

                    if (responseHotel.IsSuccessStatusCode)
                    {
                        var hotel200 = responseHotel.Content.ReadAsAsync<Hotel>().Result;
                        Console.WriteLine(hotel200.ToString());
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        /// Update (HTTP Put) hotel number 3, change the name of the hotel. 
        /// You have to create a new Hotel Object with the data and then use this object 
        /// when you create your content string. 
        /// </summary>
        /// <param name="serverUrl"></param>
        private static void Exercise4(string serverUrl)
        {
            var hotel3 = new Hotel();
            int hotelno = 3;
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("Application/json"));

                //First thing to do is to get the hotel number 3 into my hotel3 variable by a http Get request
                var hotelResponse = client.GetAsync("api/hotels/" + hotelno).Result;

                if (hotelResponse.IsSuccessStatusCode)
                {
                    hotel3 = hotelResponse.Content.ReadAsAsync<Hotel>().Result;
                    Console.WriteLine(hotel3.ToString());
                }

                //Now change the name and address of the hotel3
                hotel3.Name = "Great Hotel";
                hotel3.Address = "Great Hotel Road 1";

                //we have to serialize the hotel3 object into json format
                string jsonHotel3 = JsonConvert.SerializeObject(hotel3);

                //Create the content we want to send with the Http put request
                StringContent content = new StringContent(jsonHotel3, Encoding.UTF8, "Application/json");

                //Using a Http Put Request we can update the Hotel number 3
                var updateResponse = client.PutAsync("api/hotels/" + hotelno, content).Result;
                Console.WriteLine("status code : " + updateResponse.StatusCode);
            }
        }