コード例 #1
0
        public IHttpActionResult PostRoom(Room room)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Room.Add(room);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (RoomExists(room.Room_No))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = room.Room_No }, room);
        }
コード例 #2
0
        private static void CreateRoom(string serverUrl, Room myNewRoom)
        {
            string roomJson = JsonConvert.SerializeObject(myNewRoom);

            StringContent content = new StringContent(roomJson, Encoding.UTF8, "application/json");

            //Create a Http post
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                string posturl = "api/rooms";
                var response = client.PostAsync(posturl, content).Result;
                Console.WriteLine("Post async " + posturl);
                Console.WriteLine("Status code " + response.StatusCode);
            }
        }
コード例 #3
0
        public IHttpActionResult PutRoom(int id, Room room)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != room.Room_No)
            {
                return BadRequest();
            }

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }
コード例 #4
0
        private static void exercise7(int hotelNo, int roomNo, string serverUrl)
        {
            Console.WriteLine("7) Insert a new Room on Hotel number 4");

            //First we create the new room object
            Room myNewRoom = new Room()
            {
                Hotel_No = hotelNo,
                Price = 250.0,
                Room_No = roomNo,
                Types = "S"
            };
            CreateRoom(serverUrl, myNewRoom);
        }