Пример #1
0
        public IActionResult ListBooksCheckedOut()
        {
            HttpResponseMessage response = client.
                                           GetAsync("api/Books/GetBooksByUser/" + user.ToString()).Result;
            List <UserBookAssociation> data = response.Content.
                                              ReadAsAsync <List <UserBookAssociation> >().Result;
            List <Books>           books1 = new List <Books>();
            List <BooksCheckedOut> books2 = new List <BooksCheckedOut>();

            foreach (var book in data)
            {
                HttpResponseMessage response2 = client.
                                                GetAsync("api/Books/GetBooks/" + book.Id.ToString()).Result;
                Books data2 = response2.Content.
                              ReadAsAsync <Books>().Result;
                books1.Add(data2);
            }
            for (int i = 0; i < books1.Count; i++)
            {
                books2.Add(new BooksCheckedOut {
                    UserId = data[i].UserId, BookId = books1[i].BookId, Title = books1[i].Title, Author = books1[i].Author, DueDate = data[i].DueDate
                });
            }
            return(View(books2));
        }
        public IActionResult PostBooks(Books book)
        {
            /*_oBook = new Books();
             * using (var httpClient = new HttpClient(_clientHandler))
             * {
             *  StringContent content = new StringContent(JsonConvert.SerializeObject(book),Encoding.UTF8,"application/json");
             *
             *  using (var response = await httpClient.PostAsync("https://localhost:5001/api/Books/PostBooks",content))
             *  {
             *      string apiResponse = await response.Content.ReadAsStringAsync();
             *      _oBook = JsonConvert.DeserializeObject<Books>(apiResponse);
             *  }
             * }
             * return _oBook;*/
            string json1        = JsonConvert.SerializeObject(book);
            var    httpContent  = new StringContent(json1, Encoding.UTF8, "application/json");
            var    httpResponse = client.PostAsync("api/Books/PostBooks/" + lib.ToString(), httpContent);

            return(RedirectToAction("ListBooks", new libId {
                id = lib
            }));
        }